#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;
scalar_t__ O_APPEND;
scalar_t__ O_NONBLOCK;
scalar_t__ O_RDONLY;
scalar_t__ O_RDWR;
scalar_t__ O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("usage: fcntl1 [fd]\n");
    exit(1);
  }
  int fd = atoi(argv[1]);
  int flags = fcntl(fd, F_GETFL);
  if (flags == -1) {
    perror("fcntl error.\n");
    exit(1);
  }
  if (flags && O_RDONLY) {
    printf("rdonly.\n");
  }
  if (flags && O_WRONLY) {
    printf("wronly.\n");
  }
  if (flags && O_RDWR) {
    printf("rdwr.\n");
  }
  if (flags && O_NONBLOCK) {
    printf("nonblock.\n");
  }
  if (flags && O_APPEND) {
    printf("append.\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     1186 <main+0x36>
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ca <main+0x7a>
lea    0xe59(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     11f1 <main+0xa1>
mov    0x2dfd(%rip),%rax
cmpq   $0x0,(%rax)
je     11f1 <main+0xa1>
lea    0xe36(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     1218 <main+0xc8>
mov    0x2db6(%rip),%rax
cmpq   $0x0,(%rax)
je     1218 <main+0xc8>
lea    0xe18(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     123f <main+0xef>
mov    0x2dbf(%rip),%rax
cmpq   $0x0,(%rax)
je     123f <main+0xef>
lea    0xdfa(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     1266 <main+0x116>
mov    0x2d90(%rip),%rax
cmpq   $0x0,(%rax)
je     1266 <main+0x116>
lea    0xdda(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     128d <main+0x13d>
mov    0x2d39(%rip),%rax
cmpq   $0x0,(%rax)
je     128d <main+0x13d>
lea    0xdbe(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    1213 <main+0xc3>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1229 <main+0xd9>
mov    %eax,%ebx
test   %eax,%eax
je     119b <main+0x4b>
mov    0x2e4f(%rip),%rax
cmpq   $0x0,(%rax)
je     119b <main+0x4b>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
je     11b8 <main+0x68>
mov    0x2e12(%rip),%rax
cmpq   $0x0,(%rax)
je     11b8 <main+0x68>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
je     11d5 <main+0x85>
mov    0x2e25(%rip),%rax
cmpq   $0x0,(%rax)
je     11d5 <main+0x85>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
je     11f2 <main+0xa2>
mov    0x2e00(%rip),%rax
cmpq   $0x0,(%rax)
je     11f2 <main+0xa2>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
je     120f <main+0xbf>
mov    0x2db3(%rip),%rax
cmpq   $0x0,(%rax)
je     120f <main+0xbf>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdd0(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"usage: fcntl1 [fd]\0A\00"
@F_GETFL = global i32 0
@.str.1 = [14 x i8] c"fcntl error.\0A\00"
@O_RDONLY = global i64 0
@.str.2 = [9 x i8] c"rdonly.\0A\00"
@O_WRONLY = global i64 0
@.str.3 = [9 x i8] c"wronly.\0A\00"
@O_RDWR = global i64 0
@.str.4 = [7 x i8] c"rdwr.\0A\00"
@O_NONBLOCK = global i64 0
@.str.5 = [11 x i8] c"nonblock.\0A\00"
@O_APPEND = global i64 0
@.str.6 = [9 x i8] c"append.\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 ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = load i32, i32* @F_GETFL
%20 = call i32 @fcntl(i32 %18, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %13
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i64, i64* @O_RDONLY
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %29, %26
%35 = load i32, i32* %7
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i64, i64* @O_WRONLY
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %37, %34
%43 = load i32, i32* %7
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i64, i64* @O_RDWR
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %45, %42
%51 = load i32, i32* %7
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i64, i64* @O_NONBLOCK
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %56, %53, %50
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i64, i64* @O_APPEND
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %66
66: ; preds = %64, %61, %58
ret i32 0
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str.1 = [14 x i8] c"fcntl error.\0A\00"
@O_RDONLY = global i64 0
@O_WRONLY = global i64 0
@O_RDWR = global i64 0
@O_NONBLOCK = global i64 0
@O_APPEND = global i64 0
@str = [8 x i8] c"append.\00"
@str.7 = [10 x i8] c"nonblock.\00"
@str.8 = [6 x i8] c"rdwr.\00"
@str.9 = [8 x i8] c"wronly.\00"
@str.10 = [8 x i8] c"rdonly.\00"
@str.11 = [19 x i8] c"usage: fcntl1 [fd]\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 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.11, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
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)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %7
%18 = icmp ne i32 %12, 0
%19 = load i64, i64* @O_RDONLY
%20 = icmp ne i64 %19, 0
%21 = select i1 %18, i1 %20, i1 false
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.10, i64 0, i64 0))
br label %24
24: ; preds = %22, %17
%25 = load i64, i64* @O_WRONLY
%26 = icmp ne i64 %25, 0
%27 = select i1 %18, i1 %26, i1 false
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = load i64, i64* @O_RDWR
%32 = icmp ne i64 %31, 0
%33 = select i1 %18, i1 %32, i1 false
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = load i64, i64* @O_NONBLOCK
%38 = icmp ne i64 %37, 0
%39 = select i1 %18, i1 %38, i1 false
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = load i64, i64* @O_APPEND
%44 = icmp ne i64 %43, 0
%45 = select i1 %18, i1 %44, i1 false
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
ret i32 0
}