#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 EXIT_FAILURE;
int /*<<< orphan*/ F_GETFL;
int O_ACCMODE;
int O_APPEND;
int O_NONBLOCK;
#define O_RDONLY 130
#define O_RDWR 129
int O_SYNC;
#define O_WRONLY 128
int atoi(char *);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fl = 0;
  int fd = -1;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <fd>\n", argv[0]);
    return EXIT_FAILURE;
  }
  fd = atoi(argv[1]);
  fl = fcntl(fd, F_GETFL, 0);
  if (fl < 0) {
    strerror(errno);
    return EXIT_FAILURE;
  }
  switch (fl & 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("unknown access mode");
  }
  if (fl & O_APPEND) {
    printf(", append");
  }
  if (fl & O_NONBLOCK) {
    printf(", nonblocking");
  }
  if (fl & O_SYNC) {
    printf(", synchronous writes");
  }
  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)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bb <main+0x5b>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe5b(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1304 <main+0x1a4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120a <main+0xaa>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1304 <main+0x1a4>
mov    -0x14(%rbp),%eax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     1262 <main+0x102>
jmp    122b <main+0xcb>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     1273 <main+0x113>
jmp    123e <main+0xde>
mov    -0x1c(%rbp),%eax
sub    $0x82,%eax
jne    1284 <main+0x124>
jmp    1251 <main+0xf1>
lea    0xdb8(%rip),%rdi
call   1050 <printf@plt>
jmp    1290 <main+0x130>
lea    0xdb1(%rip),%rdi
call   1050 <printf@plt>
jmp    1290 <main+0x130>
lea    0xdab(%rip),%rdi
call   1050 <printf@plt>
jmp    1290 <main+0x130>
lea    0xda7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    0x2d06(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12b1 <main+0x151>
lea    0xd9a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    0x2d1d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12d2 <main+0x172>
lea    0xd82(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    0x2d04(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12f3 <main+0x193>
lea    0xd6f(%rip),%rdi
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    11ac <main+0x4c>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     11c6 <main+0x66>
mov    %eax,%ebx
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
and    %ebx,%eax
add    $0xffffff80,%eax
cmp    $0x2,%eax
ja     11df <main+0x7f>
cltq
lea    0xed1(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    11e6 <main+0x86>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe41(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11d4 <main+0x74>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
lea    0xe4c(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dae(%rip),%rax
test   %ebx,(%rax)
je     1202 <main+0xa2>
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dcf(%rip),%rax
test   %ebx,(%rax)
je     1219 <main+0xb9>
lea    0xe3b(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dc0(%rip),%rax
test   %ebx,(%rax)
je     1230 <main+0xd0>
lea    0xe32(%rip),%rdi
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"Usage: %s <fd>\0A\00"
@EXIT_FAILURE = global i32 0
@F_GETFL = global i32 0
@errno = global i32 0
@O_ACCMODE = global i32 0
@.str.1 = [10 x i8] c"read only\00"
@.str.2 = [11 x i8] c"write only\00"
@.str.3 = [13 x i8] c"read & write\00"
@.str.4 = [20 x i8] c"unknown access mode\00"
@O_APPEND = global i32 0
@.str.5 = [9 x i8] c", append\00"
@O_NONBLOCK = global i32 0
@.str.6 = [14 x i8] c", nonblocking\00"
@O_SYNC = global i32 0
@.str.7 = [21 x i8] c", synchronous writes\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
store i32 0, i32* %6
store i32 -1, i32* %7
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %3
br label %66
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)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = load i32, i32* @F_GETFL
%24 = call i32 @fcntl(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %17
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = load i32, i32* @EXIT_FAILURE
store i32 %30, i32* %3
br label %66
31: ; preds = %17
%32 = load i32, i32* %6
%33 = load i32, i32* @O_ACCMODE
%34 = and i32 %32, %33
switch i32 %34, label %41 [
i32 130, label %35
i32 128, label %37
i32 129, label %39
]
35: ; preds = %31
%36 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %43
37: ; preds = %31
%38 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %43
39: ; preds = %31
%40 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %43
41: ; preds = %31
%42 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %39, %37, %35
%44 = load i32, i32* %6
%45 = load i32, i32* @O_APPEND
%46 = and i32 %44, %45
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = load i32, i32* %6
%52 = load i32, i32* @O_NONBLOCK
%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 ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %57
57: ; preds = %55, %50
%58 = load i32, i32* %6
%59 = load i32, i32* @O_SYNC
%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 ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
br label %64
64: ; preds = %62, %57
%65 = call i32 @putchar(i8 signext 10)
store i32 0, i32* %3
br label %66
66: ; preds = %64, %27, %10
%67 = load i32, i32* %3
ret i32 %67
}

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