#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 */

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ unlink(char *);
int main(int ac, char **av) {
  char *fn = av[1];
  for (int i = 0; i < 10000; i++) {
    int fd = open(fn, O_CREAT | O_RDWR | O_EXCL, 0666);
    if (fd < 0)
      perror("open");
    close(fd);
    unlink(fn);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2710,-0x1c(%rbp)
jge    11d7 <main+0x97>
mov    -0x18(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11b8 <main+0x78>
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1169 <main+0x29>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x8(%rsi),%r15
mov    $0x2710,%ebx
mov    0x2e9d(%rip),%r12
mov    0x2e7e(%rip),%r13
mov    0x2e67(%rip),%r14
jmp    1184 <main+0x44>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
add    $0xffffffff,%ebx
je     11b0 <main+0x70>
mov    0x0(%r13),%esi
or     (%r12),%esi
or     (%r14),%esi
mov    %r15,%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1170 <main+0x30>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
jmp    1170 <main+0x30>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@.str = [5 x i8] c"open\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
store i8* %11, i8** %6
store i32 0, i32* %7
br label %12
12: ; preds = %32, %2
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 10000
br i1 %14, label %15, label %35
15: ; preds = %12
%16 = load i8*, i8** %6
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_RDWR
%19 = or i32 %17, %18
%20 = load i32, i32* @O_EXCL
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %16, i32 %21, i32 438)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %15
%28 = load i32, i32* %8
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %6
%31 = call i32 @unlink(i8* %30)
br label %32
32: ; preds = %27
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %12
35: ; preds = %12
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@.str = [5 x i8] c"open\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
br label %6
5: ; preds = %17
ret i32 0
6: ; preds = %2, %17
%7 = phi i32 [ 0, %2 ], [ %20, %17 ]
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %9, %8
%11 = load i32, i32* @O_EXCL
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* %4, i32 %12, i32 438)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %6
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %6
%18 = tail call i32 @close(i32 %13)
%19 = tail call i32 @unlink(i8* %4)
%20 = add nuw nsw i32 %7, 1
%21 = icmp eq i32 %20, 10000
br i1 %21, label %5, label %6
}