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


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)
movl   $0x0,-0x14(%rbp)
cmpl   $0x186a0,-0x14(%rbp)
jge    1188 <main+0x68>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1175 <main+0x55>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
jmp    117a <main+0x5a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113d <main+0x1d>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    $0x186a0,%ebp
mov    0x2eb5(%rip),%r14
jmp    1145 <main+0x25>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffff,%ebp
je     115e <main+0x3e>
mov    0x8(%rbx),%rdi
mov    (%r14),%esi
call   1040 <open@plt>
test   %eax,%eax
js     1140 <main+0x20>
mov    %eax,%edi
call   1030 <close@plt>
jmp    1140 <main+0x20>
xor    %eax,%eax
pop    %rbx
pop    %r14
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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %8
8: ; preds = %23, %2
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 100000
br i1 %10, label %11, label %26
11: ; preds = %8
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = load i32, i32* %7
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %19, %11
br label %23
23: ; preds = %22
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
br label %8
26: ; preds = %8
%27 = load i32, i32* %3
ret i32 %27
}

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