#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 *, 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: %s < descriptor#", argv[0]);
  }
  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");
  }
  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     11b3 <main+0x33>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1070 <err_quit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f9 <main+0x79>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%esi
lea    0xe24(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x14(%rbp),%eax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x18(%rbp)
sub    $0x80,%eax
je     1251 <main+0xd1>
jmp    121a <main+0x9a>
mov    -0x18(%rbp),%eax
sub    $0x81,%eax
je     1262 <main+0xe2>
jmp    122d <main+0xad>
mov    -0x18(%rbp),%eax
sub    $0x82,%eax
jne    1273 <main+0xf3>
jmp    1240 <main+0xc0>
lea    0xde7(%rip),%rdi
call   1060 <printf@plt>
jmp    127f <main+0xff>
lea    0xde0(%rip),%rdi
call   1060 <printf@plt>
jmp    127f <main+0xff>
lea    0xdda(%rip),%rdi
call   1060 <printf@plt>
jmp    127f <main+0xff>
lea    0xdd4(%rip),%rdi
call   1080 <err_dump@plt>
mov    -0x14(%rbp),%eax
mov    0x2d37(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12a0 <main+0x120>
lea    0xdc7(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
mov    0x2d36(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c1 <main+0x141>
lea    0xdaf(%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     119b <main+0x1b>
mov    (%r14),%rsi
lea    0xe6a(%rip),%rdi
call   1070 <err_quit@plt>
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
mov    0x2e25(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11d3 <main+0x53>
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
lea    0xe4c(%rip),%rdi
mov    %eax,%esi
call   1040 <err_sys@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
and    %ebx,%eax
cmp    $0x80,%eax
je     1201 <main+0x81>
cmp    $0x81,%eax
je     120f <main+0x8f>
cmp    $0x82,%eax
jne    121d <main+0x9d>
lea    0xe34(%rip),%rdi
call   1060 <printf@plt>
jmp    1229 <main+0xa9>
lea    0xe30(%rip),%rdi
call   1060 <printf@plt>
jmp    1229 <main+0xa9>
lea    0xe2d(%rip),%rdi
call   1060 <printf@plt>
jmp    1229 <main+0xa9>
lea    0xe2a(%rip),%rdi
call   1080 <err_dump@plt>
mov    0x2d90(%rip),%rax
test   %ebx,(%rax)
je     1240 <main+0xc0>
lea    0xe27(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d99(%rip),%rax
test   %ebx,(%rax)
je     1257 <main+0xd7>
lea    0xe19(%rip),%rdi
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"usage: %s < 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"
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 %14
9: ; preds = %2
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%13 = call i32 @err_quit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %9, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
%19 = load i32, i32* @F_GETFL
%20 = call i32 @fcntl(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %14
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
%27 = call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %22, %14
%29 = load i32, i32* %6
%30 = load i32, i32* @O_ACCMODE
%31 = and i32 %29, %30
switch i32 %31, label %38 [
i32 130, label %32
i32 128, label %34
i32 129, label %36
]
32: ; preds = %28
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %40
34: ; preds = %28
%35 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %40
36: ; preds = %28
%37 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %40
38: ; preds = %28
%39 = call i32 @err_dump(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %38, %36, %34, %32
%41 = load i32, i32* %6
%42 = load i32, i32* @O_APPEND
%43 = and i32 %41, %42
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %45, %40
%48 = load i32, i32* %6
%49 = load i32, i32* @O_NONBLOCK
%50 = and i32 %48, %49
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %54
54: ; preds = %52, %47
%55 = call i32 @putchar(i8 signext 10)
%56 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [24 x i8] c"usage: %s < 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"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @err_quit(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %7
7: ; preds = %4, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 @fcntl(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = load i8*, i8** %8
%16 = tail call i32 @atoi(i8* %15)
%17 = tail call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14, %7
%19 = load i32, i32* @O_ACCMODE
%20 = and i32 %19, %12
switch i32 %20, label %27 [
i32 130, label %21
i32 128, label %23
i32 129, label %25
]
21: ; preds = %18
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %29
23: ; preds = %18
%24 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %29
25: ; preds = %18
%26 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %29
27: ; preds = %18
%28 = tail call i32 @err_dump(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %29
29: ; preds = %27, %25, %23, %21
%30 = load i32, i32* @O_APPEND
%31 = and i32 %30, %12
%32 = icmp eq i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i32, i32* @O_NONBLOCK
%37 = and i32 %36, %12
%38 = icmp eq i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = tail call i32 @putchar(i8 signext 10)
%43 = tail call i32 @exit(i32 0)
unreachable
}