#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*/ O_RDONLY;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ err_ret(char *, char *);
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  if (argc != 2) {
    err_quit("Usage: prog file\n");
  }
  if (access(*++argv, R_OK) < 0) {
    err_ret("access error for %s", *argv);
  } else {
    printf("read access OK\n");
  }
  if (open(*argv, O_RDONLY) < 0) {
    err_ret("open error for %s", *argv);
  } else {
    printf("open for reading OK\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     117c <main+0x2c>
lea    0xe89(%rip),%rdi
call   1040 <err_quit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jge    11bf <main+0x6f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5d(%rip),%rdi
call   1070 <err_ret@plt>
jmp    11cb <main+0x7b>
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0x0,%rax
jge    1202 <main+0xb2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3e(%rip),%rdi
call   1070 <err_ret@plt>
jmp    120e <main+0xbe>
lea    0xe3f(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
je     1165 <main+0x15>
lea    0xea0(%rip),%rdi
call   1040 <err_quit@plt>
mov    0x8(%rbx),%rdi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
test   %rax,%rax
js     118a <main+0x3a>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
jmp    119a <main+0x4a>
mov    0x8(%rbx),%rsi
lea    0xe7d(%rip),%rdi
call   1070 <err_ret@plt>
mov    0x8(%rbx),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %rax,%rax
js     11bf <main+0x6f>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
jmp    11cf <main+0x7f>
mov    0x8(%rbx),%rsi
lea    0xe5c(%rip),%rdi
call   1070 <err_ret@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Usage: prog file\0A\00"
@R_OK = global i32 0
@.str.1 = [20 x i8] c"access error for %s\00"
@.str.2 = [16 x i8] c"read access OK\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [18 x i8] c"open error for %s\00"
@.str.4 = [21 x i8] c"open for reading OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* %4
%7 = icmp ne i32 %6, 2
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @err_quit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i32 1
store i8** %12, i8*** %5
%13 = load i8*, i8** %12
%14 = load i32, i32* @R_OK
%15 = call i64 @access(i8* %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = load i8**, i8*** %5
%19 = load i8*, i8** %18
%20 = call i32 @err_ret(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %23
21: ; preds = %10
%22 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i8**, i8*** %5
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDONLY
%27 = call i64 @open(i8* %25, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = call i32 @err_ret(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %31)
br label %35
33: ; preds = %23
%34 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"Usage: prog file\0A\00"
@R_OK = global i32 0
@.str.1 = [20 x i8] c"access error for %s\00"
@O_RDONLY = global i32 0
@.str.3 = [18 x i8] c"open error for %s\00"
@str = [15 x i8] c"read access OK\00"
@str.5 = [20 x i8] c"open for reading OK\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @err_quit(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @R_OK
%10 = tail call i64 @access(i8* %8, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = load i8*, i8** %7
%14 = tail call i32 @err_ret(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %17
15: ; preds = %6
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i8*, i8** %7
%19 = load i32, i32* @O_RDONLY
%20 = tail call i64 @open(i8* %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %7
%24 = tail call i32 @err_ret(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %27
25: ; preds = %17
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
ret i32 0
}