#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *fp;
  int LINE_MAX = 50;
  char line[LINE_MAX];
  fp = fopen("myfile.txt", "r");
  if (fp == NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }
  while (fgets(line, LINE_MAX, fp) != NULL) {
    printf("%s", line);
  }
  fclose(fp);
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x32,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe5d(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d6 <main+0x76>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11db <main+0x7b>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     120c <main+0xac>
mov    -0x30(%rbp),%rsi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11db <main+0x7b>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
lea    0xe80(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11fd <main+0x8d>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x32,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11e2 <main+0x72>
lea    0xe5d(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x32,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11c0 <main+0x50>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"myfile.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Cannot open file.\0A\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i32 0, i32* %1
store i32 50, i32* %3
%6 = load i32, i32* %3
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %4
%9 = alloca i8, i64 %7
store i64 %7, i64* %5
%10 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %2
%11 = load i32*, i32** %2
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
br label %17
17: ; preds = %22, %16
%18 = load i32, i32* %3
%19 = load i32*, i32** %2
%20 = call i32* @fgets(i8* %9, i32 %18, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %9)
br label %17
24: ; preds = %17
%25 = load i32*, i32** %2
%26 = call i32 @fclose(i32* %25)
%27 = call i32 (...) @getchar()
store i32 0, i32* %1
%28 = load i8*, i8** %4
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [11 x i8] c"myfile.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%s\00"
@str = [18 x i8] c"Cannot open file.\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i32* @fgets(i8* %2, i32 50, i32* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %15, label %11
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %5, %11
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%13 = call i32* @fgets(i8* %2, i32 50, i32* %3)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %11
15: ; preds = %11, %5
%16 = call i32 @fclose(i32* %3)
%17 = call i32 (...) @getchar()
ret i32 0
}