#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 /*<<< orphan*/ abort();
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
void set_cloexec(int fd, int flag) {
  int flags = fcntl(fd, F_GETFD);
  if (flags < 0) {
    fprintf(stderr, "[vagga] Error getting flags: %m\n");
    abort();
  }
  int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
  if (rc < 0) {
    fprintf(stderr, "[vagga] Error setting flags: %m\n");
    abort();
  }
}


x86 assembly (clang, -O0):
<set_cloexec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    117a <set_cloexec+0x4a>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2e40(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11c2 <set_cloexec+0x92>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_cloexec>:
push   %rbx
mov    %edi,%ebx
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     116a <set_cloexec+0x3a>
mov    0x2e8a(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e79(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     117c <set_cloexec+0x4c>
pop    %rbx
ret
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
jmp    118c <set_cloexec+0x5c>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
lea    0xe95(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@F_GETFD = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"[vagga] Error getting flags: %m\0A\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.1 = [33 x i8] c"[vagga] Error setting flags: %m\0A\00"
define void @set_cloexec(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* @F_GETFD
%9 = call i32 (i32, i32, ...) @fcntl(i32 %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (...) @abort()
unreachable
16: ; preds = %2
%17 = load i32, i32* %3
%18 = load i32, i32* @F_SETFD
%19 = load i32, i32* %5
%20 = load i32, i32* @FD_CLOEXEC
%21 = or i32 %19, %20
%22 = call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %18, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 (...) @abort()
unreachable
29: ; preds = %16
ret void
}

LLVM IR (-O2):
@F_GETFD = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"[vagga] Error getting flags: %m\0A\00"
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.1 = [33 x i8] c"[vagga] Error setting flags: %m\0A\00"
define void @set_cloexec(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFD
%4 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 (...) @abort()
unreachable
10: ; preds = %2
%11 = load i32, i32* @F_SETFD
%12 = load i32, i32* @FD_CLOEXEC
%13 = or i32 %12, %4
%14 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %11, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 (...) @abort()
unreachable
20: ; preds = %10
ret void
}