#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 L_tmpnam;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ *__fdopen(int, char *);
int /*<<< orphan*/ __syscall(int, char *);
int sys_open(char *, int, int);
char *tmpnam(char *);
FILE *tmpfile(void) {
  char buf[L_tmpnam], *s;
  int fd;
  FILE *f;
  int try;
  for (try = 0; try < 100; try++) {
    s = tmpnam(buf);
    if (!s)
      return 0;
    fd = sys_open(s, O_RDWR | O_CREAT | O_EXCL, 0600);
    if (fd >= 0) {
      f = __fdopen(fd, "w+");
      __syscall(10, s);
      return f;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<tmpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x64,-0x34(%rbp)
jge    122f <tmpfile+0xef>
mov    -0x40(%rbp),%rdi
call   1060 <tmpnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <tmpfile+0x71>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    123e <tmpfile+0xfe>
mov    -0x20(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1040 <sys_open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     121c <tmpfile+0xdc>
mov    -0x24(%rbp),%edi
lea    0xe0f(%rip),%rsi
call   1050 <__fdopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rsi
mov    $0xa,%edi
call   1030 <__syscall@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    123e <tmpfile+0xfe>
jmp    1221 <tmpfile+0xe1>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    117b <tmpfile+0x3b>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%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   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    $0x64,%ebx
mov    0x2e84(%rip),%r14
mov    0x2e4d(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1060 <tmpnam@plt>
test   %rax,%rax
je     11b6 <tmpfile+0x76>
mov    %rax,%r12
mov    (%r14),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
or     0x0(%r13),%esi
mov    %r12,%rdi
mov    $0x180,%edx
call   1040 <sys_open@plt>
test   %eax,%eax
jns    11ba <tmpfile+0x7a>
add    $0xffffffff,%ebx
jne    1180 <tmpfile+0x40>
xor    %ebx,%ebx
jmp    11d8 <tmpfile+0x98>
lea    0xe3f(%rip),%rsi
mov    %eax,%edi
call   1050 <__fdopen@plt>
mov    %rax,%rbx
mov    $0xa,%edi
mov    %r12,%rsi
call   1030 <__syscall@plt>
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@L_tmpnam = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [3 x i8] c"w+\00"
define i32* @tmpfile() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = load i32, i32* @L_tmpnam
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %2
%12 = alloca i8, i64 %10
store i64 %10, i64* %3
store i32 0, i32* %7
br label %13
13: ; preds = %38, %0
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 100
br i1 %15, label %16, label %41
16: ; preds = %13
%17 = call i8* @tmpnam(i8* %12)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %16
store i32* null, i32** %1
store i32 1, i32* %8
br label %42
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = load i32, i32* @O_EXCL
%27 = or i32 %25, %26
%28 = call i32 @sys_open(i8* %22, i32 %27, i32 384)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %21
%32 = load i32, i32* %5
%33 = call i32* @__fdopen(i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %33, i32** %6
%34 = load i8*, i8** %4
%35 = call i32 @__syscall(i32 10, i8* %34)
%36 = load i32*, i32** %6
store i32* %36, i32** %1
store i32 1, i32* %8
br label %42
37: ; preds = %21
br label %38
38: ; preds = %37
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %13
41: ; preds = %13
store i32* null, i32** %1
store i32 1, i32* %8
br label %42
42: ; preds = %41, %31, %20
%43 = load i8*, i8** %2
%44 = load i32*, i32** %1
ret i32* %44
}

LLVM IR (-O2):
@L_tmpnam = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str = [3 x i8] c"w+\00"
define i32* @tmpfile() {
%1 = load i32, i32* @L_tmpnam
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
br label %7
4: ; preds = %11
%5 = add nuw nsw i32 %8, 1
%6 = icmp eq i32 %5, 100
br i1 %6, label %22, label %7
7: ; preds = %0, %4
%8 = phi i32 [ 0, %0 ], [ %5, %4 ]
%9 = call i8* @tmpnam(i8* %3)
%10 = icmp eq i8* %9, null
br i1 %10, label %22, label %11
11: ; preds = %7
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_EXCL
%16 = or i32 %14, %15
%17 = call i32 @sys_open(i8* %9, i32 %16, i32 384)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %4
19: ; preds = %11
%20 = call i32* @__fdopen(i32 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @__syscall(i32 10, i8* %9)
br label %22
22: ; preds = %4, %7, %19
%23 = phi i32* [ %20, %19 ], [ null, %7 ], [ null, %4 ]
ret i32* %23
}