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

/* 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 /*<<< orphan*/ write(int, char *, int);
int main(int ac, char **av) {
  char *fn = av[1];
  for (int i = 0; i < 1000; i++) {
    int fd = open(fn, O_CREAT | O_RDWR | O_EXCL, 0666);
    if (fd < 0)
      perror("open");
    char buf[128];
    write(fd, buf, sizeof(buf));
    close(fd);
    unlink(fn);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%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   $0x3e8,-0x1c(%rbp)
jge    11fe <main+0xae>
mov    -0x18(%rbp),%rdi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11cb <main+0x7b>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1040 <write@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117c <main+0x2c>
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x8(%rsi),%r12
mov    $0x3e8,%ebx
mov    0x2e6f(%rip),%r14
mov    0x2e58(%rip),%r13
mov    %rsp,%r15
jmp    11a3 <main+0x53>
nopl   (%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x80,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12,%rdi
call   1030 <unlink@plt>
add    $0xffffffff,%ebx
je     11d4 <main+0x84>
mov    (%r14),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
or     0x0(%r13),%esi
mov    %r12,%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1180 <main+0x30>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
jmp    1180 <main+0x30>
xor    %eax,%eax
add    $0x88,%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
%9 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
store i8* %12, i8** %6
store i32 0, i32* %7
br label %13
13: ; preds = %36, %2
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 1000
br i1 %15, label %16, label %39
16: ; preds = %13
%17 = load i8*, i8** %6
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_RDWR
%20 = or i32 %18, %19
%21 = load i32, i32* @O_EXCL
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %17, i32 %22, i32 438)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %16
%29 = load i32, i32* %8
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%31 = call i32 @write(i32 %29, i8* %30, i32 128)
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = load i8*, i8** %6
%35 = call i32 @unlink(i8* %34)
br label %36
36: ; preds = %28
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %13
39: ; preds = %13
%40 = load i32, i32* %3
ret i32 %40
}

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 = alloca [128 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
br label %8
7: ; preds = %19
ret i32 0
8: ; preds = %2, %19
%9 = phi i32 [ 0, %2 ], [ %23, %19 ]
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %11, %10
%13 = load i32, i32* @O_EXCL
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %5, i32 %14, i32 438)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %8
%20 = call i32 @write(i32 %15, i8* %6, i32 128)
%21 = tail call i32 @close(i32 %15)
%22 = tail call i32 @unlink(i8* %5)
%23 = add nuw nsw i32 %9, 1
%24 = icmp eq i32 %23, 1000
br i1 %24, label %7, label %8
}