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

/* Variables and functions */
int /*<<< orphan*/ errx(int, char *);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int main(int argc, char **argv) {
  if (argc != 3) {
    fprintf(stderr, "Usage: %s read|write|append <filename>\n", argv[0]);
    return 1;
  }
  const char *mode;
  if (!strcmp(argv[1], "read")) {
    mode = "r";
  } else if (!strcmp(argv[1], "write")) {
    mode = "r+";
  } else if (!strcmp(argv[1], "append")) {
    mode = "a";
  } else {
    errx(1, "invalid cmdline argument");
  }
  FILE *fp = fopen(argv[2], mode);
  if (!fp) {
    perror("fopen failed");
    return 2;
  }
  int ret = fclose(fp);
  if (ret) {
    perror("fclose failed");
    return 3;
  }
  printf("file_check_policy succeeded\n");
  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   $0x3,-0x8(%rbp)
je     11b8 <main+0x48>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x163>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe61(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11e5 <main+0x75>
lea    0xe51(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    125a <main+0xea>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1212 <main+0xa2>
lea    0xe2c(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1255 <main+0xe5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe17(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    123f <main+0xcf>
lea    0xe09(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    1250 <main+0xe0>
mov    $0x1,%edi
lea    0xdf6(%rip),%rsi
call   1040 <errx@plt>
jmp    1255 <main+0xe5>
jmp    125a <main+0xea>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1292 <main+0x122>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d3 <main+0x163>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12c0 <main+0x150>
lea    0xdb8(%rip),%rdi
call   1090 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12d3 <main+0x163>
lea    0xdae(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    11c6 <main+0x56>
mov    0x8(%rbx),%r14
lea    0xea1(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e5 <main+0x75>
lea    0xe95(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11ee <main+0x7e>
lea    0xe8b(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    124b <main+0xdb>
lea    0xe7b(%rip),%rsi
jmp    11f5 <main+0x85>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe27(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
jmp    121d <main+0xad>
lea    0xe41(%rip),%rsi
jmp    11f5 <main+0x85>
lea    0xe40(%rip),%rsi
mov    0x10(%rbx),%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1225 <main+0xb5>
mov    %rax,%rdi
call   1040 <fclose@plt>
test   %eax,%eax
jne    1238 <main+0xc8>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2e(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
jmp    121d <main+0xad>
lea    0xe28(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%eax
jmp    121d <main+0xad>
lea    0xdef(%rip),%rsi
mov    $0x1,%edi
call   1050 <errx@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s read|write|append <filename>\0A\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [3 x i8] c"r+\00"
@.str.5 = [7 x i8] c"append\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [25 x i8] c"invalid cmdline argument\00"
@.str.8 = [13 x i8] c"fopen failed\00"
@.str.9 = [14 x i8] c"fclose failed\00"
@.str.10 = [29 x i8] c"file_check_policy succeeded\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 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 %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %61
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @strcmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %17
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %6
br label %42
24: ; preds = %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @strcmp(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %24
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8** %6
br label %41
31: ; preds = %24
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @strcmp(i8* %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %31
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8** %6
br label %40
38: ; preds = %31
%39 = call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
br label %40
40: ; preds = %38, %37
br label %41
41: ; preds = %40, %30
br label %42
42: ; preds = %41, %23
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = load i8*, i8** %44
%46 = load i8*, i8** %6
%47 = call i32* @fopen(i8* %45, i8* %46)
store i32* %47, i32** %7
%48 = load i32*, i32** %7
%49 = icmp ne i32* %48, null
br i1 %49, label %52, label %50
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
store i32 2, i32* %3
br label %61
52: ; preds = %42
%53 = load i32*, i32** %7
%54 = call i32 @fclose(i32* %53)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0))
store i32 3, i32* %3
br label %61
59: ; preds = %52
%60 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %3
br label %61
61: ; preds = %59, %57, %50, %11
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s read|write|append <filename>\0A\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [3 x i8] c"r+\00"
@.str.5 = [7 x i8] c"append\00"
@.str.6 = [2 x i8] c"a\00"
@.str.7 = [25 x i8] c"invalid cmdline argument\00"
@.str.8 = [13 x i8] c"fopen failed\00"
@.str.9 = [14 x i8] c"fclose failed\00"
@str = [28 x i8] c"file_check_policy succeeded\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %36
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %8
%14 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
unreachable
21: ; preds = %16, %13, %8
%22 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), %16 ]
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = tail call i32* @fopen(i8* %24, i8* %22)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
br label %36
29: ; preds = %21
%30 = tail call i32 @fclose(i32* %25)
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0))
br label %36
34: ; preds = %29
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %27, %34, %32, %4
%37 = phi i32 [ 1, %4 ], [ 2, %27 ], [ 3, %32 ], [ 0, %34 ]
ret i32 %37
}