#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 F_OK;
int O_RDONLY;
int O_RDWR;
int O_WRONLY;
int R_OK;
int W_OK;
int X_OK;
scalar_t__ access(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ err_ret(char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strcasecmp(char *, char *);
int main(int argc, char *argv[]) {
  if (argc != 3)
    err_quit("usage: acc <pathname> <flag>");
  int flag = 0, oflag = 0;
  if (strcasecmp(argv[2], "R") == 0) {
    flag = R_OK;
    oflag = O_RDONLY;
  } else if (strcasecmp(argv[2], "W") == 0) {
    flag = W_OK;
    oflag = O_WRONLY;
  } else if (strcasecmp(argv[2], "X") == 0) {
    flag = X_OK;
    oflag = O_RDWR;
  } else if (strcasecmp(argv[2], "F") == 0) {
    flag = F_OK;
    oflag = O_RDONLY;
  } else {
    printf("unknown flag %s\n", argv[2]);
    exit(0);
  }
  if (access(argv[1], flag) < 0)
    err_ret("access error for %s", argv[1]);
  else
    printf("%s access OK\n", argv[2]);
  int fd = open(argv[1], oflag);
  if (fd < 0)
    err_ret("open error for %s", argv[1]);
  else
    printf("open for %sing OK\n", argv[2]);
  close(fd);
  exit(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   $0x3,-0x8(%rbp)
je     11ac <main+0x2c>
lea    0xe59(%rip),%rdi
call   1060 <err_quit@plt>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe54(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    11f5 <main+0x75>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    12d0 <main+0x150>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe1b(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    1230 <main+0xb0>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    12cb <main+0x14b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xde2(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    126b <main+0xeb>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    12c6 <main+0x146>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xda9(%rip),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    12a6 <main+0x126>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    12c1 <main+0x141>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd70(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    12c6 <main+0x146>
jmp    12cb <main+0x14b>
jmp    12d0 <main+0x150>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x14(%rbp),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jge    1303 <main+0x183>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd3d(%rip),%rdi
call   1090 <err_ret@plt>
jmp    1317 <main+0x197>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd38(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x18(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    134d <main+0x1cd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd15(%rip),%rdi
call   1090 <err_ret@plt>
jmp    1361 <main+0x1e1>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xd0e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
cmp    $0x3,%edi
je     1199 <main+0x19>
lea    0xe6c(%rip),%rdi
call   1060 <err_quit@plt>
mov    0x10(%r14),%rbx
lea    0xe79(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
mov    0x2e2d(%rip),%r15
test   %rax,%rax
jne    11c1 <main+0x41>
mov    0x2e01(%rip),%rax
jmp    1224 <main+0xa4>
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
jne    11e5 <main+0x65>
mov    0x2dfc(%rip),%rax
mov    0x2dcd(%rip),%r15
jmp    1224 <main+0xa4>
lea    0xe35(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
jne    1209 <main+0x89>
mov    0x2dd0(%rip),%rax
mov    0x2de1(%rip),%r15
jmp    1224 <main+0xa4>
lea    0xe13(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
jne    129a <main+0x11a>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
mov    (%r15),%ebx
mov    0x8(%r14),%rdi
call   1080 <access@plt>
test   %rax,%rax
jns    1249 <main+0xc9>
mov    0x8(%r14),%rsi
lea    0xdf4(%rip),%rdi
call   1090 <err_ret@plt>
jmp    1259 <main+0xd9>
mov    0x10(%r14),%rsi
lea    0xdf6(%rip),%rdi
call   1040 <printf@plt>
mov    0x8(%r14),%rdi
mov    %ebx,%esi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    127c <main+0xfc>
mov    0x8(%r14),%rsi
lea    0xde3(%rip),%rdi
call   1090 <err_ret@plt>
jmp    128c <main+0x10c>
mov    0x10(%r14),%rsi
lea    0xde3(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd84(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"usage: acc <pathname> <flag>\00"
@.str.1 = [2 x i8] c"R\00"
@R_OK = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [2 x i8] c"W\00"
@W_OK = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [2 x i8] c"X\00"
@X_OK = global i32 0
@O_RDWR = global i32 0
@.str.4 = [2 x i8] c"F\00"
@F_OK = global i32 0
@.str.5 = [17 x i8] c"unknown flag %s\0A\00"
@.str.6 = [20 x i8] c"access error for %s\00"
@.str.7 = [14 x i8] c"%s access OK\0A\00"
@.str.8 = [18 x i8] c"open error for %s\00"
@.str.9 = [19 x i8] c"open for %sing OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @err_quit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
store i32 0, i32* %6
store i32 0, i32* %7
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 2
%16 = load i8*, i8** %15
%17 = call i64 @strcasecmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @R_OK
store i32 %20, i32* %6
%21 = load i32, i32* @O_RDONLY
store i32 %21, i32* %7
br label %58
22: ; preds = %13
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i64 @strcasecmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i32, i32* @W_OK
store i32 %29, i32* %6
%30 = load i32, i32* @O_WRONLY
store i32 %30, i32* %7
br label %57
31: ; preds = %22
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i64 @strcasecmp(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i32, i32* @X_OK
store i32 %38, i32* %6
%39 = load i32, i32* @O_RDWR
store i32 %39, i32* %7
br label %56
40: ; preds = %31
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i64 @strcasecmp(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* @F_OK
store i32 %47, i32* %6
%48 = load i32, i32* @O_RDONLY
store i32 %48, i32* %7
br label %55
49: ; preds = %40
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 2
%52 = load i8*, i8** %51
%53 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %46
br label %56
56: ; preds = %55, %37
br label %57
57: ; preds = %56, %28
br label %58
58: ; preds = %57, %19
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 1
%61 = load i8*, i8** %60
%62 = load i32, i32* %6
%63 = call i64 @access(i8* %61, i32 %62)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %70
65: ; preds = %58
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 1
%68 = load i8*, i8** %67
%69 = call i32 @err_ret(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %68)
br label %75
70: ; preds = %58
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 2
%73 = load i8*, i8** %72
%74 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %70, %65
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = load i32, i32* %7
%80 = call i32 @open(i8* %78, i32 %79)
store i32 %80, i32* %8
%81 = load i32, i32* %8
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %88
83: ; preds = %75
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = call i32 @err_ret(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* %86)
br label %93
88: ; preds = %75
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 2
%91 = load i8*, i8** %90
%92 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i8* %91)
br label %93
93: ; preds = %88, %83
%94 = load i32, i32* %8
%95 = call i32 @close(i32 %94)
%96 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [29 x i8] c"usage: acc <pathname> <flag>\00"
@.str.1 = [2 x i8] c"R\00"
@R_OK = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [2 x i8] c"W\00"
@W_OK = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [2 x i8] c"X\00"
@X_OK = global i32 0
@O_RDWR = global i32 0
@.str.4 = [2 x i8] c"F\00"
@F_OK = global i32 0
@.str.5 = [17 x i8] c"unknown flag %s\0A\00"
@.str.6 = [20 x i8] c"access error for %s\00"
@.str.7 = [14 x i8] c"%s access OK\0A\00"
@.str.8 = [18 x i8] c"open error for %s\00"
@.str.9 = [19 x i8] c"open for %sing OK\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @err_quit(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i64 @strcasecmp(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %6
%12 = tail call i64 @strcasecmp(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %11
%15 = tail call i64 @strcasecmp(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = tail call i64 @strcasecmp(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %17, %14, %11, %6
%24 = phi i32* [ @O_RDONLY, %6 ], [ @O_WRONLY, %11 ], [ @O_RDWR, %14 ], [ @O_RDONLY, %17 ]
%25 = phi i32* [ @R_OK, %6 ], [ @W_OK, %11 ], [ @X_OK, %14 ], [ @F_OK, %17 ]
%26 = load i32, i32* %25
%27 = load i32, i32* %24
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = tail call i64 @access(i8* %29, i32 %26)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i8*, i8** %28
%34 = tail call i32 @err_ret(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %33)
br label %38
35: ; preds = %23
%36 = load i8*, i8** %7
%37 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %32
%39 = load i8*, i8** %28
%40 = tail call i32 @open(i8* %39, i32 %27)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i8*, i8** %28
%44 = tail call i32 @err_ret(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i8* %43)
br label %48
45: ; preds = %38
%46 = load i8*, i8** %7
%47 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %45, %42
%49 = tail call i32 @close(i32 %40)
%50 = tail call i32 @exit(i32 0)
unreachable
}