#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 */
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  char buf;
  if (argc < 2 || argc > 3) {
    fprintf(stderr, "Usage: %s file [open|fopen]\n", argv[0]);
    return 1;
  }
  if (argc == 2 || strcmp(argv[2], "open") == 0) {
    int fd;
    ssize_t len;
    fd = open(argv[1], O_RDWR);
    if (fd < 0) {
      perror("open");
      return 1;
    }
    len = read(fd, &buf, 1);
    if (len < 0) {
      perror("read");
      return 1;
    }
    close(fd);
  } else if (strcmp(argv[2], "fopen") == 0) {
    FILE *f;
    size_t len;
    f = fopen(argv[1], "rb");
    if (f == NULL) {
      perror("fopen");
      return 1;
    }
    len = fread(&buf, 1, 1, f);
    if (len != 1 && ferror(f) != 0) {
      perror("fread");
      return 1;
    }
    fclose(f);
  } else {
    fprintf(stderr, "ERROR: Unknown mode %s\n", argv[2]);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11ca <main+0x2a>
cmpl   $0x3,-0x8(%rbp)
jle    11f2 <main+0x52>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1f(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <main+0x1e4>
cmpl   $0x2,-0x8(%rbp)
je     121a <main+0x7a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe13(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    129a <main+0xfa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1255 <main+0xb5>
lea    0xdda(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <main+0x1e4>
mov    -0x18(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    128d <main+0xed>
lea    0xdab(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <main+0x1e4>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    137d <main+0x1dd>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xd74(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    134f <main+0x1af>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd5c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12f3 <main+0x153>
lea    0xd3b(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <main+0x1e4>
mov    -0x28(%rbp),%rcx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
je     1341 <main+0x1a1>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1341 <main+0x1a1>
lea    0xcf6(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <main+0x1e4>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1378 <main+0x1d8>
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xcc5(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <main+0x1e4>
jmp    137d <main+0x1dd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
lea    -0x4(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     11d0 <main+0x30>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe3f(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%ebx
jmp    1251 <main+0xb1>
cmp    $0x2,%edi
je     1215 <main+0x75>
mov    0x10(%rbx),%rdi
lea    0xe3e(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1215 <main+0x75>
mov    0x10(%rbx),%rdi
lea    0xe28(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     125a <main+0xba>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xe19(%rip),%rsi
jmp    11c1 <main+0x21>
mov    0x8(%rbx),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     12a6 <main+0x106>
mov    %eax,%ebp
lea    0x7(%rsp),%rsi
mov    $0x1,%ebx
mov    %eax,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
js     12b7 <main+0x117>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x8(%rbx),%rdi
lea    0xdbe(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12c5 <main+0x125>
mov    %rax,%rbx
lea    0x7(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
cmp    $0x1,%rax
je     129c <main+0xfc>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    12d6 <main+0x136>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    124f <main+0xaf>
lea    0xd71(%rip),%rdi
call   10c0 <perror@plt>
jmp    11c6 <main+0x26>
lea    0xd69(%rip),%rdi
call   10c0 <perror@plt>
jmp    1251 <main+0xb1>
lea    0xd51(%rip),%rdi
call   10c0 <perror@plt>
jmp    11c6 <main+0x26>
lea    0xd49(%rip),%rdi
call   10c0 <perror@plt>
jmp    11c6 <main+0x26>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Usage: %s file [open|fopen]\0A\00"
@.str.1 = [5 x i8] c"open\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [6 x i8] c"fopen\00"
@.str.4 = [3 x i8] c"rb\00"
@.str.5 = [6 x i8] c"fread\00"
@.str.6 = [24 x i8] c"ERROR: Unknown mode %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %13, %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %88
22: ; preds = %13
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 2
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %51
31: ; preds = %25, %22
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* @O_RDWR
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %88
41: ; preds = %31
%42 = load i32, i32* %7
%43 = call i64 @read(i32 %42, i8* %6, i32 1)
store i64 %43, i64* %8
%44 = load i64, i64* %8
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %88
48: ; preds = %41
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
br label %87
51: ; preds = %25
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = call i64 @strcmp(i8* %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %80
57: ; preds = %51
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = call i32* @fopen(i8* %60, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %61, i32** %9
%62 = load i32*, i32** %9
%63 = icmp eq i32* %62, null
br i1 %63, label %64, label %66
64: ; preds = %57
%65 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %88
66: ; preds = %57
%67 = load i32*, i32** %9
%68 = call i64 @fread(i8* %6, i32 1, i32 1, i32* %67)
store i64 %68, i64* %10
%69 = load i64, i64* %10
%70 = icmp ne i64 %69, 1
br i1 %70, label %71, label %77
71: ; preds = %66
%72 = load i32*, i32** %9
%73 = call i64 @ferror(i32* %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %88
77: ; preds = %71, %66
%78 = load i32*, i32** %9
%79 = call i32 @fclose(i32* %78)
br label %86
80: ; preds = %51
%81 = load i32, i32* @stderr
%82 = load i8**, i8*** %5
%83 = getelementptr inbounds i8*, i8** %82, i64 2
%84 = load i8*, i8** %83
%85 = call i32 @fprintf(i32 %81, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %84)
store i32 1, i32* %3
br label %88
86: ; preds = %77
br label %87
87: ; preds = %86, %48
store i32 0, i32* %3
br label %88
88: ; preds = %87, %80, %75, %64, %46, %39, %16
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Usage: %s file [open|fopen]\0A\00"
@.str.1 = [5 x i8] c"open\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [6 x i8] c"fopen\00"
@.str.4 = [3 x i8] c"rb\00"
@.str.5 = [6 x i8] c"fread\00"
@.str.6 = [24 x i8] c"ERROR: Unknown mode %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = add i32 %0, -4
%5 = icmp ult i32 %4, -2
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %57
10: ; preds = %2
%11 = icmp eq i32 %0, 2
br i1 %11, label %17, label %12
12: ; preds = %10
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %32
17: ; preds = %12, %10
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = tail call i32 @open(i8* %19, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %57
25: ; preds = %17
%26 = call i64 @read(i32 %21, i8* %3, i32 1)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %57
30: ; preds = %25
%31 = tail call i32 @close(i32 %21)
br label %57
32: ; preds = %12
%33 = load i8*, i8** %13
%34 = tail call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %53
36: ; preds = %32
%37 = getelementptr inbounds i8*, i8** %1, i64 1
%38 = load i8*, i8** %37
%39 = tail call i32* @fopen(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %57
43: ; preds = %36
%44 = call i64 @fread(i8* %3, i32 1, i32 1, i32* %39)
%45 = icmp eq i64 %44, 1
br i1 %45, label %51, label %46
46: ; preds = %43
%47 = tail call i64 @ferror(i32* %39)
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %57
51: ; preds = %43, %46
%52 = tail call i32 @fclose(i32* %39)
br label %57
53: ; preds = %32
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %13
%56 = tail call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %41, %49, %23, %28, %30, %51, %53, %6
%58 = phi i32 [ 1, %6 ], [ 1, %53 ], [ 0, %51 ], [ 0, %30 ], [ 1, %28 ], [ 1, %23 ], [ 1, %49 ], [ 1, %41 ]
ret i32 %58
}