//
// sub_1004F4A50 from 0x1004f4a50 to 0x1004f4d6f (799 bytes)
//
// 3 xrefs:
// > sub_1000E7B50 @ 0x1000e7be2
// > sub_1000E7DF0 @ 0x1000e7e95
// > sub_10054CEA0 @ 0x10054cf37
//
// This function is hookable on this platform (idp: pc, 32 bit: False, 64 bit: True!
//
// Using BromaIDA 8.3.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit 1c6ec395 at Thu Nov 26 21:29:22 2026 from https://github.com/geode-sdk/bindings
// Generated at Fri, 27 Nov 2026 12:31:26 +0000
//
__int64 __fastcall sub_1004F4A50(std::string *__str, unsigned __int8 *a2, int a3, char a4)
{
unsigned __int8 *v5; // r14
__int64 v6; // rax
unsigned int v7; // edx
std::string::size_type size; // rdx
const std::string::value_type *data; // r15
std::string *v10; // rax
std::string::__short::$7162EEB0FEF29CC674EFA33D0008C7E0 v11; // bl
std::string::value_type v12; // r14
std::string::size_type v13; // r15
std::string::__short::$7162EEB0FEF29CC674EFA33D0008C7E0 v14; // bl
size_t v15; // r12
unsigned __int64 v16; // r14
char *v17; // r13
std::string::pointer v18; // rsi
const std::__fs::filesystem::path *v19; // r12
const char *v20; // rdi
__int64 v21; // rsi
FILE *v22; // rax
std::error_code *v23; // rdx
FILE *v24; // r13
unsigned int v25; // edx
const void *v26; // rdi
size_t v27; // rdx
size_t v28; // r15
bool v29; // r13
unsigned int v30; // eax
__int64 v31; // rax
const std::__fs::filesystem::path *v32; // rdi
int v33; // eax
size_t v35; // r15
unsigned __int8 *v36; // [rsp+0h] [rbp-90h]
std::__fs::filesystem::path *__to; // [rsp+8h] [rbp-88h]
char v38; // [rsp+14h] [rbp-7Ch]
std::string v39; // [rsp+18h] [rbp-78h] BYREF
std::string __dst; // [rsp+30h] [rbp-60h] BYREF
_BYTE v41[14]; // [rsp+50h] [rbp-40h]
v5 = a2;
*(_WORD *)&v39.__r_.__value_.__l.0 = 0;
if ( a3 )
{
std::string::operator=(&v39, __str);
}
else
{
v6 = cocos2d::CCFileUtils::sharedFileUtils(__str, a2);
(*(void (__fastcall **)(std::string *, __int64))(*(_QWORD *)v6 + 144LL))(&__dst, v6);
v7 = (unsigned int)__str->__r_.__value_.__l.0;
if ( (v7 & 1) != 0 )
{
size = __str->__r_.__value_.__l.__size_;
data = __str->__r_.__value_.__l.__data_;
}
else
{
data = __str->__r_.__value_.__s.__data_;
size = v7 >> 1;
}
v10 = std::string::append(&__dst, data, size);
v11 = v10->__r_.__value_.__s.0;
v12 = v10->__r_.__value_.__s.__data_[0];
*(_QWORD *)&v41[6] = v10->__r_.__value_.__l.__size_;
*(_QWORD *)v41 = *(std::string::size_type *)((char *)v10->__r_.__value_.__r.__words + 2);
v13 = v10->__r_.__value_.__r.__words[2];
*(_OWORD *)&v10->__r_.__value_.__l.0 = 0;
v10->__r_.__value_.__r.__words[2] = 0;
if ( (*(_BYTE *)&v39.__r_.__value_.__s.0 & 1) != 0 )
operator delete(v39.__r_.__value_.__l.__data_);
v39.__r_.__value_.__s.0 = v11;
v39.__r_.__value_.__s.__data_[0] = v12;
*(std::string::size_type *)((char *)v39.__r_.__value_.__r.__words + 2) = *(_QWORD *)v41;
v39.__r_.__value_.__l.__size_ = *(_QWORD *)&v41[6];
v39.__r_.__value_.__r.__words[2] = v13;
if ( (*(_BYTE *)&__dst.__r_.__value_.__s.0 & 1) != 0 )
operator delete(__dst.__r_.__value_.__l.__data_);
v5 = a2;
}
__to = (std::__fs::filesystem::path *)((char *)&v39.__r_.__value_.__l.0 + 1);
v38 = a4;
v36 = v5;
if ( a4 )
{
v14 = v39.__r_.__value_.__s.0;
if ( (*(_BYTE *)&v39.__r_.__value_.__s.0 & 1) != 0 )
v15 = v39.__r_.__value_.__l.__size_;
else
v15 = *(_BYTE *)&v39.__r_.__value_.__l.0 >> 1;
v16 = v15 + 4;
if ( v15 + 4 >= 0xFFFFFFFFFFFFFFF8LL )
sub_100003630();
if ( v16 >= 0x17 )
{
v35 = 26;
if ( (v16 | 7) != 0x17 )
v35 = (v16 | 7) + 1;
v17 = (char *)operator new(v35);
__dst.__r_.__value_.__r.__words[0] = v35 | 1;
__dst.__r_.__value_.__r.__words[2] = (std::string::size_type)v17;
__dst.__r_.__value_.__l.__size_ = v15 + 4;
v18 = v39.__r_.__value_.__s.__data_;
if ( (*(_BYTE *)&v14 & 1) == 0 )
goto LABEL_22;
}
else
{
memset(&__dst, 0, sizeof(__dst));
__dst.__r_.__value_.__s.0 = (std::string::__short::$7162EEB0FEF29CC674EFA33D0008C7E0)(2 * v16);
v17 = __dst.__r_.__value_.__s.__data_;
if ( !v15 )
{
LABEL_23:
strcpy(&v17[v15], ".tmp");
goto LABEL_25;
}
v18 = v39.__r_.__value_.__s.__data_;
if ( (*(_BYTE *)&v39.__r_.__value_.__s.0 & 1) == 0 )
{
LABEL_22:
memmove(v17, v18, v15);
goto LABEL_23;
}
}
v18 = v39.__r_.__value_.__l.__data_;
goto LABEL_22;
}
if ( (*(_BYTE *)&v39.__r_.__value_.__s.0 & 1) != 0 )
sub_10001AD30(&__dst, v39.__r_.__value_.__l.__data_, v39.__r_.__value_.__l.__size_);
else
__dst = v39;
LABEL_25:
v19 = (const std::__fs::filesystem::path *)((char *)&__dst.__r_.__value_.__l.0 + 1);
v20 = __dst.__r_.__value_.__s.__data_;
if ( (*(_BYTE *)&__dst.__r_.__value_.__s.0 & 1) != 0 )
v20 = __dst.__r_.__value_.__l.__data_;
v21 = (__int64)"w";
v22 = fopen(v20, "w");
v24 = v22;
if ( v22 )
{
v25 = *v36;
if ( (v25 & 1) != 0 )
{
v26 = *((const void **)v36 + 2);
v27 = *((_QWORD *)v36 + 1);
}
else
{
v26 = v36 + 1;
v27 = v25 >> 1;
}
v21 = 1;
v28 = fwrite(v26, 1u, v27, v22);
v29 = fclose(v24) == 0;
v30 = *v36;
if ( (v30 & 1) != 0 )
v31 = *((_QWORD *)v36 + 1);
else
v31 = v30 >> 1;
LOBYTE(v24) = v28 == v31 && v29;
if ( !v38 )
goto LABEL_47;
}
else
{
LODWORD(v24) = 0;
if ( !v38 )
goto LABEL_47;
}
if ( (_BYTE)v24 )
{
v32 = (const std::__fs::filesystem::path *)((char *)&__dst.__r_.__value_.__l.0 + 1);
if ( (*(_BYTE *)&__dst.__r_.__value_.__s.0 & 1) != 0 )
v32 = (const std::__fs::filesystem::path *)__dst.__r_.__value_.__l.__data_;
if ( (*(_BYTE *)&v39.__r_.__value_.__s.0 & 1) != 0 )
__to = (std::__fs::filesystem::path *)v39.__r_.__value_.__l.__data_;
v21 = (__int64)__to;
rename(v32, __to, v23);
LOBYTE(v24) = v33 == 0;
}
if ( !(_BYTE)v24 )
{
if ( (*(_BYTE *)&__dst.__r_.__value_.__s.0 & 1) != 0 )
v19 = (const std::__fs::filesystem::path *)__dst.__r_.__value_.__l.__data_;
remove(v19, (std::error_code *)v21);
}
LABEL_47:
if ( (*(_BYTE *)&__dst.__r_.__value_.__s.0 & 1) != 0 )
{
operator delete(__dst.__r_.__value_.__l.__data_);
if ( (*(_BYTE *)&v39.__r_.__value_.__s.0 & 1) == 0 )
return (unsigned int)v24;
LABEL_51:
operator delete(v39.__r_.__value_.__l.__data_);
return (unsigned int)v24;
}
if ( (*(_BYTE *)&v39.__r_.__value_.__s.0 & 1) != 0 )
goto LABEL_51;
return (unsigned int)v24;
}