#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
int O_SYNC;
#define O_WRONLY 128
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <fd>\n", argv[0]);
    return 128;
  }
  errno = 0;
  int fd = strtol(argv[1], NULL, 10);
  if (errno != 0) {
    perror("Invalid file descriptor");
    return 128;
  }
  if (fd < 0) {
    fprintf(stderr, "Please use a positive file descriptor");
    return 128;
  }
  int flags = fcntl(fd, F_GETFL, 0);
  if (flags < 0) {
    perror("Could not get file status flags");
    return 128;
  }
  switch (flags & 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 flag");
    break;
  }
  if (flags & O_APPEND) {
    printf(", append");
  }
  if (flags & O_NONBLOCK) {
    printf(", non-blocking");
  }
  if (flags & O_SYNC) {
    printf(", synchronous writes");
  }
  printf("\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)
jge    119a <main+0x4a>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x80,-0x4(%rbp)
jmp    134b <main+0x1fb>
mov    0x2e1f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    0x2df8(%rip),%rax
cmpq   $0x0,(%rax)
je     11ea <main+0x9a>
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
movl   $0x80,-0x4(%rbp)
jmp    134b <main+0x1fb>
cmpl   $0x0,-0x14(%rbp)
jge    1217 <main+0xc7>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x80,-0x4(%rbp)
jmp    134b <main+0x1fb>
mov    -0x14(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    124f <main+0xff>
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
movl   $0x80,-0x4(%rbp)
jmp    134b <main+0x1fb>
mov    -0x18(%rbp),%eax
mov    0x2d97(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     12a7 <main+0x157>
jmp    1270 <main+0x120>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     12b8 <main+0x168>
jmp    1283 <main+0x133>
mov    -0x1c(%rbp),%eax
sub    $0x82,%eax
jne    12c9 <main+0x179>
jmp    1296 <main+0x146>
lea    0xdd1(%rip),%rdi
call   1040 <printf@plt>
jmp    12d5 <main+0x185>
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
jmp    12d5 <main+0x185>
lea    0xdc4(%rip),%rdi
call   1040 <printf@plt>
jmp    12d5 <main+0x185>
lea    0xdbe(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2cc9(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12f6 <main+0x1a6>
lea    0xdaa(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2cd8(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1317 <main+0x1c7>
lea    0xd92(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2cbf(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1338 <main+0x1e8>
lea    0xd80(%rip),%rdi
call   1040 <printf@plt>
lea    0xccf(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x1,%edi
jg     1185 <main+0x25>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe87(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    127f <main+0x11f>
mov    0x2e34(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
cmpq   $0x0,(%rbx)
jne    126a <main+0x10a>
test   %eax,%eax
js     11f2 <main+0x92>
mov    0x2e00(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     1273 <main+0x113>
mov    %eax,%ebx
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
and    %ebx,%eax
add    $0xffffff80,%eax
cmp    $0x2,%eax
ja     120b <main+0xab>
cltq
lea    0xedf(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    1212 <main+0xb2>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    127f <main+0x11f>
lea    0xe7c(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d8a(%rip),%rax
test   %ebx,(%rax)
je     122e <main+0xce>
lea    0xe72(%rip),%rdi
call   1050 <printf@plt>
mov    0x2da3(%rip),%rax
test   %ebx,(%rax)
je     1245 <main+0xe5>
lea    0xe64(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d94(%rip),%rax
test   %ebx,(%rax)
je     125c <main+0xfc>
lea    0xe5c(%rip),%rdi
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xd9f(%rip),%rdi
jmp    127a <main+0x11a>
lea    0xdd4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x80,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"Usage: %s <fd>\0A\00"
@errno = global i64 0
@.str.1 = [24 x i8] c"Invalid file descriptor\00"
@.str.2 = [38 x i8] c"Please use a positive file descriptor\00"
@F_GETFL = global i32 0
@.str.3 = [32 x i8] c"Could not get file status flags\00"
@O_ACCMODE = global i32 0
@.str.4 = [10 x i8] c"read-only\00"
@.str.5 = [11 x i8] c"write-only\00"
@.str.6 = [11 x i8] c"read-write\00"
@.str.7 = [13 x i8] c"Unknown flag\00"
@O_APPEND = global i32 0
@.str.8 = [9 x i8] c", append\00"
@O_NONBLOCK = global i32 0
@.str.9 = [15 x i8] c", non-blocking\00"
@O_SYNC = global i32 0
@.str.10 = [21 x i8] c", synchronous writes\00"
@.str.11 = [2 x i8] c"\0A\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 slt i32 %8, 2
br i1 %9, label %10, label %16
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 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 128, i32* %3
br label %74
16: ; preds = %2
store i64 0, i64* @errno
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @strtol(i8* %19, i32* null, i32 10)
store i32 %20, i32* %6
%21 = load i64, i64* @errno
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 128, i32* %3
br label %74
25: ; preds = %16
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 128, i32* %3
br label %74
31: ; preds = %25
%32 = load i32, i32* %6
%33 = load i32, i32* @F_GETFL
%34 = call i32 @fcntl(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 128, i32* %3
br label %74
39: ; preds = %31
%40 = load i32, i32* %7
%41 = load i32, i32* @O_ACCMODE
%42 = and i32 %40, %41
switch i32 %42, label %49 [
i32 130, label %43
i32 128, label %45
i32 129, label %47
]
43: ; preds = %39
%44 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %51
45: ; preds = %39
%46 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %51
47: ; preds = %39
%48 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %51
49: ; preds = %39
%50 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %51
51: ; preds = %49, %47, %45, %43
%52 = load i32, i32* %7
%53 = load i32, i32* @O_APPEND
%54 = and i32 %52, %53
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
br label %58
58: ; preds = %56, %51
%59 = load i32, i32* %7
%60 = load i32, i32* @O_NONBLOCK
%61 = and i32 %59, %60
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %58
%64 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %65
65: ; preds = %63, %58
%66 = load i32, i32* %7
%67 = load i32, i32* @O_SYNC
%68 = and i32 %66, %67
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
br label %72
72: ; preds = %70, %65
%73 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0))
store i32 0, i32* %3
br label %74
74: ; preds = %72, %37, %28, %23, %10
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"Usage: %s <fd>\0A\00"
@errno = global i64 0
@.str.1 = [24 x i8] c"Invalid file descriptor\00"
@.str.2 = [38 x i8] c"Please use a positive file descriptor\00"
@F_GETFL = global i32 0
@.str.3 = [32 x i8] c"Could not get file status flags\00"
@O_ACCMODE = global i32 0
@.str.4 = [10 x i8] c"read-only\00"
@.str.5 = [11 x i8] c"write-only\00"
@.str.6 = [11 x i8] c"read-write\00"
@.str.7 = [13 x i8] c"Unknown flag\00"
@O_APPEND = global i32 0
@.str.8 = [9 x i8] c", append\00"
@O_NONBLOCK = global i32 0
@.str.9 = [15 x i8] c", non-blocking\00"
@O_SYNC = global i32 0
@.str.10 = [21 x i8] c", synchronous writes\00"
@reltable.main = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([11 x i8]* @.str.5 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([11 x i8]* @.str.6 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([10 x i8]* @.str.4 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 %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %58
8: ; preds = %2
store i64 0, i64* @errno
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @strtol(i8* nocapture %10, i32* null, i32 10)
%12 = load i64, i64* @errno
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %58
16: ; preds = %8
%17 = icmp slt i32 %11, 0
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %58
21: ; preds = %16
%22 = load i32, i32* @F_GETFL
%23 = tail call i32 @fcntl(i32 %11, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %58
27: ; preds = %21
%28 = load i32, i32* @O_ACCMODE
%29 = and i32 %28, %23
%30 = add i32 %29, -128
%31 = icmp ult i32 %30, 3
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = sext i32 %30 to i64
%34 = shl i64 %33, 2
%35 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %34)
br label %36
36: ; preds = %27, %32
%37 = phi i8* [ %35, %32 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), %27 ]
%38 = tail call i32 @printf(i8* dereferenceable(1) %37)
%39 = load i32, i32* @O_APPEND
%40 = and i32 %39, %23
%41 = icmp eq i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %36
%43 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
br label %44
44: ; preds = %42, %36
%45 = load i32, i32* @O_NONBLOCK
%46 = and i32 %45, %23
%47 = icmp eq i32 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = load i32, i32* @O_SYNC
%52 = and i32 %51, %23
%53 = icmp eq i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %50
%55 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
br label %56
56: ; preds = %54, %50
%57 = tail call i32 @putchar(i32 10)
br label %58
58: ; preds = %14, %18, %56, %25, %4
%59 = phi i32 [ 128, %4 ], [ 128, %14 ], [ 128, %18 ], [ 128, %25 ], [ 0, %56 ]
ret i32 %59
}