#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_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: a.out <descriptor#>");
  if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0)
    err_sys("fcntl error for fd %d\n", 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");
  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    0x2e0e(%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    0xe2e(%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    0xdf2(%rip),%rdi
call   1060 <printf@plt>
jmp    1278 <main+0xf8>
lea    0xdeb(%rip),%rdi
call   1060 <printf@plt>
jmp    1278 <main+0xf8>
lea    0xde5(%rip),%rdi
call   1060 <printf@plt>
jmp    1278 <main+0xf8>
lea    0xddf(%rip),%rdi
call   1080 <err_dump@plt>
mov    -0x14(%rbp),%eax
mov    0x2d3e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1299 <main+0x119>
lea    0xdd2(%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    0xdba(%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   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
je     1198 <main+0x18>
lea    0xe6d(%rip),%rdi
call   1070 <err_quit@plt>
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
mov    0x2e28(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11d0 <main+0x50>
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
lea    0xe52(%rip),%rdi
mov    %eax,%esi
call   1040 <err_sys@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
and    %ebx,%eax
cmp    $0x80,%eax
je     11fe <main+0x7e>
cmp    $0x81,%eax
je     120c <main+0x8c>
cmp    $0x82,%eax
jne    121a <main+0x9a>
lea    0xe3b(%rip),%rdi
call   1060 <printf@plt>
jmp    1226 <main+0xa6>
lea    0xe37(%rip),%rdi
call   1060 <printf@plt>
jmp    1226 <main+0xa6>
lea    0xe34(%rip),%rdi
call   1060 <printf@plt>
jmp    1226 <main+0xa6>
lea    0xe31(%rip),%rdi
call   1080 <err_dump@plt>
mov    0x2d93(%rip),%rax
test   %ebx,(%rax)
je     123d <main+0xbd>
lea    0xe2e(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d9c(%rip),%rax
test   %ebx,(%rax)
je     1254 <main+0xd4>
lea    0xe20(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Usage: a.out <descriptor#>\00"
@F_GETFL = global i32 0
@.str.1 = [23 x i8] c"fcntl error for fd %d\0A\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"
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 ([27 x i8], [27 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 ([23 x i8], [23 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 = call i32 @putchar(i8 signext 10)
%53 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [27 x i8] c"Usage: a.out <descriptor#>\00"
@F_GETFL = global i32 0
@.str.1 = [23 x i8] c"fcntl error for fd %d\0A\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"
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 ([27 x i8], [27 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 ([23 x i8], [23 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 = tail call i32 @putchar(i8 signext 10)
%42 = tail call i32 @exit(i32 0)
unreachable
}