#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*/ abort();
int atoi(char const *);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
int main(int argc, char const *argv[]) {
  int val;
  if (argc != 2) {
    printf("Usage: printflag <descriptor#>\n");
    exit(0);
  }
  if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0) {
    fprintf(stderr, "fcntl error for fd %d\n", atoi(argv[1]));
    exit(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:
    fprintf(stderr, "unknown access mode\n");
    abort();
  }
  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     11a3 <main+0x33>
lea    0xe69(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1204 <main+0x94>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%eax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     125c <main+0xec>
jmp    1225 <main+0xb5>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     126d <main+0xfd>
jmp    1238 <main+0xc8>
mov    -0x1c(%rbp),%eax
sub    $0x82,%eax
jne    127e <main+0x10e>
jmp    124b <main+0xdb>
lea    0xde5(%rip),%rdi
call   1060 <printf@plt>
jmp    129c <main+0x12c>
lea    0xdde(%rip),%rdi
call   1060 <printf@plt>
jmp    129c <main+0x12c>
lea    0xdd8(%rip),%rdi
call   1060 <printf@plt>
jmp    129c <main+0x12c>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x14(%rbp),%eax
mov    0x2d12(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12bd <main+0x14d>
lea    0xdb4(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
mov    0x2d19(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12de <main+0x16e>
lea    0xd9c(%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
cmp    $0x2,%edi
jne    1223 <main+0xa3>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    0x2e28(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1060 <fcntl@plt>
test   %eax,%eax
js     1236 <main+0xb6>
mov    %eax,%ebx
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
and    %ebx,%eax
add    $0xffffff80,%eax
cmp    $0x3,%eax
jae    1264 <main+0xe4>
cltq
lea    0xeaf(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
call   1070 <printf@plt>
mov    0x2dd0(%rip),%rax
test   %ebx,(%rax)
je     11f8 <main+0x78>
lea    0xe59(%rip),%rdi
call   1070 <printf@plt>
mov    0x2de1(%rip),%rax
test   %ebx,(%rax)
je     120f <main+0x8f>
lea    0xe4b(%rip),%rdi
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe39(%rip),%rdi
call   1050 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
lea    0xdb1(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: printflag <descriptor#>\0A\00"
@F_GETFL = global i32 0
@stderr = 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 = [21 x i8] c"unknown access mode\0A\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 %12
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%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* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %12
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %12
%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 %42
34: ; preds = %28
%35 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %42
36: ; preds = %28
%37 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %42
38: ; preds = %28
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 (...) @abort()
unreachable
42: ; preds = %36, %34, %32
%43 = load i32, i32* %6
%44 = load i32, i32* @O_APPEND
%45 = and i32 %43, %44
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %49
49: ; preds = %47, %42
%50 = load i32, i32* %6
%51 = load i32, i32* @O_NONBLOCK
%52 = and i32 %50, %51
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %56
56: ; preds = %54, %49
%57 = call i32 @putchar(i8 signext 10)
ret i32 0
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@stderr = 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 = [21 x i8] c"unknown access mode\0A\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"
@str = [31 x i8] c"Usage: printflag <descriptor#>\00"
@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 eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %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 %20
14: ; preds = %7
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %8
%17 = tail call i32 @atoi(i8* %16)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %7
%21 = load i32, i32* @O_ACCMODE
%22 = and i32 %21, %12
%23 = add i32 %22, -128
%24 = icmp ult i32 %23, 3
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%28 = tail call i32 (...) @abort()
unreachable
29: ; preds = %20
%30 = sext i32 %23 to i64
%31 = shl i64 %30, 2
%32 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %31)
%33 = tail call i32 @printf(i8* dereferenceable(1) %32)
%34 = load i32, i32* @O_APPEND
%35 = and i32 %34, %12
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %29
%38 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %37, %29
%40 = load i32, i32* @O_NONBLOCK
%41 = and i32 %40, %12
%42 = icmp eq i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %39
%44 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = tail call i32 @putchar(i8 signext 10)
ret i32 0
}