#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 FILENAME_MAX;
int __gen_tempname(char *, int /*<<< orphan*/);
scalar_t__ __path_search(char *, int, int /*<<< orphan*/ *, char *,
                         int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ remove(char *);
FILE *tmpfile(void) {
  char buf[FILENAME_MAX];
  int fd;
  FILE *f;
  if (__path_search(buf, FILENAME_MAX, NULL, "tmpf", 0))
    return NULL;
  fd = __gen_tempname(buf, 0);
  if (fd < 0)
    return NULL;
  (void)remove(buf);
  if ((f = fdopen(fd, "w+b")) == NULL)
    close(fd);
  return f;
}


x86 assembly (clang, -O0):
<tmpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
lea    0xe68(%rip),%rcx
xor    %r8d,%r8d
call   1050 <__path_search@plt>
cmp    $0x0,%rax
je     11be <tmpfile+0x6e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1227 <tmpfile+0xd7>
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1070 <__gen_tempname@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ea <tmpfile+0x9a>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1227 <tmpfile+0xd7>
mov    -0x38(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x1c(%rbp),%edi
lea    0xe08(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1218 <tmpfile+0xc8>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tmpfile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe82(%rip),%rcx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %edx,%edx
xor    %r8d,%r8d
call   1050 <__path_search@plt>
test   %rax,%rax
je     11a1 <tmpfile+0x51>
mov    %r14,%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <__gen_tempname@plt>
test   %eax,%eax
js     1193 <tmpfile+0x43>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <remove@plt>
lea    0xe41(%rip),%rsi
mov    %r15d,%edi
call   1060 <fdopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1193 <tmpfile+0x43>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
jmp    1193 <tmpfile+0x43>

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [5 x i8] c"tmpf\00"
@.str.1 = [4 x i8] c"w+b\00"
define i32* @tmpfile() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = load i32, i32* @FILENAME_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %2
%10 = alloca i8, i64 %8
store i64 %8, i64* %3
%11 = load i32, i32* @FILENAME_MAX
%12 = call i64 @__path_search(i8* %10, i32 %11, i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %0
store i32* null, i32** %1
store i32 1, i32* %6
br label %30
15: ; preds = %0
%16 = call i32 @__gen_tempname(i8* %10, i32 0)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32* null, i32** %1
store i32 1, i32* %6
br label %30
20: ; preds = %15
%21 = call i32 @remove(i8* %10)
%22 = load i32, i32* %4
%23 = call i32* @fdopen(i32 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %5
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25, %20
%29 = load i32*, i32** %5
store i32* %29, i32** %1
store i32 1, i32* %6
br label %30
30: ; preds = %28, %19, %14
%31 = load i8*, i8** %2
%32 = load i32*, i32** %1
ret i32* %32
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@.str = [5 x i8] c"tmpf\00"
@.str.1 = [4 x i8] c"w+b\00"
define noalias i32* @tmpfile() {
%1 = load i32, i32* @FILENAME_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = call i64 @__path_search(i8* %3, i32 %1, i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %15
6: ; preds = %0
%7 = call i32 @__gen_tempname(i8* %3, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = call i32 @remove(i8* %3)
%11 = call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @close(i32 %7)
br label %15
15: ; preds = %9, %13, %6, %0
%16 = phi i32* [ null, %0 ], [ null, %6 ], [ null, %13 ], [ %11, %9 ]
ret i32* %16
}