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

/* Variables and functions */
int /*<<< orphan*/ SIG_BLOCK;
int /*<<< orphan*/ SIG_SETMASK;
char *_PATH_TMP;
int /*<<< orphan*/ _close(int);
int /*<<< orphan*/ _sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *);
int /*<<< orphan*/ asprintf(char **, char *, char const *, char *, char *);
int errno;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
scalar_t__ issetugid();
int mkstemp(char *);
int /*<<< orphan*/ sigfillset(int /*<<< orphan*/ *);
int strlen(char const *);
int /*<<< orphan*/ unlink(char *);
FILE *tmpfile() {
  sigset_t set, oset;
  FILE *fp;
  int fd, sverrno;
  char *buf;
  const char *tmpdir;
  tmpdir = NULL;
  if (issetugid() == 0)
    tmpdir = getenv("TMPDIR");
  if (tmpdir == NULL)
    tmpdir = _PATH_TMP;
  (void)asprintf(&buf, "%s%s%s", tmpdir,
                 (tmpdir[strlen(tmpdir) - 1] == '/') ? "" : "/", "tmp.XXXXXX");
  if (buf == NULL)
    return (NULL);
  sigfillset(&set);
  (void)_sigprocmask(SIG_BLOCK, &set, &oset);
  fd = mkstemp(buf);
  if (fd != -1)
    (void)unlink(buf);
  free(buf);
  (void)_sigprocmask(SIG_SETMASK, &oset, NULL);
  if (fd == -1)
    return (NULL);
  if ((fp = fdopen(fd, "w+")) == NULL) {
    sverrno = errno;
    (void)_close(fd);
    errno = sverrno;
    return (NULL);
  }
  return (fp);
}


