#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
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*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int val;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <N file descriptor>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0) {
    fprintf(stderr, "Err.(%s) fcntl() failed\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  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:
    printf("access mode unknow");
    exit(EXIT_FAILURE);
  }
  if (val & O_APPEND)
    printf(", append");
  if (val & O_NONBLOCK)
    printf(", non-blocking");
  printf("\n");
  return (EXIT_SUCCESS);
}


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)
jge    11aa <main+0x4a>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120f <main+0xaf>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe23(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%eax
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     1267 <main+0x107>
jmp    1230 <main+0xd0>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     1278 <main+0x118>
jmp    1243 <main+0xe3>
mov    -0x1c(%rbp),%eax
sub    $0x82,%eax
jne    1289 <main+0x129>
jmp    1256 <main+0xf6>
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
jmp    12a3 <main+0x143>
lea    0xdd4(%rip),%rdi
call   1040 <printf@plt>
jmp    12a3 <main+0x143>
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
jmp    12a3 <main+0x143>
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%eax
mov    0x2cf3(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c4 <main+0x164>
lea    0xdac(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    0x2d12(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12e5 <main+0x185>
lea    0xd94(%rip),%rdi
call   1040 <printf@plt>
lea    0xd31(%rip),%rdi
call   1040 <printf@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jle    120e <main+0x9e>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    0x2e36(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     1223 <main+0xb3>
mov    %eax,%ebx
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
and    %ebx,%eax
add    $0xffffff80,%eax
cmp    $0x3,%eax
jae    124d <main+0xdd>
cltq
lea    0xec5(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
call   1050 <printf@plt>
mov    0x2dce(%rip),%rax
test   %ebx,(%rax)
je     11e2 <main+0x72>
lea    0xe8e(%rip),%rdi
call   1050 <printf@plt>
mov    0x2df7(%rip),%rax
test   %ebx,(%rax)
je     11f9 <main+0x89>
lea    0xe80(%rip),%rdi
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xddf(%rip),%rsi
jmp    1246 <main+0xd6>
mov    0x2da6(%rip),%rax
mov    (%rax),%ebx
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdde(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    1259 <main+0xe9>
lea    0xe04(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Usage: %s <N file descriptor>\0A\00"
@EXIT_FAILURE = global i32 0
@F_GETFL = global i32 0
@.str.1 = [25 x i8] c"Err.(%s) fcntl() failed\0A\00"
@errno = global i32 0
@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 = [19 x i8] c"access mode unknow\00"
@O_APPEND = global i32 0
@.str.6 = [9 x i8] c", append\00"
@O_NONBLOCK = global i32 0
@.str.7 = [15 x i8] c", non-blocking\00"
@.str.8 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
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 slt i32 %7, 2
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
%22 = load i32, i32* @F_GETFL
%23 = call i32 @fcntl(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %17
%33 = load i32, i32* %6
%34 = load i32, i32* @O_ACCMODE
%35 = and i32 %33, %34
switch i32 %35, label %42 [
i32 130, label %36
i32 128, label %38
i32 129, label %40
]
36: ; preds = %32
%37 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %46
38: ; preds = %32
%39 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %46
40: ; preds = %32
%41 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %46
42: ; preds = %32
%43 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %40, %38, %36
%47 = load i32, i32* %6
%48 = load i32, i32* @O_APPEND
%49 = and i32 %47, %48
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %51, %46
%54 = load i32, i32* %6
%55 = load i32, i32* @O_NONBLOCK
%56 = and i32 %54, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %60
60: ; preds = %58, %53
%61 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%62 = load i32, i32* @EXIT_SUCCESS
ret i32 %62
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Usage: %s <N file descriptor>\0A\00"
@EXIT_FAILURE = global i32 0
@F_GETFL = global i32 0
@.str.1 = [25 x i8] c"Err.(%s) fcntl() failed\0A\00"
@errno = global i32 0
@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 = [19 x i8] c"access mode unknow\00"
@O_APPEND = global i32 0
@.str.6 = [9 x i8] c", append\00"
@O_NONBLOCK = global i32 0
@.str.7 = [15 x i8] c", non-blocking\00"
@EXIT_SUCCESS = global i32 0
@reltable.main = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([11 x i8]* @.str.3 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([11 x i8]* @.str.4 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([10 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32)]
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = load i32, i32* @F_GETFL
%15 = tail call i32 @fcntl(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %10
%25 = load i32, i32* @O_ACCMODE
%26 = and i32 %25, %15
%27 = add i32 %26, -128
%28 = icmp ult i32 %27, 3
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = sext i32 %27 to i64
%35 = shl i64 %34, 2
%36 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %35)
%37 = tail call i32 @printf(i8* dereferenceable(1) %36)
%38 = load i32, i32* @O_APPEND
%39 = and i32 %38, %15
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %33
%42 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %33
%44 = load i32, i32* @O_NONBLOCK
%45 = and i32 %44, %15
%46 = icmp eq i32 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %43
%48 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = tail call i32 @putchar(i32 10)
%51 = load i32, i32* @EXIT_SUCCESS
ret i32 %51
}