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


x86 assembly (clang, -O0):
<tmpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0xeb1(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xeae(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeac(%rip),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x64,-0x34(%rbp)
jge    1218 <tmpfile+0xd8>
lea    -0x20(%rbp),%rdi
add    $0xd,%rdi
call   1040 <__randname@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1050 <sys_open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1205 <tmpfile+0xc5>
lea    -0x20(%rbp),%rsi
mov    $0xa,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    -0x24(%rbp),%edi
lea    0xe3f(%rip),%rsi
call   1060 <__fdopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f8 <tmpfile+0xb8>
mov    -0x24(%rbp),%esi
mov    $0x6,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1220 <tmpfile+0xe0>
jmp    120a <tmpfile+0xca>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    116e <tmpfile+0x2e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tmpfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movaps 0xeab(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x585858,0x10(%rsp)
mov    $0x64,%ebx
mov    0x2e73(%rip),%r13
mov    0x2e84(%rip),%r14
mov    0x2e55(%rip),%r15
mov    %rsp,%r12
xchg   %ax,%ax
lea    0xd(%rsp),%rdi
call   1040 <__randname@plt>
mov    (%r14),%esi
or     0x0(%r13),%esi
or     (%r15),%esi
mov    %r12,%rdi
mov    $0x180,%edx
call   1050 <sys_open@plt>
test   %eax,%eax
jns    11ae <tmpfile+0x6e>
add    $0xffffffff,%ebx
jne    1180 <tmpfile+0x40>
xor    %ebx,%ebx
jmp    11e5 <tmpfile+0xa5>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    $0xa,%edi
xor    %eax,%eax
call   1030 <__syscall@plt>
lea    0xe4e(%rip),%rsi
mov    %ebp,%edi
call   1060 <__fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e5 <tmpfile+0xa5>
xor    %ebx,%ebx
mov    $0x6,%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.tmpfile.s = [20 x i8] c"/tmp/tmpfile_XXXXXX\00"
@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 [20 x i8]
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = bitcast [20 x i8]* %2 to i8*
store i32 0, i32* %5
br label %7
7: ; preds = %36, %0
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 100
br i1 %9, label %10, label %39
10: ; preds = %7
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%12 = getelementptr inbounds i8, i8* %11, i64 13
%13 = call i32 @__randname(i8* %12)
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_EXCL
%19 = or i32 %17, %18
%20 = call i32 @sys_open(i8* %14, i32 %19, i32 384)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %10
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%25 = call i32 (i32, ...) @__syscall(i32 10, i8* %24)
%26 = load i32, i32* %3
%27 = call i32* @__fdopen(i32 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %4
%28 = load i32*, i32** %4
%29 = icmp ne i32* %28, null
br i1 %29, label %33, label %30
30: ; preds = %23
%31 = load i32, i32* %3
%32 = call i32 (i32, ...) @__syscall(i32 6, i32 %31)
br label %33
33: ; preds = %30, %23
%34 = load i32*, i32** %4
store i32* %34, i32** %1
br label %40
35: ; preds = %10
br label %36
36: ; preds = %35
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %7
39: ; preds = %7
store i32* null, i32** %1
br label %40
40: ; preds = %39, %33
%41 = load i32*, i32** %1
ret i32* %41
}

LLVM IR (-O2):
@__const.tmpfile.s = [20 x i8] c"/tmp/tmpfile_XXXXXX\00"
@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 [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 13
br label %7
4: ; preds = %7
%5 = add nuw nsw i32 %8, 1
%6 = icmp eq i32 %5, 100
br i1 %6, label %23, label %7
7: ; preds = %0, %4
%8 = phi i32 [ 0, %0 ], [ %5, %4 ]
%9 = call i32 @__randname(i8* %3)
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = call i32 @sys_open(i8* %2, i32 %14, i32 384)
%16 = icmp sgt i32 %15, -1
br i1 %16, label %17, label %4
17: ; preds = %7
%18 = call i32 (i32, ...) @__syscall(i32 10, i8* %2)
%19 = call i32* @__fdopen(i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i32, ...) @__syscall(i32 6, i32 %15)
br label %23
23: ; preds = %4, %17, %21
%24 = phi i32* [ null, %21 ], [ %19, %17 ], [ null, %4 ]
ret i32* %24
}