#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*/ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ wheatSetError(char *, char *, int /*<<< orphan*/);
int wheatCloseOnExec(char *err, int fd) {
  int flags;
  if ((flags = fcntl(fd, F_GETFD, 0)) < 0) {
    wheatSetError(err, "fcntl(F_GETFL): %s", strerror(errno));
    return -1;
  }
  if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
    wheatSetError(err, "fcntl(F_SETFL, FD_CLOEXEC): %s", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<wheatCloseOnExec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1192 <wheatCloseOnExec+0x62>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xe7f(%rip),%rsi
call   1040 <wheatSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <wheatCloseOnExec+0xc3>
mov    -0x14(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e30(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11ec <wheatCloseOnExec+0xbc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe38(%rip),%rsi
call   1040 <wheatSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <wheatCloseOnExec+0xc3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wheatCloseOnExec>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1179 <wheatCloseOnExec+0x49>
mov    0x2e88(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e77(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    11a0 <wheatCloseOnExec+0x70>
lea    0xe9c(%rip),%rbx
jmp    1180 <wheatCloseOnExec+0x50>
lea    0xe80(%rip),%rbx
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <wheatSetError@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@F_GETFD = global i32 0
@.str = [19 x i8] c"fcntl(F_GETFL): %s\00"
@errno = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.1 = [31 x i8] c"fcntl(F_SETFL, FD_CLOEXEC): %s\00"
define i32 @wheatCloseOnExec(i8* %0, i32 %1) {
%3 = load i32, i32* @F_GETFD
%4 = tail call i32 @fcntl(i32 %1, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @F_SETFD
%8 = load i32, i32* @FD_CLOEXEC
%9 = or i32 %8, %4
%10 = tail call i32 @fcntl(i32 %1, i32 %7, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %6, %2
%13 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %6 ]
%14 = load i32, i32* @errno
%15 = tail call i32 @strerror(i32 %14)
%16 = tail call i32 @wheatSetError(i8* %0, i8* %13, i32 %15)
br label %17
17: ; preds = %12, %6
%18 = phi i32 [ 0, %6 ], [ -1, %12 ]
ret i32 %18
}