#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int main(int ac, char **av) {
  for (int i = 0; i < 100000; i++) {
    int fd = open(av[1], O_RDONLY);
    char buf[4096];
    if (fd >= 0)
      read(fd, buf, sizeof(buf));
    if (fd >= 0)
      close(fd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x186a0,-0x14(%rbp)
jge    11b9 <main+0x89>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1194 <main+0x64>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
cmpl   $0x0,-0x18(%rbp)
jl     11a6 <main+0x76>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
jmp    11ab <main+0x7b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1150 <main+0x20>
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%r15
mov    $0x186a0,%ebx
mov    0x2e9a(%rip),%r12
mov    %rsp,%r14
jmp    1165 <main+0x35>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%ebx
je     1190 <main+0x60>
mov    0x8(%r15),%rdi
mov    (%r12),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1160 <main+0x30>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    1160 <main+0x30>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %9
9: ; preds = %31, %2
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 100000
br i1 %11, label %12, label %34
12: ; preds = %9
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = load i32, i32* %7
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 4096)
br label %24
24: ; preds = %20, %12
%25 = load i32, i32* %7
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* %7
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %30
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %9
34: ; preds = %9
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %7
6: ; preds = %16
ret i32 0
7: ; preds = %2, %16
%8 = phi i32 [ 0, %2 ], [ %17, %16 ]
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp sgt i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = call i32 @read(i32 %11, i8* %5, i32 4096)
%15 = tail call i32 @close(i32 %11)
br label %16
16: ; preds = %7, %13
%17 = add nuw nsw i32 %8, 1
%18 = icmp eq i32 %17, 100000
br i1 %18, label %6, label %7
}