#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main(int argc, char *argv[]) {
  int fd;
  int flags;
  if ((fd = open("testfile", O_RDWR)) == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  if ((flags = fcntl(fd, F_GETFL)) == -1) {
    perror("fcntl get flags");
    exit(EXIT_FAILURE);
  }
  flags |= FD_CLOEXEC;
  if (fcntl(fd, F_SETFL, flags) == -1) {
    perror("fcntl set flags");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1191 <main+0x51>
lea    0xe8b(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11ca <main+0x8a>
lea    0xe57(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1212 <main+0xd2>
lea    0xe1f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    1164 <main+0x24>
lea    0xea7(%rip),%rdi
jmp    11af <main+0x6f>
mov    %eax,%ebx
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1186 <main+0x46>
lea    0xe8a(%rip),%rdi
jmp    11af <main+0x6f>
mov    0x2e43(%rip),%rcx
or     (%rcx),%eax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11c2 <main+0x82>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"testfile\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@F_GETFL = global i32 0
@.str.2 = [16 x i8] c"fcntl get flags\00"
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
@.str.3 = [16 x i8] c"fcntl set flags\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* @F_GETFL
%18 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %17)
store i32 %18, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = load i32, i32* @FD_CLOEXEC
%26 = load i32, i32* %7
%27 = or i32 %26, %25
store i32 %27, i32* %7
%28 = load i32, i32* %6
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* %7
%31 = call i32 (i32, i32, ...) @fcntl(i32 %28, i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %24
%34 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %24
%38 = load i32, i32* @EXIT_SUCCESS
%39 = call i32 @exit(i32 %38)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"testfile\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@F_GETFL = global i32 0
@.str.2 = [16 x i8] c"fcntl get flags\00"
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
@.str.3 = [16 x i8] c"fcntl set flags\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %4, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
%19 = load i32, i32* @FD_CLOEXEC
%20 = or i32 %19, %12
%21 = load i32, i32* @F_SETFL
%22 = tail call i32 (i32, i32, ...) @fcntl(i32 %4, i32 %21, i32 %20)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = load i32, i32* @EXIT_SUCCESS
%30 = tail call i32 @exit(i32 %29)
unreachable
}