#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *, ...);
int /*<<< orphan*/ execve(char *, char **, char **);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd = open("file.lk", O_RDWR | O_CREAT, 0644);
  if (fd == -1)
    err_sys("open file.lk");
  int val = 0;
  if ((val = fcntl(fd, F_GETFD, 0)) < 0)
    err_sys("fcntl error for fd %d", fd);
  if (val & FD_CLOEXEC)
    printf("fd %d: close_on_exec\n", fd);
  char const *ptr = "hello world!";
  write(fd, ptr, strlen(ptr));
  printf("open & write to fd %d\n", fd);
  char arg[128] = {0};
  sprintf(arg, "%d", fd);
  char *target = "cloexec_i";
  char *args[3] = {target, &arg[0], 0};
  char *env[1] = {0};
  execve(target, args, env);
  err_sys("execve");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
lea    0xe33(%rip),%rdi
mov    $0x1a4,%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f7 <main+0x57>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    122e <main+0x8e>
mov    -0x14(%rbp),%esi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
mov    -0x18(%rbp),%eax
mov    0x2d98(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1252 <main+0xb2>
mov    -0x14(%rbp),%esi
lea    0xdd6(%rip),%rdi
call   1070 <printf@plt>
lea    0xde0(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xd4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xd4(%rbp),%edi
mov    -0xd0(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    -0x14(%rbp),%esi
lea    0xdae(%rip),%rdi
call   1070 <printf@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <memset@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd5f(%rip),%rsi
call   10c0 <sprintf@plt>
lea    0xd90(%rip),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb8(%rbp)
movq   $0x0,-0xb0(%rbp)
lea    -0xc8(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    -0xa8(%rbp),%rdi
lea    -0xc0(%rbp),%rsi
lea    -0xc8(%rbp),%rdx
call   10a0 <execve@plt>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0xb8,%rsp
mov    0x2e40(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe53(%rip),%rdi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11d1 <main+0x41>
lea    0xe36(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11f9 <main+0x69>
lea    0xe1d(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <err_sys@plt>
mov    0x2dd0(%rip),%rax
test   %ebp,(%rax)
je     1212 <main+0x82>
lea    0xe18(%rip),%rdi
mov    %ebx,%esi
call   1070 <printf@plt>
lea    0xe20(%rip),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    0xe12(%rip),%rdi
mov    %ebx,%esi
call   1070 <printf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
lea    0xdaa(%rip),%rsi
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
mov    %ebx,%edx
call   10b0 <sprintf@plt>
lea    0xdd1(%rip),%rdi
mov    %rdi,0x10(%rsp)
mov    %rbp,0x18(%rsp)
movq   $0x0,0x20(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1090 <execve@plt>
lea    0xda9(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"file.lk\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [13 x i8] c"open file.lk\00"
@F_GETFD = global i32 0
@.str.2 = [22 x i8] c"fcntl error for fd %d\00"
@FD_CLOEXEC = global i32 0
@.str.3 = [22 x i8] c"fd %d: close_on_exec\0A\00"
@.str.4 = [13 x i8] c"hello world!\00"
@.str.5 = [23 x i8] c"open & write to fd %d\0A\00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [10 x i8] c"cloexec_i\00"
@.str.8 = [7 x i8] c"execve\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 i8*
%9 = alloca [128 x i8]
%10 = alloca i8*
%11 = alloca [3 x i8*]
%12 = alloca [1 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %15, i32 420)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
store i32 0, i32* %7
%22 = load i32, i32* %6
%23 = load i32, i32* @F_GETFD
%24 = call i32 @fcntl(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %6
%28 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26, %21
%30 = load i32, i32* %7
%31 = load i32, i32* @FD_CLOEXEC
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* %6
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %29
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8** %8
%38 = load i32, i32* %6
%39 = load i8*, i8** %8
%40 = load i8*, i8** %8
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @write(i32 %38, i8* %39, i32 %41)
%43 = load i32, i32* %6
%44 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %43)
%45 = bitcast [128 x i8]* %9 to i8*
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%47 = load i32, i32* %6
%48 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %47)
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8** %10
%49 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
%50 = load i8*, i8** %10
store i8* %50, i8** %49
%51 = getelementptr inbounds i8*, i8** %49, i64 1
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
store i8* %52, i8** %51
%53 = getelementptr inbounds i8*, i8** %51, i64 1
store i8* null, i8** %53
%54 = bitcast [1 x i8*]* %12 to i8*
%55 = load i8*, i8** %10
%56 = getelementptr inbounds [3 x i8*], [3 x i8*]* %11, i64 0, i64 0
%57 = getelementptr inbounds [1 x i8*], [1 x i8*]* %12, i64 0, i64 0
%58 = call i32 @execve(i8* %55, i8** %56, i8** %57)
%59 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"file.lk\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [13 x i8] c"open file.lk\00"
@F_GETFD = global i32 0
@.str.2 = [22 x i8] c"fcntl error for fd %d\00"
@FD_CLOEXEC = global i32 0
@.str.3 = [22 x i8] c"fd %d: close_on_exec\0A\00"
@.str.4 = [13 x i8] c"hello world!\00"
@.str.5 = [23 x i8] c"open & write to fd %d\0A\00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [10 x i8] c"cloexec_i\00"
@.str.8 = [7 x i8] c"execve\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = alloca [3 x i8*]
%5 = alloca [1 x i8*]
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %8, i32 420)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* @F_GETFD
%15 = tail call i32 @fcntl(i32 %9, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %19
19: ; preds = %17, %13
%20 = load i32, i32* @FD_CLOEXEC
%21 = and i32 %20, %15
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %9)
br label %25
25: ; preds = %23, %19
%26 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i32 @write(i32 %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %9)
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%30 = call i32 @sprintf(i8* %29, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %9)
%31 = bitcast [3 x i8*]* %4 to i8*
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8** %32
%33 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 1
store i8* %29, i8** %33
%34 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 2
store i8* null, i8** %34
%35 = bitcast [1 x i8*]* %5 to i8*
%36 = bitcast [1 x i8*]* %5 to i64*
store i64 0, i64* %36
%37 = getelementptr inbounds [1 x i8*], [1 x i8*]* %5, i64 0, i64 0
%38 = call i32 @execve(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8** %32, i8** %37)
%39 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%40 = call i32 @close(i32 %9)
ret i32 0
}