#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_msg_and_die(char *, int);
void set_cloexec_flag(int fd) {
  int flags, newflags;
  flags = fcntl(fd, F_GETFD);
  if (flags < 0) {
    perror_msg_and_die("fcntl(%d, F_GETFD)", fd);
  }
  newflags = flags | FD_CLOEXEC;
  if (flags == newflags)
    return;
  fcntl(fd, F_SETFD, newflags);
}


x86 assembly (clang, -O0):
<set_cloexec_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    115a <set_cloexec_flag+0x3a>
mov    -0x4(%rbp),%esi
lea    0xeab(%rip),%rdi
call   1040 <perror_msg_and_die@plt>
mov    -0x8(%rbp),%eax
mov    0x2e74(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    117a <set_cloexec_flag+0x5a>
jmp    1190 <set_cloexec_flag+0x70>
mov    -0x4(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cloexec_flag>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1149 <set_cloexec_flag+0x29>
lea    0xebe(%rip),%rdi
mov    %ebx,%esi
call   1040 <perror_msg_and_die@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
or     %ebp,%edx
cmp    %edx,%ebp
jne    115f <set_cloexec_flag+0x3f>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <fcntl@plt>

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

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