#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*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
scalar_t__ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int unused_argc, char **unused_argv) {
  int res;
  printf("Setting the close-on-exec flag of file-descriptor 0.\n");
  if (fcntl(0, F_SETFD, 1) < 0) {
    perror("fcntl(0, F_SETFD, 1)");
    exit(1);
  };
  printf("Duplicating file-descriptor 0 to 3.\n");
  if (dup2(0, 3) < 0) {
    perror("dup2(0, 3)");
    exit(1);
  };
  printf("Testing if the close-on-exec flag of file-descriptor 3 is set.\n");
  if ((res = fcntl(3, F_GETFD, 0)) < 0) {
    perror("(res = fcntl(3, F_GETFD, 0))");
    exit(1);
  };
  if (res & 1)
    printf(
        "Yes, a newly dup2()ed file-descriptor has the close-on-exec flag "
        "cloned.\nTHIS VIOLATES Posix1003.1 section 6.2.1.2 or 6.5.2.2!\nYou "
        "should #define DUP2_DUPS_CLOSE_ON_EXEC in sys_defs.h for your OS.\n");
  else
    printf("No, a newly dup2()ed file-descriptor has the close-on-exec flag "
           "cleared.\nThis complies with Posix1003.1 section 6.2.1.2 and "
           "6.5.2.2!\n");
  return 0;
}


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)
lea    0xe93(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11a6 <main+0x56>
lea    0xe9f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe9e(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
mov    $0x3,%esi
call   1040 <dup2@plt>
cmp    $0x0,%rax
jge    11de <main+0x8e>
lea    0xea1(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe96(%rip),%rdi
call   1050 <printf@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    $0x3,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1221 <main+0xd1>
lea    0xea9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1241 <main+0xf1>
lea    0xea1(%rip),%rdi
call   1050 <printf@plt>
jmp    124d <main+0xfd>
lea    0xf56(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xee5(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     11d5 <main+0x85>
lea    0xef5(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
mov    $0x3,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     11de <main+0x8e>
lea    0xefc(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    $0x3,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     11e7 <main+0x97>
test   $0x1,%al
lea    0xf14(%rip),%rax
lea    0xf92(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe24(%rip),%rdi
jmp    11ee <main+0x9e>
lea    0xe30(%rip),%rdi
jmp    11ee <main+0x9e>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [54 x i8] c"Setting the close-on-exec flag of file-descriptor 0.\0A\00"
@F_SETFD = global i32 0
@.str.1 = [21 x i8] c"fcntl(0, F_SETFD, 1)\00"
@.str.2 = [37 x i8] c"Duplicating file-descriptor 0 to 3.\0A\00"
@.str.3 = [11 x i8] c"dup2(0, 3)\00"
@.str.4 = [64 x i8] c"Testing if the close-on-exec flag of file-descriptor 3 is set.\0A\00"
@F_GETFD = global i32 0
@.str.5 = [29 x i8] c"(res = fcntl(3, F_GETFD, 0))\00"
@.str.6 = [198 x i8] c"Yes, a newly dup2()ed file-descriptor has the close-on-exec flag cloned.\0ATHIS VIOLATES Posix1003.1 section 6.2.1.2 or 6.5.2.2!\0AYou should #define DUP2_DUPS_CLOSE_ON_EXEC in sys_defs.h for your OS.\0A\00"
@.str.7 = [134 x i8] c"No, a newly dup2()ed file-descriptor has the close-on-exec flag cleared.\0AThis complies with Posix1003.1 section 6.2.1.2 and 6.5.2.2!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @F_SETFD
%9 = call i32 @fcntl(i32 0, i32 %8, i32 1)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
%16 = call i64 @dup2(i32 0, i32 3)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = call i32 @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0))
%23 = load i32, i32* @F_GETFD
%24 = call i32 @fcntl(i32 3, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = load i32, i32* %6
%31 = and i32 %30, 1
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([198 x i8], [198 x i8]* @.str.6, i64 0, i64 0))
br label %37
35: ; preds = %29
%36 = call i32 @printf(i8* getelementptr inbounds ([134 x i8], [134 x i8]* @.str.7, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
ret i32 0
}

LLVM IR (-O2):
@F_SETFD = global i32 0
@.str.1 = [21 x i8] c"fcntl(0, F_SETFD, 1)\00"
@.str.3 = [11 x i8] c"dup2(0, 3)\00"
@F_GETFD = global i32 0
@.str.5 = [29 x i8] c"(res = fcntl(3, F_GETFD, 0))\00"
@str = [53 x i8] c"Setting the close-on-exec flag of file-descriptor 0.\00"
@str.8 = [36 x i8] c"Duplicating file-descriptor 0 to 3.\00"
@str.9 = [63 x i8] c"Testing if the close-on-exec flag of file-descriptor 3 is set.\00"
@str.10 = [133 x i8] c"No, a newly dup2()ed file-descriptor has the close-on-exec flag cleared.\0AThis complies with Posix1003.1 section 6.2.1.2 and 6.5.2.2!\00"
@str.11 = [197 x i8] c"Yes, a newly dup2()ed file-descriptor has the close-on-exec flag cloned.\0ATHIS VIOLATES Posix1003.1 section 6.2.1.2 or 6.5.2.2!\0AYou should #define DUP2_DUPS_CLOSE_ON_EXEC in sys_defs.h for your OS.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @F_SETFD
%5 = tail call i32 @fcntl(i32 0, i32 %4, i32 1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.8, i64 0, i64 0))
%12 = tail call i64 @dup2(i32 0, i32 3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @str.9, i64 0, i64 0))
%19 = load i32, i32* @F_GETFD
%20 = tail call i32 @fcntl(i32 3, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = and i32 %20, 1
%27 = icmp eq i32 %26, 0
%28 = select i1 %27, i8* getelementptr inbounds ([133 x i8], [133 x i8]* @str.10, i64 0, i64 0), i8* getelementptr inbounds ([197 x i8], [197 x i8]* @str.11, i64 0, i64 0)
%29 = tail call i32 @puts(i8* dereferenceable(1) %28)
ret i32 0
}