#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 /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, int);
char *tmpnam(int /*<<< orphan*/ *);
int main() {
  char *path;
  int i;
  for (i = 0; i < 2000; i++) {
    path = tmpnam(NULL);
    FILE *fp = fopen(path, "w+");
    if (NULL == fp) {
      printf("%d\n", i);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x7d0,-0x14(%rbp)
jge    11ae <main+0x7e>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <tmpnam@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    119b <main+0x6b>
mov    -0x14(%rbp),%esi
lea    0xe79(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <main+0x85>
jmp    11a0 <main+0x70>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <main+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %ebx,%ebx
lea    0xec3(%rip),%r14
nopl   (%rax)
xor    %edi,%edi
call   1050 <tmpnam@plt>
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1166 <main+0x36>
add    $0x1,%ebx
cmp    $0x7d0,%ebx
jne    1140 <main+0x10>
xor    %eax,%eax
jmp    1179 <main+0x49>
lea    0xe96(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %3
br label %5
5: ; preds = %18, %0
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 2000
br i1 %7, label %8, label %21
8: ; preds = %5
%9 = call i8* @tmpnam(i32* null)
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp eq i32* null, %12
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i32, i32* %3
%16 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 -1, i32* %1
br label %22
17: ; preds = %8
br label %18
18: ; preds = %17
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
store i32 %20, i32* %3
br label %5
21: ; preds = %5
store i32 0, i32* %1
br label %22
22: ; preds = %21, %14
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
br label %1
1: ; preds = %0, %8
%2 = phi i32 [ 0, %0 ], [ %9, %8 ]
%3 = tail call i8* @tmpnam(i32* null)
%4 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %2)
br label %11
8: ; preds = %1
%9 = add nuw nsw i32 %2, 1
%10 = icmp eq i32 %9, 2000
br i1 %10, label %11, label %1
11: ; preds = %8, %6
%12 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %12
}