#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*/ *fopen(char const *, char *);
size_t getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ printf(char *, char const *);
void readFile(const char *file) {
  FILE *fp;
  fp = fopen(file, "rt");
  if (fp == NULL) {
    printf("Cannot open file '%s'\n", file);
    exit(1);
  }
  size_t bytes_read;
  size_t nbytes = 8;
  char *line = (char *)malloc(nbytes + 1);
  while ((bytes_read = getline(&line, &nbytes, fp)) != -1) {
    printf("%s", line);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a5 <readFile+0x45>
mov    -0x8(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movq   $0x8,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <getline@plt>
mov    %rax,-0x18(%rbp)
cmp    $0xffffffffffffffff,%rax
je     11f2 <readFile+0x92>
mov    -0x28(%rbp),%rsi
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
jmp    11be <readFile+0x5e>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1205 <readFile+0xa5>
mov    %rax,%rbx
movq   $0x8,0x10(%rsp)
mov    $0x9,%edi
call   1050 <malloc@plt>
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   1070 <getline@plt>
cmp    $0xffffffffffffffff,%rax
je     11f1 <readFile+0x91>
lea    0xe5d(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1070 <getline@plt>
cmp    $0xffffffffffffffff,%rax
jne    11d0 <readFile+0x70>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf7(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
@.str.1 = [23 x i8] c"Cannot open file '%s'\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define void @readFile(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = bitcast i64* %2 to i8*
store i64 8, i64* %2
%11 = bitcast i8** %3 to i8*
%12 = tail call i64 @malloc(i64 9)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %3
%14 = call i64 @getline(i8** %3, i64* %2, i32* %4)
%15 = icmp eq i64 %14, -1
br i1 %15, label %21, label %16
16: ; preds = %9, %16
%17 = load i8*, i8** %3
%18 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = call i64 @getline(i8** %3, i64* %2, i32* %4)
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %16
21: ; preds = %16, %9
%22 = call i32 @fclose(i32* %4)
ret void
}