#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_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ log_fatal(char *);
void drs_setNonBlockingCloExecFlags(int fd) {
  int fdFlags;
  if ((fdFlags = fcntl(fd, F_GETFL)) == -1)
    log_fatal("fcntl(F_GETFL)");
  if (fcntl(fd, F_SETFL, fdFlags | O_NONBLOCK) < 0)
    log_fatal("fcntl(F_SETFL)");
  if ((fdFlags = fcntl(fd, F_GETFD)) == -1)
    log_fatal("fcntl(F_GETFD)");
  if (fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC) < 0)
    log_fatal("fcntl(F_SETFD)");
}


x86 assembly (clang, -O0):
<drs_setNonBlockingCloExecFlags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1156 <drs_setNonBlockingCloExecFlags+0x36>
lea    0xeaf(%rip),%rdi
call   1040 <log_fatal@plt>
mov    -0x4(%rbp),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e7c(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    118a <drs_setNonBlockingCloExecFlags+0x6a>
lea    0xe8a(%rip),%rdi
call   1040 <log_fatal@plt>
mov    -0x4(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11b5 <drs_setNonBlockingCloExecFlags+0x95>
lea    0xe6e(%rip),%rdi
call   1040 <log_fatal@plt>
mov    -0x4(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2dfd(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11e9 <drs_setNonBlockingCloExecFlags+0xc9>
lea    0xe49(%rip),%rdi
call   1040 <log_fatal@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drs_setNonBlockingCloExecFlags>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1148 <drs_setNonBlockingCloExecFlags+0x28>
lea    0xebd(%rip),%rdi
call   1040 <log_fatal@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
or     %ebp,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
jns    1175 <drs_setNonBlockingCloExecFlags+0x55>
lea    0xe9f(%rip),%rdi
call   1040 <log_fatal@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    119a <drs_setNonBlockingCloExecFlags+0x7a>
lea    0xe89(%rip),%rdi
call   1040 <log_fatal@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
or     %ebp,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     11c2 <drs_setNonBlockingCloExecFlags+0xa2>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe64(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <log_fatal@plt>

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

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