#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ errno;
int mkstemp2(char *);
int /*<<< orphan*/ pm_asprintf(char const **const, char *, ...);
int /*<<< orphan*/ pm_strfree(char *);
char *strdup(char const *const);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void makeTmpfileWithTemplate(const char *const filenameTemplate, int *const fdP,
                             const char **const filenameP,
                             const char **const errorP) {
  char *filenameBuffer;
  filenameBuffer = strdup(filenameTemplate);
  if (filenameBuffer == NULL)
    pm_asprintf(errorP, "Unable to allocate storage for temporary "
                        "file name");
  else {
    int rc;
    rc = mkstemp2(filenameBuffer);
    if (rc < 0)
      pm_asprintf(errorP,
                  "Unable to create temporary file according to name "
                  "pattern '%s'.  mkstemp() failed with errno %d (%s)",
                  filenameTemplate, errno, strerror(errno));
    else {
      *fdP = rc;
      *filenameP = filenameBuffer;
      *errorP = NULL;
    }
    if (*errorP)
      pm_strfree(filenameBuffer);
  }
}


x86 assembly (clang, -O0):
<makeTmpfileWithTemplate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1197 <makeTmpfileWithTemplate+0x47>
mov    -0x20(%rbp),%rdi
lea    0xe75(%rip),%rsi
mov    $0x0,%al
call   1030 <pm_asprintf@plt>
jmp    1233 <makeTmpfileWithTemplate+0xe3>
mov    -0x28(%rbp),%rdi
call   1040 <mkstemp2@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11f8 <makeTmpfileWithTemplate+0xa8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    %eax,%r8d
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1030 <pm_asprintf@plt>
jmp    1217 <makeTmpfileWithTemplate+0xc7>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     122e <makeTmpfileWithTemplate+0xde>
mov    -0x28(%rbp),%rdi
call   1050 <pm_strfree@plt>
jmp    1233 <makeTmpfileWithTemplate+0xe3>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeTmpfileWithTemplate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r15
call   1060 <strdup@plt>
test   %rax,%rax
je     119e <makeTmpfileWithTemplate+0x4e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <mkstemp2@plt>
test   %eax,%eax
js     11bd <makeTmpfileWithTemplate+0x6d>
mov    %eax,0x0(%r13)
mov    %rbx,(%r12)
movq   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5b(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <pm_asprintf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xe5f(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
mov    %ebp,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1030 <pm_asprintf@plt>
cmpq   $0x0,(%r14)
je     118f <makeTmpfileWithTemplate+0x3f>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <pm_strfree@plt>

LLVM IR (-O0):
@.str = [51 x i8] c"Unable to allocate storage for temporary file name\00"
@.str.1 = [101 x i8] c"Unable to create temporary file according to name pattern '%s'. mkstemp() failed with errno %d (%s)\00"
@errno = global i32 0
define void @makeTmpfileWithTemplate(i8* %0, i32* %1, i8** %2, i8** %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i8**
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8** %2, i8*** %7
store i8** %3, i8*** %8
%11 = load i8*, i8** %5
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i8**, i8*** %8
%17 = call i32 (i8**, i8*, ...) @pm_asprintf(i8** %16, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %44
18: ; preds = %4
%19 = load i8*, i8** %9
%20 = call i32 @mkstemp2(i8* %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = load i8**, i8*** %8
%25 = load i8*, i8** %5
%26 = load i32, i32* @errno
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 (i8**, i8*, ...) @pm_asprintf(i8** %24, i8* getelementptr inbounds ([101 x i8], [101 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %26, i32 %28)
br label %36
30: ; preds = %18
%31 = load i32, i32* %10
%32 = load i32*, i32** %6
store i32 %31, i32* %32
%33 = load i8*, i8** %9
%34 = load i8**, i8*** %7
store i8* %33, i8** %34
%35 = load i8**, i8*** %8
store i8* null, i8** %35
br label %36
36: ; preds = %30, %23
%37 = load i8**, i8*** %8
%38 = load i8*, i8** %37
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i8*, i8** %9
%42 = call i32 @pm_strfree(i8* %41)
br label %43
43: ; preds = %40, %36
br label %44
44: ; preds = %43, %15
ret void
}

LLVM IR (-O2):
@.str = [51 x i8] c"Unable to allocate storage for temporary file name\00"
@.str.1 = [101 x i8] c"Unable to create temporary file according to name pattern '%s'. mkstemp() failed with errno %d (%s)\00"
@errno = global i32 0
define void @makeTmpfileWithTemplate(i8* %0, i32* nocapture writeonly %1, i8** nocapture writeonly %2, i8** %3) {
%5 = tail call i8* @strdup(i8* %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 (i8**, i8*, ...) @pm_asprintf(i8** %3, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
br label %21
9: ; preds = %4
%10 = tail call i32 @mkstemp2(i8* %5)
%11 = icmp slt i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %9
store i32 %10, i32* %1
store i8* %5, i8** %2
store i8* null, i8** %3
br label %21
13: ; preds = %9
%14 = load i32, i32* @errno
%15 = tail call i32 @strerror(i32 %14)
%16 = tail call i32 (i8**, i8*, ...) @pm_asprintf(i8** %3, i8* getelementptr inbounds ([101 x i8], [101 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %14, i32 %15)
%17 = load i8*, i8** %3
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = tail call i32 @pm_strfree(i8* %5)
br label %21
21: ; preds = %12, %13, %19, %7
ret void
}