#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 O_ACCMODE;
int O_APPEND;
int O_NONBLOCK;
#define O_RDONLY 130
#define O_RDWR 129
#define O_WRONLY 128
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ err_dump(char *);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ err_sys(char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int main(int argc, char *argv[]) {
  int val;
  if (argc != 2)
    err_quit("usage:print_fd <descriptor#>");
  if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0)
    err_sys("fcntl error for fd %d", atoi(argv[1]));
  switch (val & O_ACCMODE) {
  case O_RDONLY:
    printf("read only");
    break;
  case O_WRONLY:
    printf("write only");
    break;
  case O_RDWR:
    printf("read write");
    break;
  default:
    err_dump("unknown access mode");
  }
  if (val & O_APPEND)
    printf(", append");
  if (val & O_NONBLOCK)
    printf(", nonblocking");
  if ((val = fcntl(atoi(argv[1]), F_GETFD, 0)) < 0)
    err_sys("fcntl error for fd %d", atoi(argv[1]));
  if (val & FD_CLOEXEC)
    printf(", close_on_exec");
  putchar('\n');
  exit(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)
cmpl   $0x2,-0x8(%rbp)
je     11ac <main+0x2c>
lea    0xe59(%rip),%rdi
call   1070 <err_quit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f2 <main+0x72>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%esi
lea    0xe30(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x14(%rbp),%eax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x18(%rbp)
sub    $0x80,%eax
je     124a <main+0xca>
jmp    1213 <main+0x93>
mov    -0x18(%rbp),%eax
sub    $0x81,%eax
je     125b <main+0xdb>
jmp    1226 <main+0xa6>
mov    -0x18(%rbp),%eax
sub    $0x82,%eax
jne    126c <main+0xec>
jmp    1239 <main+0xb9>
lea    0xdf3(%rip),%rdi
call   1060 <printf@plt>
jmp    1278 <main+0xf8>
lea    0xdec(%rip),%rdi
call   1060 <printf@plt>
jmp    1278 <main+0xf8>
lea    0xde6(%rip),%rdi
call   1060 <printf@plt>
jmp    1278 <main+0xf8>
lea    0xde0(%rip),%rdi
call   1080 <err_dump@plt>
mov    -0x14(%rbp),%eax
mov    0x2d2e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1299 <main+0x119>
lea    0xdd3(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
mov    0x2d3d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12ba <main+0x13a>
lea    0xdbb(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1300 <main+0x180>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%esi
lea    0xd22(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x14(%rbp),%eax
mov    0x2cc6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1321 <main+0x1a1>
lea    0xd62(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
je     1197 <main+0x17>
lea    0xe6e(%rip),%rdi
call   1070 <err_quit@plt>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    0x2e19(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11cf <main+0x4f>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
lea    0xe55(%rip),%rdi
mov    %eax,%esi
call   1040 <err_sys@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
and    %ebp,%eax
cmp    $0x80,%eax
je     11fd <main+0x7d>
cmp    $0x81,%eax
je     120b <main+0x8b>
cmp    $0x82,%eax
jne    1219 <main+0x99>
lea    0xe3d(%rip),%rdi
call   1060 <printf@plt>
jmp    1225 <main+0xa5>
lea    0xe39(%rip),%rdi
call   1060 <printf@plt>
jmp    1225 <main+0xa5>
lea    0xe36(%rip),%rdi
call   1060 <printf@plt>
jmp    1225 <main+0xa5>
lea    0xe33(%rip),%rdi
call   1080 <err_dump@plt>
mov    0x2d84(%rip),%rax
test   %ebp,(%rax)
je     123c <main+0xbc>
lea    0xe30(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d9d(%rip),%rax
test   %ebp,(%rax)
je     1253 <main+0xd3>
lea    0xe22(%rip),%rdi
call   1060 <printf@plt>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    0x2d65(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    128b <main+0x10b>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
lea    0xd99(%rip),%rdi
mov    %eax,%esi
call   1040 <err_sys@plt>
mov    0x2d3e(%rip),%rax
test   %ebp,(%rax)
je     12a2 <main+0x122>
lea    0xde1(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"usage:print_fd <descriptor#>\00"
@F_GETFL = global i32 0
@.str.1 = [22 x i8] c"fcntl error for fd %d\00"
@O_ACCMODE = global i32 0
@.str.2 = [10 x i8] c"read only\00"
@.str.3 = [11 x i8] c"write only\00"
@.str.4 = [11 x i8] c"read write\00"
@.str.5 = [20 x i8] c"unknown access mode\00"
@O_APPEND = global i32 0
@.str.6 = [9 x i8] c", append\00"
@O_NONBLOCK = global i32 0
@.str.7 = [14 x i8] c", nonblocking\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.8 = [16 x i8] c", close_on_exec\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 = load i32, i32* %4
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @err_quit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atoi(i8* %14)
%16 = load i32, i32* @F_GETFL
%17 = call i32 @fcntl(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %11
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %19, %11
%26 = load i32, i32* %6
%27 = load i32, i32* @O_ACCMODE
%28 = and i32 %26, %27
switch i32 %28, label %35 [
i32 130, label %29
i32 128, label %31
i32 129, label %33
]
29: ; preds = %25
%30 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %37
31: ; preds = %25
%32 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %37
33: ; preds = %25
%34 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %37
35: ; preds = %25
%36 = call i32 @err_dump(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %33, %31, %29
%38 = load i32, i32* %6
%39 = load i32, i32* @O_APPEND
%40 = and i32 %38, %39
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
%45 = load i32, i32* %6
%46 = load i32, i32* @O_NONBLOCK
%47 = and i32 %45, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %51
51: ; preds = %49, %44
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 1
%54 = load i8*, i8** %53
%55 = call i32 @atoi(i8* %54)
%56 = load i32, i32* @F_GETFD
%57 = call i32 @fcntl(i32 %55, i32 %56, i32 0)
store i32 %57, i32* %6
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %51
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 1
%62 = load i8*, i8** %61
%63 = call i32 @atoi(i8* %62)
%64 = call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %59, %51
%66 = load i32, i32* %6
%67 = load i32, i32* @FD_CLOEXEC
%68 = and i32 %66, %67
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
br label %72
72: ; preds = %70, %65
%73 = call i32 @putchar(i8 signext 10)
%74 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [29 x i8] c"usage:print_fd <descriptor#>\00"
@F_GETFL = global i32 0
@.str.1 = [22 x i8] c"fcntl error for fd %d\00"
@O_ACCMODE = global i32 0
@.str.2 = [10 x i8] c"read only\00"
@.str.3 = [11 x i8] c"write only\00"
@.str.4 = [11 x i8] c"read write\00"
@.str.5 = [20 x i8] c"unknown access mode\00"
@O_APPEND = global i32 0
@.str.6 = [9 x i8] c", append\00"
@O_NONBLOCK = global i32 0
@.str.7 = [14 x i8] c", nonblocking\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.8 = [16 x i8] c", close_on_exec\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @err_quit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = load i32, i32* @F_GETFL
%11 = tail call i32 @fcntl(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = load i8*, i8** %7
%15 = tail call i32 @atoi(i8* %14)
%16 = tail call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %13, %6
%18 = load i32, i32* @O_ACCMODE
%19 = and i32 %18, %11
switch i32 %19, label %26 [
i32 130, label %20
i32 128, label %22
i32 129, label %24
]
20: ; preds = %17
%21 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %28
22: ; preds = %17
%23 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %28
24: ; preds = %17
%25 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %28
26: ; preds = %17
%27 = tail call i32 @err_dump(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %26, %24, %22, %20
%29 = load i32, i32* @O_APPEND
%30 = and i32 %29, %11
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = load i32, i32* @O_NONBLOCK
%36 = and i32 %35, %11
%37 = icmp eq i32 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = load i8*, i8** %7
%42 = tail call i32 @atoi(i8* %41)
%43 = load i32, i32* @F_GETFD
%44 = tail call i32 @fcntl(i32 %42, i32 %43, i32 0)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = load i8*, i8** %7
%48 = tail call i32 @atoi(i8* %47)
%49 = tail call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %46, %40
%51 = load i32, i32* @FD_CLOEXEC
%52 = and i32 %51, %44
%53 = icmp eq i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %50
%55 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
br label %56
56: ; preds = %54, %50
%57 = tail call i32 @putchar(i8 signext 10)
%58 = tail call i32 @exit(i32 0)
unreachable
}