#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 /*<<< orphan*/ F_SETFD;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int close_on_exec(int fd) {
  int flags = fcntl(fd, F_GETFD);
  if (flags == -1) {
    perror("fcntl/F_GETFD");
    return -1;
  }
  flags |= FD_CLOEXEC;
  if (fcntl(fd, F_SETFD, flags) == -1) {
    perror("fcntl/F_SETFD");
    return -1;
  }
  return 1;
}


x86 assembly (clang, -O0):
<close_on_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1163 <close_on_exec+0x43>
lea    0xeae(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <close_on_exec+0x90>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11a9 <close_on_exec+0x89>
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <close_on_exec+0x90>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_on_exec>:
push   %rbx
mov    %edi,%ebx
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1163 <close_on_exec+0x43>
mov    0x2e99(%rip),%rcx
or     (%rcx),%eax
mov    0x2e98(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0xffffffff,%ecx
je     116c <close_on_exec+0x4c>
pop    %rbx
ret
lea    0xe96(%rip),%rdi
jmp    1173 <close_on_exec+0x53>
lea    0xe9b(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@F_GETFD = global i32 0
@.str = [14 x i8] c"fcntl/F_GETFD\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.1 = [14 x i8] c"fcntl/F_SETFD\00"
define i32 @close_on_exec(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @F_GETFD
%7 = call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
12: ; preds = %1
%13 = load i32, i32* @FD_CLOEXEC
%14 = load i32, i32* %4
%15 = or i32 %14, %13
store i32 %15, i32* %4
%16 = load i32, i32* %3
%17 = load i32, i32* @F_SETFD
%18 = load i32, i32* %4
%19 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %24
23: ; preds = %12
store i32 1, i32* %2
br label %24
24: ; preds = %23, %21, %10
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@.str = [14 x i8] c"fcntl/F_GETFD\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.1 = [14 x i8] c"fcntl/F_SETFD\00"
define i32 @close_on_exec(i32 %0) {
%2 = load i32, i32* @F_GETFD
%3 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %11, label %5
5: ; preds = %1
%6 = load i32, i32* @FD_CLOEXEC
%7 = or i32 %6, %3
%8 = load i32, i32* @F_SETFD
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %8, i32 %7)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %5, %1
%12 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %5 ]
%13 = tail call i32 @perror(i8* %12)
br label %14
14: ; preds = %11, %5
%15 = phi i32 [ 1, %5 ], [ -1, %11 ]
ret i32 %15
}