// File: /workspace/github_c_makefile_star2_O0/Paxa[P]fast_excel/libxlsxwriter/third_party/tmpfileplus/tmpfileplus.c
// Function: mktempfile_internal
static FILE *mktempfile_internal(const char *tmpdir, const char *pfx, char **tmpname_ptr, int keep)
/* PRE:
 * pfx is not NULL and points to a valid null-terminated string
 * tmpname_ptr is not NULL.
 */
{
	FILE *fp;
	int fd = 0;
	char randpart[] = "1234567890";
	size_t lentempname;
	int i;
	char *tmpname = NULL;
	int oflag, pmode;

/* In Windows, we use the _O_TEMPORARY flag with `open` to ensure the file is deleted when closed.
 * In Unix, we use the unlink function after opening the file. (This does not work in Windows,
 * which does not allow an open file to be unlinked.)
 */
#ifdef _WIN32
	/* MSVC flags */
	oflag =  _O_BINARY|_O_CREAT|_O_EXCL|_O_RDWR;
	if (!keep)
		oflag |= _O_TEMPORARY;
	pmode = _S_IREAD | _S_IWRITE;
#else
	/* Standard POSIX flags */
	oflag = O_CREAT|O_EXCL|O_RDWR;
	pmode = S_IRUSR|S_IWUSR;
#endif

	if (!tmpdir || !is_valid_dir(tmpdir)) {
		errno = ENOENT;
		return NULL;
	}

	lentempname = strlen(tmpdir) + strlen(FILE_SEPARATOR) + strlen(pfx) + strlen(randpart);
	DPRINTF1("lentempname=%d\n", lentempname);
	tmpname = malloc(lentempname + 1);
	if (!tmpname)
	{
		errno = ENOMEM;
		return NULL;
	}
	/* If we don't manage to create a file after 10 goes, there is something wrong... */
	for (i = 0; i < 10; i++)
	{
		sprintf(tmpname, "%s%s%s%s", tmpdir, FILE_SEPARATOR, pfx, set_randpart(randpart));
		DPRINTF1("[%s]\n", tmpname);
		fd = OPEN_(tmpname, oflag, pmode);
		if (fd != -1) break;
	}
	DPRINTF1("strlen(tmpname)=%d\n", strlen(tmpname));
	if (fd != -1)
	{	/* Success, so return user a proper ANSI C file pointer */
		fp = FDOPEN_(fd, "w+b");
		errno = 0;

#ifndef _WIN32
		/* [Unix only] And make sure the file will be deleted once closed */
		if (!keep) unlink(tmpname);
#endif

	}
	else
	{	/* We failed */
		fp = NULL;
	}
	if (!fp)
	{
		free(tmpname);
		tmpname = NULL;
	}

	*tmpname_ptr = tmpname;
	return fp;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x34(%rbp)
movq 0x548b1(%rip), %rax     # 0x66079
movq %rax, -0x3f(%rbp)
movw 0x548ae(%rip), %ax      # 0x66081
movw %ax, -0x37(%rbp)
movb 0x548a6(%rip), %al      # 0x66083
movb %al, -0x35(%rbp)
movq $0x0, -0x58(%rbp)
movl $0xc2, -0x5c(%rbp)
movl $0x180, -0x60(%rbp)     # imm = 0x180
cmpq $0x0, -0x10(%rbp)
je 0x1180b
movq -0x10(%rbp), %rdi
callq 0x11a80
cmpl $0x0, %eax
jne 0x11823
callq 0xc180
movl $0x2, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x1197b
movq -0x10(%rbp), %rdi
callq 0xc4e0
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
callq 0xc4e0
movq %rax, %rcx
movq -0x70(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x68(%rbp)
leaq -0x3f(%rbp), %rdi
callq 0xc4e0
movq %rax, %rcx
movq -0x68(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
addq $0x1, %rdi
callq 0xcaf0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x11892
callq 0xc180
movl $0xc, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0x1197b
movl $0x0, -0x4c(%rbp)
cmpl $0xa, -0x4c(%rbp)
jge 0x11913
movq -0x58(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x3f(%rbp), %rdi
callq 0x11ad0
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rdx
movq -0x78(%rbp), %r8
movq %rax, %r9
leaq 0x547a8(%rip), %rsi     # 0x66084
leaq 0x5b036(%rip), %rcx     # 0x6c919
movb $0x0, %al
callq 0xd000
movq -0x58(%rbp), %rdi
movl -0x5c(%rbp), %esi
movl -0x60(%rbp), %edx
movb $0x0, %al
callq 0xce50
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
je 0x11906
jmp 0x11913
jmp 0x11908
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x11899
cmpl $-0x1, -0x34(%rbp)
je 0x11948
movl -0x34(%rbp), %edi
leaq 0x5476a(%rip), %rsi     # 0x6608d
callq 0xccb0
movq %rax, -0x30(%rbp)
callq 0xc180
movl $0x0, (%rax)
cmpl $0x0, -0x24(%rbp)
jne 0x11946
movq -0x58(%rbp), %rdi
callq 0xc190
jmp 0x11950
movq $0x0, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x11968
movq -0x58(%rbp), %rdi
callq 0xc0d0
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)