#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_RDONLY;
int O_RDWR;
int O_WRONLY;
int atoi(char *);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char *argv[]) {
  int fd, value;
  fd = atoi(argv[1]);
  if ((value = fcntl(fd, F_GETFL, 0)) == -1)
    return perror("fcntl :"), 0;
  value &= O_ACCMODE;
  printf("Readonly flag :: %s\n", value == O_RDONLY ? "set" : "unset");
  printf("Write Only flag :: %s\n", value == O_WRONLY ? "set" : "unset");
  printf("Read Write flag :: %s\n", value == O_RDWR ? "set" : "unset");
  printf("Append flag :: %s\n", value & O_APPEND ? "set" : "unset");
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    119d <main+0x5d>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1264 <main+0x124>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
lea    0xe5e(%rip),%rsi
lea    0xe59(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%rsi
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%ecx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
lea    0xe32(%rip),%rsi
lea    0xe2d(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%rsi
lea    0xe24(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%ecx
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
lea    0xe06(%rip),%rsi
lea    0xe01(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%rsi
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%ecx
mov    0x2d76(%rip),%rax
and    (%rax),%ecx
lea    0xdda(%rip),%rsi
lea    0xdd5(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xdf9(%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   %r15
push   %r14
push   %rbx
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    0x2e73(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11fa <main+0xba>
mov    %eax,%ebx
mov    0x2e7e(%rip),%rax
and    (%rax),%ebx
mov    0x2e5d(%rip),%rax
cmp    (%rax),%ebx
lea    0xe9b(%rip),%r14
lea    0xe92(%rip),%r15
mov    %r15,%rsi
cmove  %r14,%rsi
lea    0xe6f(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e13(%rip),%rax
cmp    (%rax),%ebx
mov    %r15,%rsi
cmove  %r14,%rsi
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e1f(%rip),%rax
cmp    (%rax),%ebx
mov    %r15,%rsi
cmove  %r14,%rsi
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dd3(%rip),%rax
and    (%rax),%ebx
cmove  %r15,%r14
lea    0xe67(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdff(%rip),%rdi
call   1050 <perror@plt>
jmp    11f2 <main+0xb2>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [8 x i8] c"fcntl :\00"
@O_ACCMODE = global i32 0
@.str.1 = [21 x i8] c"Readonly flag :: %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [4 x i8] c"set\00"
@.str.3 = [6 x i8] c"unset\00"
@.str.4 = [23 x i8] c"Write Only flag :: %s\0A\00"
@O_WRONLY = global i32 0
@.str.5 = [23 x i8] c"Read Write flag :: %s\0A\00"
@O_RDWR = global i32 0
@.str.6 = [19 x i8] c"Append flag :: %s\0A\00"
@O_APPEND = global i32 0
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 i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @atoi(i8* %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = load i32, i32* @F_GETFL
%14 = call i32 @fcntl(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %47
18: ; preds = %2
%19 = load i32, i32* @O_ACCMODE
%20 = load i32, i32* %7
%21 = and i32 %20, %19
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = load i32, i32* @O_RDONLY
%24 = icmp eq i32 %22, %23
%25 = zext i1 %24 to i64
%26 = select i1 %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%27 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %7
%29 = load i32, i32* @O_WRONLY
%30 = icmp eq i32 %28, %29
%31 = zext i1 %30 to i64
%32 = select i1 %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%33 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %7
%35 = load i32, i32* @O_RDWR
%36 = icmp eq i32 %34, %35
%37 = zext i1 %36 to i64
%38 = select i1 %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%39 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %38)
%40 = load i32, i32* %7
%41 = load i32, i32* @O_APPEND
%42 = and i32 %40, %41
%43 = icmp ne i32 %42, 0
%44 = zext i1 %43 to i64
%45 = select i1 %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%46 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %45)
store i32 0, i32* %3
br label %47
47: ; preds = %18, %16
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [8 x i8] c"fcntl :\00"
@O_ACCMODE = global i32 0
@.str.1 = [21 x i8] c"Readonly flag :: %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [4 x i8] c"set\00"
@.str.3 = [6 x i8] c"unset\00"
@.str.4 = [23 x i8] c"Write Only flag :: %s\0A\00"
@O_WRONLY = global i32 0
@.str.5 = [23 x i8] c"Read Write flag :: %s\0A\00"
@O_RDWR = global i32 0
@.str.6 = [19 x i8] c"Append flag :: %s\0A\00"
@O_APPEND = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 @atoi(i8* %4)
%6 = load i32, i32* @F_GETFL
%7 = tail call i32 @fcntl(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %31
11: ; preds = %2
%12 = load i32, i32* @O_ACCMODE
%13 = and i32 %12, %7
%14 = load i32, i32* @O_RDONLY
%15 = icmp eq i32 %13, %14
%16 = select i1 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @O_WRONLY
%19 = icmp eq i32 %13, %18
%20 = select i1 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @O_RDWR
%23 = icmp eq i32 %13, %22
%24 = select i1 %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @O_APPEND
%27 = and i32 %26, %13
%28 = icmp eq i32 %27, 0
%29 = select i1 %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0)
%30 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %11, %9
ret i32 0
}