#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*/ free(char *);
char *getenv(char *);
int mkstemp(char *);
char *ustrprintf(char *, char *, char const *);
int tmpfile_prefix_out(const char *prefix, char **const fname) {
  char *tmppath;
  int fd;
  char *tmpdir = getenv("TMPDIR");
  if (!tmpdir)
    tmpdir = "/tmp";
  tmppath = ustrprintf("%s/%sXXXXXX", tmpdir, prefix);
  fd = mkstemp(tmppath);
  if (fname)
    *fname = tmppath;
  else
    free(tmppath);
  return fd;
}


x86 assembly (clang, -O0):
<tmpfile_prefix_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea9(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1176 <tmpfile_prefix_out+0x36>
lea    0xe95(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xe87(%rip),%rdi
call   1060 <ustrprintf@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <mkstemp@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b5 <tmpfile_prefix_out+0x75>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    11be <tmpfile_prefix_out+0x7e>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tmpfile_prefix_out>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeaf(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xea7(%rip),%rsi
cmovne %rax,%rsi
lea    0xea1(%rip),%rdi
mov    %rbx,%rdx
call   1060 <ustrprintf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <mkstemp@plt>
mov    %eax,%ebp
test   %r14,%r14
je     118a <tmpfile_prefix_out+0x4a>
mov    %rbx,(%r14)
jmp    1192 <tmpfile_prefix_out+0x52>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [12 x i8] c"%s/%sXXXXXX\00"
define i32 @tmpfile_prefix_out(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i8** %1, i8*** %4
%8 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %7
%9 = load i8*, i8** %7
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %12
12: ; preds = %11, %2
%13 = load i8*, i8** %7
%14 = load i8*, i8** %3
%15 = call i8* @ustrprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8* %14)
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = call i32 @mkstemp(i8* %16)
store i32 %17, i32* %6
%18 = load i8**, i8*** %4
%19 = icmp ne i8** %18, null
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = load i8*, i8** %5
%22 = load i8**, i8*** %4
store i8* %21, i8** %22
br label %26
23: ; preds = %12
%24 = load i8*, i8** %5
%25 = call i32 @free(i8* %24)
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %6
ret i32 %27
}

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@.str.1 = [5 x i8] c"/tmp\00"
@.str.2 = [12 x i8] c"%s/%sXXXXXX\00"
define i32 @tmpfile_prefix_out(i8* %0, i8** writeonly %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
%5 = select i1 %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %3
%6 = tail call i8* @ustrprintf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %0)
%7 = tail call i32 @mkstemp(i8* %6)
%8 = icmp eq i8** %1, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i8* %6, i8** %1
br label %12
10: ; preds = %2
%11 = tail call i32 @free(i8* %6)
br label %12
12: ; preds = %10, %9
ret i32 %7
}