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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int main(int argc, const char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s FILE\n", argv[0]);
    return 0;
  }
  const char *fname = argv[1];
  FILE *infile = fopen(fname, "r");
  if (!infile) {
    fprintf(stderr, "Error opening %s: %s\n", fname, strerror(errno));
    return 2;
  }
  char *line;
  size_t line_size;
  ssize_t nread = getline(&line, &line_size, infile);
  if (nread == -1) {
    fprintf(stderr, "Error reading from %s: %s\n", fname, strerror(errno));
    return 3;
  }
  if (strcmp("open sesame", line) == 0) {
    fprintf(stdout, "Welcome!\n");
    return 0;
  } else {
    fprintf(stdout, "Access denied.\n");
    return 4;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119a <main+0x4a>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ca <main+0x17a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120b <main+0xbb>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12ca <main+0x17a>
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <getline@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    126f <main+0x11f>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    12ca <main+0x17a>
mov    -0x28(%rbp),%rsi
lea    0xdc9(%rip),%rdi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    12ac <main+0x15c>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ca <main+0x17a>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    11c9 <main+0x79>
mov    0x8(%rsi),%rbx
lea    0xeaa(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e7 <main+0x97>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1060 <getline@plt>
cmp    $0xffffffff,%eax
je     121b <main+0xcb>
mov    0x8(%rsp),%rsi
lea    0xea9(%rip),%rdi
call   1030 <strcmp@plt>
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%edi
test   %rax,%rax
je     124f <main+0xff>
lea    0xea1(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x4,%ebx
jmp    125f <main+0x10f>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe24(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    125f <main+0x10f>
mov    0x2df2(%rip),%rax
mov    (%rax),%ebp
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe0d(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%ebx
jmp    125f <main+0x10f>
mov    0x2dbe(%rip),%rax
mov    (%rax),%ebp
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdef(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x3,%ebx
jmp    125f <main+0x10f>
lea    0xdf9(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"Usage: %s FILE\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [22 x i8] c"Error opening %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [27 x i8] c"Error reading from %s: %s\0A\00"
@.str.4 = [12 x i8] c"open sesame\00"
@stdout = global i32 0
@.str.5 = [10 x i8] c"Welcome!\0A\00"
@.str.6 = [16 x i8] c"Access denied.\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 i8*
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 0, i32* %3
br label %54
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = icmp ne i32* %25, null
br i1 %26, label %33, label %27
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %6
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* %31)
store i32 2, i32* %3
br label %54
33: ; preds = %19
%34 = load i32*, i32** %7
%35 = call i32 @getline(i8** %8, i64* %9, i32* %34)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %6
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %40, i8* %42)
store i32 3, i32* %3
br label %54
44: ; preds = %33
%45 = load i8*, i8** %8
%46 = call i64 @strcmp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* @stdout
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %54
51: ; preds = %44
%52 = load i32, i32* @stdout
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 4, i32* %3
br label %54
54: ; preds = %51, %48, %38, %27, %13
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"Usage: %s FILE\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [22 x i8] c"Error opening %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [27 x i8] c"Error reading from %s: %s\0A\00"
@.str.4 = [12 x i8] c"open sesame\00"
@stdout = global i32 0
@.str.5 = [10 x i8] c"Welcome!\0A\00"
@.str.6 = [16 x i8] c"Access denied.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %41
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %12, i8* %18)
br label %41
20: ; preds = %10
%21 = bitcast i8** %3 to i8*
%22 = bitcast i64* %4 to i8*
%23 = call i32 @getline(i8** %3, i64* %4, i32* %13)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %12, i8* %28)
br label %39
30: ; preds = %20
%31 = load i8*, i8** %3
%32 = call i64 @strcmp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %31)
%33 = icmp eq i64 %32, 0
%34 = load i32, i32* @stdout
br i1 %33, label %35, label %37
35: ; preds = %30
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %39
37: ; preds = %30
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %37, %35, %25
%40 = phi i32 [ 3, %25 ], [ 0, %35 ], [ 4, %37 ]
br label %41
41: ; preds = %15, %39, %6
%42 = phi i32 [ 0, %6 ], [ %40, %39 ], [ 2, %15 ]
ret i32 %42
}