#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*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: cloexec_i 3\n");
    return -1;
  }
  int ret = 0;
  int fd = atoi(argv[1]);
  printf("get fd %d\n", fd);
  char buf[512] = {0};
  lseek(fd, 0, SEEK_SET);
  if ((ret = read(fd, buf, 512)) <= 0)
    err_sys("read file");
  printf("read %d: %s\n", fd, buf);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ad <main+0x3d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124e <main+0xde>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     1227 <main+0xb7>
lea    0xdfd(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x18(%rbp),%esi
lea    -0x220(%rbp),%rdx
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
cmp    $0x1,%edi
jg     11a3 <main+0x23>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    121d <main+0x9d>
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
lea    0xe4b(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%r14
mov    $0x200,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     1203 <main+0x83>
lea    0xe0d(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xe0b(%rip),%rdi
mov    %rsp,%rdx
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Usage: cloexec_i 3\0A\00"
@.str.1 = [11 x i8] c"get fd %d\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [10 x i8] c"read file\00"
@.str.3 = [13 x i8] c"read %d: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
13: ; preds = %2
store i32 0, i32* %6
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = bitcast [512 x i8]* %8 to i8*
%21 = load i32, i32* %7
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @lseek(i32 %21, i32 0, i32 %22)
%24 = load i32, i32* %7
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 512)
store i32 %26, i32* %6
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %13
%29 = call i32 @err_sys(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %13
%31 = load i32, i32* %7
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %31, i8* %32)
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
store i32 0, i32* %3
br label %36
36: ; preds = %30, %11
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"get fd %d\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [10 x i8] c"read file\00"
@.str.3 = [13 x i8] c"read %d: %s\0A\00"
@str = [19 x i8] c"Usage: cloexec_i 3\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %22
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @lseek(i32 %10, i32 0, i32 %13)
%15 = call i32 @read(i32 %10, i8* %12, i32 512)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = tail call i32 @err_sys(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %7
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %10, i8* %12)
%21 = call i32 @close(i32 %10)
br label %22
22: ; preds = %19, %5
%23 = phi i32 [ -1, %5 ], [ 0, %19 ]
ret i32 %23
}