x86 assembly (clang, -O0):
<tmpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1090 <issetugid@plt>
cmp    $0x0,%rax
jne    11e1 <tmpfile+0x31>
lea    0xe28(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fa <tmpfile+0x4a>
mov    0x2dd5(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xde0(%rip),%rcx
lea    0xde8(%rip),%rax
cmp    $0x2f,%esi
cmove  %rax,%rcx
lea    -0x28(%rbp),%rdi
lea    0xdc0(%rip),%rsi
lea    0xdc2(%rip),%r8
call   10d0 <asprintf@plt>
cmpq   $0x0,-0x28(%rbp)
jne    126b <tmpfile+0xbb>
movq   $0x0,-0x8(%rbp)
jmp    1331 <tmpfile+0x181>
lea    -0xc(%rbp),%rdi
call   1080 <sigfillset@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1050 <_sigprocmask@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <mkstemp@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     12a9 <tmpfile+0xf9>
mov    -0x28(%rbp),%rdi
call   1060 <unlink@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <_sigprocmask@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12df <tmpfile+0x12f>
movq   $0x0,-0x8(%rbp)
jmp    1331 <tmpfile+0x181>
mov    -0x1c(%rbp),%edi
lea    0xd32(%rip),%rsi
call   10c0 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1329 <tmpfile+0x179>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <_close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2cbe(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1331 <tmpfile+0x181>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tmpfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <issetugid@plt>
test   %rax,%rax
je     12ac <tmpfile+0xfc>
test   %rbx,%rbx
jne    11d9 <tmpfile+0x29>
mov    0x2df2(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
lea    0xe2e(%rip),%rax
lea    0xe18(%rip),%rcx
cmove  %rax,%rcx
lea    0xe06(%rip),%rsi
lea    0xe08(%rip),%r8
lea    0x10(%rsp),%rdi
mov    %rbx,%rdx
call   10d0 <asprintf@plt>
cmpq   $0x0,0x10(%rsp)
je     12c9 <tmpfile+0x119>
lea    0x1c(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <sigfillset@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %rbx,%rsi
call   1050 <_sigprocmask@plt>
mov    0x10(%rsp),%rdi
call   10b0 <mkstemp@plt>
mov    0x10(%rsp),%rdi
cmp    $0xffffffff,%eax
je     12cd <tmpfile+0x11d>
mov    %eax,%ebp
call   1060 <unlink@plt>
mov    0x10(%rsp),%rdi
call   1040 <free@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1050 <_sigprocmask@plt>
lea    0xd96(%rip),%rsi
mov    %ebp,%edi
call   10c0 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12e9 <tmpfile+0x139>
mov    0x2d3d(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   10a0 <_close@plt>
mov    %ebx,(%r14)
xor    %ebx,%ebx
jmp    12e9 <tmpfile+0x139>
lea    0xd4d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     11cf <tmpfile+0x1f>
jmp    11d9 <tmpfile+0x29>
xor    %ebx,%ebx
jmp    12e9 <tmpfile+0x139>
call   1040 <free@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1050 <_sigprocmask@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"TMPDIR\00"
@_PATH_TMP = global i8* null
@.str.1 = [7 x i8] c"%s%s%s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [11 x i8] c"tmp.XXXXXX\00"
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
@.str.5 = [3 x i8] c"w+\00"
@errno = global i32 0
define i32* @tmpfile() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* null, i8** %8
%9 = call i64 (...) @issetugid()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %8
br label %13
13: ; preds = %11, %0
%14 = load i8*, i8** %8
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i8*, i8** @_PATH_TMP
store i8* %17, i8** %8
br label %18
18: ; preds = %16, %13
%19 = load i8*, i8** %8
%20 = load i8*, i8** %8
%21 = load i8*, i8** %8
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %20, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 47
%29 = zext i1 %28 to i64
%30 = select i1 %28, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0)
%31 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %30, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%32 = load i8*, i8** %7
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %18
store i32* null, i32** %1
br label %65
35: ; preds = %18
%36 = call i32 @sigfillset(i32* %2)
%37 = load i32, i32* @SIG_BLOCK
%38 = call i32 @_sigprocmask(i32 %37, i32* %2, i32* %3)
%39 = load i8*, i8** %7
%40 = call i32 @mkstemp(i8* %39)
store i32 %40, i32* %5
%41 = load i32, i32* %5
%42 = icmp ne i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %35
%44 = load i8*, i8** %7
%45 = call i32 @unlink(i8* %44)
br label %46
46: ; preds = %43, %35
%47 = load i8*, i8** %7
%48 = call i32 @free(i8* %47)
%49 = load i32, i32* @SIG_SETMASK
%50 = call i32 @_sigprocmask(i32 %49, i32* %3, i32* null)
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %46
store i32* null, i32** %1
br label %65
54: ; preds = %46
%55 = load i32, i32* %5
%56 = call i32* @fdopen(i32 %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %56, i32** %4
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %63
58: ; preds = %54
%59 = load i32, i32* @errno
store i32 %59, i32* %6
%60 = load i32, i32* %5
%61 = call i32 @_close(i32 %60)
%62 = load i32, i32* %6
store i32 %62, i32* @errno
store i32* null, i32** %1
br label %65
63: ; preds = %54
%64 = load i32*, i32** %4
store i32* %64, i32** %1
br label %65
65: ; preds = %63, %58, %53, %34
%66 = load i32*, i32** %1
ret i32* %66
}

LLVM IR (-O2):
@.str = [7 x i8] c"TMPDIR\00"
@_PATH_TMP = global i8* null
@.str.1 = [7 x i8] c"%s%s%s\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [11 x i8] c"tmp.XXXXXX\00"
@SIG_BLOCK = global i32 0
@SIG_SETMASK = global i32 0
@.str.5 = [3 x i8] c"w+\00"
@errno = global i32 0
define noalias i32* @tmpfile() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i8** %3 to i8*
%7 = tail call i64 (...) @issetugid()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = phi i8* [ %10, %9 ], [ null, %0 ]
%13 = icmp eq i8* %12, null
%14 = load i8*, i8** @_PATH_TMP
%15 = select i1 %13, i8* %14, i8* %12
%16 = tail call i32 @strlen(i8* %15)
%17 = add nsw i32 %16, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 47
%22 = select i1 %21, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0)
%23 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%24 = load i8*, i8** %3
%25 = icmp eq i8* %24, null
br i1 %25, label %49, label %26
26: ; preds = %11
%27 = call i32 @sigfillset(i32* %1)
%28 = load i32, i32* @SIG_BLOCK
%29 = call i32 @_sigprocmask(i32 %28, i32* %1, i32* %2)
%30 = load i8*, i8** %3
%31 = call i32 @mkstemp(i8* %30)
%32 = icmp eq i32 %31, -1
%33 = load i8*, i8** %3
br i1 %32, label %45, label %34
34: ; preds = %26
%35 = call i32 @unlink(i8* %33)
%36 = load i8*, i8** %3
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* @SIG_SETMASK
%39 = call i32 @_sigprocmask(i32 %38, i32* %2, i32* null)
%40 = call i32* @fdopen(i32 %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %49
42: ; preds = %34
%43 = load i32, i32* @errno
%44 = call i32 @_close(i32 %31)
store i32 %43, i32* @errno
br label %49
45: ; preds = %26
%46 = call i32 @free(i8* %33)
%47 = load i32, i32* @SIG_SETMASK
%48 = call i32 @_sigprocmask(i32 %47, i32* %2, i32* null)
br label %49
49: ; preds = %45, %34, %11, %42
%50 = phi i32* [ null, %42 ], [ null, %11 ], [ %40, %34 ], [ null, %45 ]
ret i32* %50
}