#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;
int O_RDONLY;
int O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ err_dump(char *);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ err_sys(char *, 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 accmode, val;
  if (argc != 2) {
    err_quit("Usage: eg0303 <descriptor#>");
  }
  if ((val = fcntl(atoi(*++argv), F_GETFL, 0)) < 0) {
    err_sys("fcntl error for fd %d\n", atoi(*argv));
  }
  accmode = val & O_ACCMODE;
  if (accmode == O_RDONLY) {
    printf("read only");
  } else if (accmode == O_WRONLY) {
    printf("write only");
  } else if (accmode == O_RDWR) {
    printf("read write");
  } else {
    err_dump("unknown access mode");
  }
  if (val & O_APPEND) {
    printf(", append");
  }
  if (val & O_NONBLOCK) {
    printf(", nonblocking");
  }
  putchar('\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)
cmpl   $0x2,-0x8(%rbp)
je     119c <main+0x2c>
lea    0xe69(%rip),%rdi
call   1070 <err_quit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11ec <main+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%esi
lea    0xe35(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x18(%rbp),%eax
mov    0x2dfa(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%eax
jne    121e <main+0xae>
lea    0xe1f(%rip),%rdi
call   1060 <printf@plt>
jmp    127a <main+0x10a>
mov    -0x14(%rbp),%eax
mov    0x2d88(%rip),%rcx
cmp    (%rcx),%eax
jne    1241 <main+0xd1>
lea    0xe06(%rip),%rdi
call   1060 <printf@plt>
jmp    1275 <main+0x105>
mov    -0x14(%rbp),%eax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%eax
jne    1264 <main+0xf4>
lea    0xdee(%rip),%rdi
call   1060 <printf@plt>
jmp    1270 <main+0x100>
lea    0xde8(%rip),%rdi
call   1080 <err_dump@plt>
jmp    1275 <main+0x105>
jmp    127a <main+0x10a>
mov    -0x18(%rbp),%eax
mov    0x2d24(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     129b <main+0x12b>
lea    0xdd1(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2d33(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12bc <main+0x14c>
lea    0xdb9(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
je     1188 <main+0x18>
lea    0xe7d(%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    11c0 <main+0x50>
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
lea    0xe63(%rip),%rdi
mov    %eax,%esi
call   1040 <err_sys@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
and    %ebx,%eax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%eax
jne    11e4 <main+0x74>
lea    0xe56(%rip),%rdi
call   1060 <printf@plt>
jmp    1222 <main+0xb2>
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%eax
jne    11fd <main+0x8d>
lea    0xe47(%rip),%rdi
call   1060 <printf@plt>
jmp    1222 <main+0xb2>
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%eax
jne    1216 <main+0xa6>
lea    0xe39(%rip),%rdi
call   1060 <printf@plt>
jmp    1222 <main+0xb2>
lea    0xe36(%rip),%rdi
call   1080 <err_dump@plt>
mov    0x2d7f(%rip),%rax
test   %ebx,(%rax)
je     1239 <main+0xc9>
lea    0xe33(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d98(%rip),%rax
test   %ebx,(%rax)
je     1250 <main+0xe0>
lea    0xe25(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Usage: eg0303 <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
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"read only\00"
@O_WRONLY = global i32 0
@.str.3 = [11 x i8] c"write only\00"
@O_RDWR = global i32 0
@.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
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @err_quit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i32 1
store i8** %14, i8*** %5
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
%17 = load i32, i32* @F_GETFL
%18 = call i32 @fcntl(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %12
%21 = load i8**, i8*** %5
%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 = %20, %12
%26 = load i32, i32* %7
%27 = load i32, i32* @O_ACCMODE
%28 = and i32 %26, %27
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = load i32, i32* @O_RDONLY
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %50
34: ; preds = %25
%35 = load i32, i32* %6
%36 = load i32, i32* @O_WRONLY
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %49
40: ; preds = %34
%41 = load i32, i32* %6
%42 = load i32, i32* @O_RDWR
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %48
46: ; preds = %40
%47 = call i32 @err_dump(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %44
br label %49
49: ; preds = %48, %38
br label %50
50: ; preds = %49, %32
%51 = load i32, i32* %7
%52 = load i32, i32* @O_APPEND
%53 = and i32 %51, %52
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %57
57: ; preds = %55, %50
%58 = load i32, i32* %7
%59 = load i32, i32* @O_NONBLOCK
%60 = and i32 %58, %59
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %64
64: ; preds = %62, %57
%65 = call i32 @putchar(i8 signext 10)
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"Usage: eg0303 <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
@O_RDONLY = global i32 0
@.str.2 = [10 x i8] c"read only\00"
@O_WRONLY = global i32 0
@.str.3 = [11 x i8] c"write only\00"
@O_RDWR = global i32 0
@.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 ([28 x i8], [28 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
%20 = load i32, i32* @O_RDONLY
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %36
24: ; preds = %17
%25 = load i32, i32* @O_WRONLY
%26 = icmp eq i32 %19, %25
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %36
29: ; preds = %24
%30 = load i32, i32* @O_RDWR
%31 = icmp eq i32 %19, %30
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %36
34: ; preds = %29
%35 = tail call i32 @err_dump(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %27, %34, %32, %22
%37 = load i32, i32* @O_APPEND
%38 = and i32 %37, %11
%39 = icmp eq i32 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %36
%41 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = load i32, i32* @O_NONBLOCK
%44 = and i32 %43, %11
%45 = icmp eq i32 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %42
%47 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
%49 = tail call i32 @putchar(i8 signext 10)
ret i32 0
}