#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 dup2(int, int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int, int);
int open(char *, int);
main() {
  int fd1, fd2, fl;
  fd1 = open("/dev/null", 2);
  if (fcntl(fd1, 2, 1) < 0)
    exit(1);
  fd2 = dup2(fd1, 1);
  if (fd2 < 0)
    exit(2);
  fl = fcntl(fd2, 1, 0);
  exit(fl != 1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x2,%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    1188 <main+0x48>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ac <main+0x6c>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xeb8(%rip),%rdi
mov    $0x2,%esi
call   1050 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x2,%esi
mov    $0x1,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    1173 <main+0x33>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
test   %eax,%eax
jns    118d <main+0x4d>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    %eax,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
xor    %edi,%edi
cmp    $0x1,%eax
setne  %dil
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 2)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @fcntl(i32 %6, i32 2, i32 1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 @dup2(i32 %12, i32 1)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @exit(i32 2)
unreachable
18: ; preds = %11
%19 = load i32, i32* %3
%20 = call i32 @fcntl(i32 %19, i32 1, i32 0)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 1
%23 = zext i1 %22 to i32
%24 = call i32 @exit(i32 %23)
unreachable
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
define i32 @main() {
%1 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 2)
%2 = tail call i32 @fcntl(i32 %1, i32 2, i32 1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 @dup2(i32 %1, i32 1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %6
%12 = tail call i32 @fcntl(i32 %7, i32 1, i32 0)
%13 = icmp ne i32 %12, 1
%14 = zext i1 %13 to i32
%15 = tail call i32 @exit(i32 %14)
unreachable
}