#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*/ sprintf(char *, char *, char *, int);
int main(int ac, char **av) {
  char *base = av[1];
  for (int i = 0; i < 1000; i++) {
    char fn[256];
    sprintf(fn, "%s/f%d.txt", base, i);
    int fd = open(fn, O_CREAT | O_RDWR | O_EXCL, 0666);
    if (fd < 0)
      perror("open");
    close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%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    11f7 <main+0xb7>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe72(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
or     (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1040 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    11de <main+0x9e>
lea    0xe32(%rip),%rdi
call   1050 <perror@plt>
mov    -0x124(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116c <main+0x2c>
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    0x8(%rsi),%r15
xor    %ebp,%ebp
mov    %rsp,%r13
mov    0x2e7f(%rip),%r14
mov    0x2e68(%rip),%r12
jmp    1182 <main+0x42>
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x1,%ebp
cmp    $0x3e8,%ebp
je     11c7 <main+0x87>
mov    %r13,%rdi
lea    0xe74(%rip),%rsi
mov    %r15,%rdx
mov    %ebp,%ecx
call   1060 <sprintf@plt>
mov    (%r14),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
or     (%r12),%esi
mov    %r13,%rdi
mov    $0x1b6,%edx
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1170 <main+0x30>
lea    0xe4b(%rip),%rdi
call   1050 <perror@plt>
jmp    1170 <main+0x30>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s/f%d.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@.str.1 = [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 [256 x i8]
%9 = alloca i32
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 = %35, %2
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 1000
br i1 %15, label %16, label %38
16: ; preds = %13
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_RDWR
%24 = or i32 %22, %23
%25 = load i32, i32* @O_EXCL
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %21, i32 %26, i32 438)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %16
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %16
%33 = load i32, i32* %9
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %13
38: ; preds = %13
%39 = load i32, i32* %3
ret i32 %39
}

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