#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_buffer;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char file_buffer[1024];
  if (argc != 2) {
    fprintf(stderr, "Usage: testopen file_count\n");
    exit(1);
  }
  int file_count = atoi(argv[1]);
  for (int i = 0; i < file_count; i++) {
    snprintf(file_buffer, sizeof(file_buffer), "/tmp/tst_open%d", i);
    int fd = open(file_buffer, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
    if (fd == -1) {
      fprintf(stderr, "Unable to open %s: %s\n", file_buffer, strerror(errno));
      exit(1);
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b4 <main+0x44>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x414(%rbp)
movl   $0x0,-0x418(%rbp)
mov    -0x418(%rbp),%eax
cmp    -0x414(%rbp),%eax
jge    12bf <main+0x14f>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%ecx
mov    $0x400,%esi
lea    0xe20(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    0x2dc0(%rip),%rax
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
mov    0x2d9d(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0xffffffff,-0x41c(%rbp)
jne    12a0 <main+0x130>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x41c(%rbp),%edi
call   1040 <close@plt>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
jmp    11d1 <main+0x61>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
cmp    $0x2,%edi
jne    125a <main+0xea>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
test   %eax,%eax
jle    120f <main+0x9f>
mov    %eax,%r15d
xor    %ebp,%ebp
mov    %rsp,%rbx
mov    0x2e4a(%rip),%r14
mov    0x2e03(%rip),%r12
mov    0x2e1c(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x400,%esi
lea    0xe4d(%rip),%rdx
mov    %ebp,%ecx
call   1030 <snprintf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
or     (%r14),%esi
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1223 <main+0xb3>
mov    %eax,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    %ebp,%r15d
jne    11c0 <main+0x50>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d96(%rip),%rax
mov    (%rax),%ebx
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdeb(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Usage: testopen file_count\0A\00"
@.str.1 = [16 x i8] c"/tmp/tst_open%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [23 x i8] c"Unable to open %s: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %7
store i32 0, i32* %8
br label %21
21: ; preds = %51, %16
%22 = load i32, i32* %8
%23 = load i32, i32* %7
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %54
25: ; preds = %21
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%27 = load i32, i32* %8
%28 = call i32 @snprintf(i8* %26, i32 1024, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%30 = load i32, i32* @O_CREAT
%31 = load i32, i32* @O_RDWR
%32 = or i32 %30, %31
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %29, i32 %34, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %48
41: ; preds = %25
%42 = load i32, i32* @stderr
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %43, i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %25
%49 = load i32, i32* %9
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %21
54: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Usage: testopen file_count\0A\00"
@.str.1 = [16 x i8] c"/tmp/tst_open%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [23 x i8] c"Unable to open %s: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %35, %10
ret i32 0
16: ; preds = %10, %35
%17 = phi i32 [ %37, %35 ], [ 0, %10 ]
%18 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %20, %19
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = call i32 @open(i8* %4, i32 %23, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %4, i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %16
%36 = call i32 @close(i32 %27)
%37 = add nuw nsw i32 %17, 1
%38 = icmp eq i32 %37, %13
br i1 %38, label %15, label %16
}