#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 S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int errno;
int fchmod(int, int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int mkstemp(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
scalar_t__ unlink(char *);
char *xmalloc(size_t);
FILE *tempfile(const char *base) {
  FILE *f;
  size_t baselen;
  const char *dir;
  size_t dirlen;
  char *template = NULL;
  size_t templatelen;
  int fd = -1;
  int saved_errno;
  if (!base || (*base == '\0')) {
    base = "tmp";
  }
  if (!(dir = getenv("TMPDIR"))) {
    dir = "/tmp";
  }
  dirlen = strlen(dir);
  baselen = strlen(base);
  templatelen = dirlen + 1 + baselen + 6;
  template = xmalloc((templatelen + 1) * sizeof(char));
  strncpy(template, dir, dirlen);
  if (dirlen == 0 || template[dirlen - 1] != '/') {
    template[dirlen++] = '/';
  }
  strncpy(template + dirlen, base, baselen);
  strcpy(template + dirlen + baselen, "XXXXXX");
  if ((fd = mkstemp(template)) == -1) {
    goto error_exit;
  }
  if (fchmod(fd, S_IRUSR | S_IWUSR) == -1) {
    goto error_exit;
  }
  if (unlink(template) != 0) {
    goto error_exit;
  }
  if (!(f = fdopen(fd, "w+"))) {
    goto error_exit;
  }
  free(template);
  return f;
error_exit:
  saved_errno = errno;
  if (fd >= 0) {
    close(fd);
  }
  if (template) {
    (void)remove(template);
    free(template);
  }
  errno = saved_errno;
  return NULL;
}


x86 assembly (clang, -O0):
<tempfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x44(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f6 <tempfile+0x36>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1201 <tempfile+0x41>
lea    0xe0b(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xdf8(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1226 <tempfile+0x66>
lea    0xde5(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
add    -0x20(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   10a0 <xmalloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <strncpy@plt>
cmpq   $0x0,-0x30(%rbp)
je     129e <tempfile+0xde>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     12b5 <tempfile+0xf5>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
movb   $0x2f,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <strncpy@plt>
mov    -0x38(%rbp),%rdi
add    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
lea    0xd2f(%rip),%rsi
call   1080 <strcpy@plt>
mov    -0x38(%rbp),%rdi
call   10c0 <mkstemp@plt>
mov    %eax,-0x44(%rbp)
cmp    $0xffffffff,%eax
jne    12fc <tempfile+0x13c>
jmp    1374 <tempfile+0x1b4>
mov    -0x44(%rbp),%edi
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
mov    0x2cd9(%rip),%rax
or     (%rax),%esi
call   10e0 <fchmod@plt>
cmp    $0xffffffff,%eax
jne    1324 <tempfile+0x164>
jmp    1374 <tempfile+0x1b4>
mov    -0x38(%rbp),%rdi
call   1050 <unlink@plt>
cmp    $0x0,%rax
je     133c <tempfile+0x17c>
jmp    1374 <tempfile+0x1b4>
mov    -0x44(%rbp),%edi
lea    0xccd(%rip),%rsi
call   10d0 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    135e <tempfile+0x19e>
jmp    1374 <tempfile+0x1b4>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13c3 <tempfile+0x203>
mov    0x2c5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x44(%rbp)
jl     1392 <tempfile+0x1d2>
mov    -0x44(%rbp),%edi
call   10b0 <close@plt>
cmpq   $0x0,-0x38(%rbp)
je     13af <tempfile+0x1ef>
mov    -0x38(%rbp),%rdi
call   1070 <remove@plt>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x48(%rbp),%ecx
mov    0x2c1f(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tempfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     11d5 <tempfile+0x15>
mov    %rdi,%r15
cmpb   $0x0,(%rdi)
jne    11dc <tempfile+0x1c>
lea    0xe2c(%rip),%r15
lea    0xe1d(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe15(%rip),%rbp
cmovne %rax,%rbp
mov    %rbp,%rdi
call   1090 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1090 <strlen@plt>
mov    %rax,%r12
lea    (%rax,%rbx,1),%rdi
add    $0x8,%rdi
call   10a0 <xmalloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1060 <strncpy@plt>
test   %rbx,%rbx
je     1237 <tempfile+0x77>
cmpb   $0x2f,-0x1(%rbx,%r14,1)
je     1240 <tempfile+0x80>
movb   $0x2f,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rbp
add    %rbx,%rbp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <strncpy@plt>
add    %r12,%rbp
lea    0xdae(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcpy@plt>
mov    %r14,%rdi
call   10c0 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     12b6 <tempfile+0xf6>
mov    %eax,%ebp
mov    0x2d54(%rip),%rax
mov    0x2d65(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   10e0 <fchmod@plt>
cmp    $0xffffffff,%eax
je     12a0 <tempfile+0xe0>
mov    %r14,%rdi
call   1050 <unlink@plt>
test   %rax,%rax
je     12e6 <tempfile+0x126>
mov    0x2d31(%rip),%rax
mov    (%rax),%ebx
test   %ebp,%ebp
js     12bf <tempfile+0xff>
mov    %ebp,%edi
call   10b0 <close@plt>
jmp    12bf <tempfile+0xff>
mov    0x2d1b(%rip),%rax
mov    (%rax),%ebx
mov    %r14,%rdi
call   1070 <remove@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    0x2d02(%rip),%rax
mov    %ebx,(%rax)
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd26(%rip),%rsi
mov    %ebp,%edi
call   10d0 <fdopen@plt>
test   %rax,%rax
je     12a0 <tempfile+0xe0>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <free@plt>
jmp    12da <tempfile+0x11a>

LLVM IR (-O0):
@.str = [4 x i8] c"tmp\00"
@.str.1 = [7 x i8] c"TMPDIR\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [7 x i8] c"XXXXXX\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [3 x i8] c"w+\00"
@errno = global i32 0
define i32* @tempfile(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %8
store i32 -1, i32* %10
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14, %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %3
br label %20
20: ; preds = %19, %14
%21 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* %21, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %20
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %6
br label %24
24: ; preds = %23, %20
%25 = load i8*, i8** %6
%26 = call i64 @strlen(i8* %25)
store i64 %26, i64* %7
%27 = load i8*, i8** %3
%28 = call i64 @strlen(i8* %27)
store i64 %28, i64* %5
%29 = load i64, i64* %7
%30 = add i64 %29, 1
%31 = load i64, i64* %5
%32 = add i64 %30, %31
%33 = add i64 %32, 6
store i64 %33, i64* %9
%34 = load i64, i64* %9
%35 = add i64 %34, 1
%36 = mul i64 %35, 1
%37 = call i8* @xmalloc(i64 %36)
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = load i8*, i8** %6
%40 = load i64, i64* %7
%41 = call i32 @strncpy(i8* %38, i8* %39, i64 %40)
%42 = load i64, i64* %7
%43 = icmp eq i64 %42, 0
br i1 %43, label %52, label %44
44: ; preds = %24
%45 = load i8*, i8** %8
%46 = load i64, i64* %7
%47 = sub i64 %46, 1
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 47
br i1 %51, label %52, label %57
52: ; preds = %44, %24
%53 = load i8*, i8** %8
%54 = load i64, i64* %7
%55 = add i64 %54, 1
store i64 %55, i64* %7
%56 = getelementptr inbounds i8, i8* %53, i64 %54
store i8 47, i8* %56
br label %57
57: ; preds = %52, %44
%58 = load i8*, i8** %8
%59 = load i64, i64* %7
%60 = getelementptr inbounds i8, i8* %58, i64 %59
%61 = load i8*, i8** %3
%62 = load i64, i64* %5
%63 = call i32 @strncpy(i8* %60, i8* %61, i64 %62)
%64 = load i8*, i8** %8
%65 = load i64, i64* %7
%66 = getelementptr inbounds i8, i8* %64, i64 %65
%67 = load i64, i64* %5
%68 = getelementptr inbounds i8, i8* %66, i64 %67
%69 = call i32 @strcpy(i8* %68, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%70 = load i8*, i8** %8
%71 = call i32 @mkstemp(i8* %70)
store i32 %71, i32* %10
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %74
73: ; preds = %57
br label %96
74: ; preds = %57
%75 = load i32, i32* %10
%76 = load i32, i32* @S_IRUSR
%77 = load i32, i32* @S_IWUSR
%78 = or i32 %76, %77
%79 = call i32 @fchmod(i32 %75, i32 %78)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %82
81: ; preds = %74
br label %96
82: ; preds = %74
%83 = load i8*, i8** %8
%84 = call i64 @unlink(i8* %83)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %82
br label %96
87: ; preds = %82
%88 = load i32, i32* %10
%89 = call i32* @fdopen(i32 %88, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %89, i32** %4
%90 = icmp ne i32* %89, null
br i1 %90, label %92, label %91
91: ; preds = %87
br label %96
92: ; preds = %87
%93 = load i8*, i8** %8
%94 = call i32 @free(i8* %93)
%95 = load i32*, i32** %4
store i32* %95, i32** %2
br label %113
96: ; preds = %91, %86, %81, %73
%97 = load i32, i32* @errno
store i32 %97, i32* %11
%98 = load i32, i32* %10
%99 = icmp sge i32 %98, 0
br i1 %99, label %100, label %103
100: ; preds = %96
%101 = load i32, i32* %10
%102 = call i32 @close(i32 %101)
br label %103
103: ; preds = %100, %96
%104 = load i8*, i8** %8
%105 = icmp ne i8* %104, null
br i1 %105, label %106, label %111
106: ; preds = %103
%107 = load i8*, i8** %8
%108 = call i32 @remove(i8* %107)
%109 = load i8*, i8** %8
%110 = call i32 @free(i8* %109)
br label %111
111: ; preds = %106, %103
%112 = load i32, i32* %11
store i32 %112, i32* @errno
store i32* null, i32** %2
br label %113
113: ; preds = %111, %92
%114 = load i32*, i32** %2
ret i32* %114
}

LLVM IR (-O2):
@.str = [4 x i8] c"tmp\00"
@.str.1 = [7 x i8] c"TMPDIR\00"
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [7 x i8] c"XXXXXX\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [3 x i8] c"w+\00"
@errno = global i32 0
define noalias i32* @tempfile(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %6, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %3, %1
br label %7
7: ; preds = %6, %3
%8 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %6 ], [ %0, %3 ]
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i8* %9, null
%11 = select i1 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %9
%12 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%13 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%14 = add i64 %12, 1
%15 = add i64 %13, 7
%16 = add i64 %15, %14
%17 = tail call i8* @xmalloc(i64 %16)
%18 = tail call i32 @strncpy(i8* %17, i8* %11, i64 %12)
%19 = icmp eq i64 %12, 0
br i1 %19, label %25, label %20
20: ; preds = %7
%21 = add i64 %12, -1
%22 = getelementptr inbounds i8, i8* %17, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 47
br i1 %24, label %27, label %25
25: ; preds = %20, %7
%26 = getelementptr inbounds i8, i8* %17, i64 %12
store i8 47, i8* %26
br label %27
27: ; preds = %25, %20
%28 = phi i64 [ %14, %25 ], [ %12, %20 ]
%29 = getelementptr inbounds i8, i8* %17, i64 %28
%30 = tail call i32 @strncpy(i8* %29, i8* %8, i64 %13)
%31 = getelementptr inbounds i8, i8* %29, i64 %13
%32 = tail call i32 @strcpy(i8* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%33 = tail call i32 @mkstemp(i8* %17)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = load i32, i32* @errno
br label %56
37: ; preds = %27
%38 = load i32, i32* @S_IRUSR
%39 = load i32, i32* @S_IWUSR
%40 = or i32 %39, %38
%41 = tail call i32 @fchmod(i32 %33, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %51, label %43
43: ; preds = %37
%44 = tail call i64 @unlink(i8* %17)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = tail call i32* @fdopen(i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%48 = icmp eq i32* %47, null
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = tail call i32 @free(i8* %17)
br label %60
51: ; preds = %46, %43, %37
%52 = load i32, i32* @errno
%53 = icmp sgt i32 %33, -1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = tail call i32 @close(i32 %33)
br label %56
56: ; preds = %35, %54, %51
%57 = phi i32 [ %36, %35 ], [ %52, %54 ], [ %52, %51 ]
%58 = tail call i32 @remove(i8* %17)
%59 = tail call i32 @free(i8* %17)
store i32 %57, i32* @errno
br label %60
60: ; preds = %56, %49
%61 = phi i32* [ null, %56 ], [ %47, %49 ]
ret i32* %61
}