#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ P_tmpdir;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int mkstemp(char *);
int /*<<< orphan*/ strcmp(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ *tmpfile();
int /*<<< orphan*/ unlink(char *);
char *xmalloc(size_t);
FILE *xtmpfile(void) {
  FILE *ret;
  char *tmpfname;
  char *tmpdir = getenv("TMPDIR");
  size_t tmpdirlen;
  int fd;
  if (tmpdir == NULL || !strcmp(tmpdir, P_tmpdir))
    return tmpfile();
  tmpdirlen = strlen(tmpdir);
  tmpfname = xmalloc(tmpdirlen + 8);
  strcpy(tmpfname, tmpdir);
  strcpy(tmpfname + tmpdirlen, "/XXXXXX");
  fd = mkstemp(tmpfname);
  ret = fdopen(fd, "w+b");
  if (ret == NULL)
    error(EXIT_FAILURE, errno, "fdopen");
  unlink(tmpfname);
  free(tmpfname);
  return ret;
}


x86 assembly (clang, -O0):
<xtmpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe41(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ee <xtmpfile+0x3e>
mov    -0x20(%rbp),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   10a0 <strcmp@plt>
cmp    $0x0,%eax
jne    11fe <xtmpfile+0x4e>
mov    $0x0,%al
call   1090 <tmpfile@plt>
mov    %rax,-0x8(%rbp)
jmp    129f <xtmpfile+0xef>
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x8,%rdi
call   1080 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x18(%rbp),%rdi
add    -0x28(%rbp),%rdi
lea    0xdcf(%rip),%rsi
call   1060 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <mkstemp@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
lea    0xdbc(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1285 <xtmpfile+0xd5>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
lea    0xd93(%rip),%rdx
call   10d0 <error@plt>
mov    -0x18(%rbp),%rdi
call   1050 <unlink@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xtmpfile>:
push   %r15
push   %r14
push   %rbx
lea    0xe44(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     126f <xtmpfile+0xbf>
mov    %rax,%rbx
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <strcmp@plt>
test   %eax,%eax
je     126f <xtmpfile+0xbf>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %rax,%r15
lea    0x8(%rax),%rdi
call   1080 <xmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <strcpy@plt>
add    %r14,%r15
lea    0xdf5(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcpy@plt>
mov    %r14,%rdi
call   10b0 <mkstemp@plt>
lea    0xde6(%rip),%rsi
mov    %eax,%edi
call   10c0 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1256 <xtmpfile+0xa6>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
lea    0xdc2(%rip),%rdx
call   10d0 <error@plt>
mov    %r14,%rdi
call   1050 <unlink@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1090 <tmpfile@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"TMPDIR\00"
@P_tmpdir = global i32 0
@.str.1 = [8 x i8] c"/XXXXXX\00"
@.str.2 = [4 x i8] c"w+b\00"
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str.3 = [7 x i8] c"fdopen\00"
define i32* @xtmpfile() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %0
%11 = load i8*, i8** %4
%12 = load i32, i32* @P_tmpdir
%13 = call i32 @strcmp(i8* %11, i32 %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %10, %0
%16 = call i32* (...) @tmpfile()
store i32* %16, i32** %1
br label %46
17: ; preds = %10
%18 = load i8*, i8** %4
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = add i64 %20, 8
%22 = call i8* @xmalloc(i64 %21)
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = load i8*, i8** %4
%25 = call i32 @strcpy(i8* %23, i8* %24)
%26 = load i8*, i8** %3
%27 = load i64, i64* %5
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%30 = load i8*, i8** %3
%31 = call i32 @mkstemp(i8* %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = call i32* @fdopen(i32 %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %2
%34 = load i32*, i32** %2
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %40
36: ; preds = %17
%37 = load i32, i32* @EXIT_FAILURE
%38 = load i32, i32* @errno
%39 = call i32 @error(i32 %37, i32 %38, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %36, %17
%41 = load i8*, i8** %3
%42 = call i32 @unlink(i8* %41)
%43 = load i8*, i8** %3
%44 = call i32 @free(i8* %43)
%45 = load i32*, i32** %2
store i32* %45, i32** %1
br label %46
46: ; preds = %40, %15
%47 = load i32*, i32** %1
ret i32* %47
}

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@P_tmpdir = global i32 0
@.str.1 = [8 x i8] c"/XXXXXX\00"
@.str.2 = [4 x i8] c"w+b\00"
@EXIT_FAILURE = global i32 0
@errno = global i32 0
@.str.3 = [7 x i8] c"fdopen\00"
define noalias i32* @xtmpfile() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %7, label %3
3: ; preds = %0
%4 = load i32, i32* @P_tmpdir
%5 = tail call i32 @strcmp(i8* %1, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3, %0
%8 = tail call i32* (...) @tmpfile()
br label %26
9: ; preds = %3
%10 = tail call i64 @strlen(i8* %1)
%11 = add i64 %10, 8
%12 = tail call i8* @xmalloc(i64 %11)
%13 = tail call i32 @strcpy(i8* %12, i8* %1)
%14 = getelementptr inbounds i8, i8* %12, i64 %10
%15 = tail call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @mkstemp(i8* %12)
%17 = tail call i32* @fdopen(i32 %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %9
%20 = load i32, i32* @EXIT_FAILURE
%21 = load i32, i32* @errno
%22 = tail call i32 @error(i32 %20, i32 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %19, %9
%24 = tail call i32 @unlink(i8* %12)
%25 = tail call i32 @free(i8* %12)
br label %26
26: ; preds = %23, %7
%27 = phi i32* [ %8, %7 ], [ %17, %23 ]
ret i32* %27
}