#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
size_t grepline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, size_t, char *);
int /*<<< orphan*/ puts(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  char *line = NULL;
  size_t length = 0;
  size_t read_length;
  if (argc != 2) {
    puts("Please give a file");
    exit(1);
  }
  fp = fopen(argv[1], "r");
  if (fp == NULL) {
    puts("Empty file");
    exit(1);
  }
  while (!feof(fp)) {
    read_length = grepline(&line, &length, fp);
    printf("Read line of length %ld:\n%s", read_length, line);
  }
  if (line) {
    free(line);
  }
  fclose(fp);
  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)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c6 <main+0x46>
lea    0xe49(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ff <main+0x7f>
lea    0xe25(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1204 <main+0x84>
mov    -0x18(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1222 <main+0xa2>
jmp    1250 <main+0xd0>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <grepline@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdda(%rip),%rdi
call   1060 <printf@plt>
jmp    1204 <main+0x84>
cmpq   $0x0,-0x20(%rbp)
je     1264 <main+0xe4>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
cmp    $0x2,%edi
jne    122f <main+0xaf>
mov    0x8(%rsi),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1238 <main+0xb8>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    1219 <main+0x99>
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%r15
lea    0xe41(%rip),%r12
nop
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <grepline@plt>
mov    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     11e0 <main+0x60>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1219 <main+0x99>
call   1030 <free@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdca(%rip),%rdi
jmp    123f <main+0xbf>
lea    0xdd6(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Please give a file\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"Empty file\00"
@.str.3 = [28 x i8] c"Read line of length %ld:\0A%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i64 0, i64* %8
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 @puts(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
br label %26
26: ; preds = %31, %25
%27 = load i32*, i32** %6
%28 = call i32 @feof(i32* %27)
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br i1 %30, label %31, label %37
31: ; preds = %26
%32 = load i32*, i32** %6
%33 = call i64 @grepline(i8** %7, i64* %8, i32* %32)
store i64 %33, i64* %9
%34 = load i64, i64* %9
%35 = load i8*, i8** %7
%36 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %34, i8* %35)
br label %26
37: ; preds = %26
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %7
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %40, %37
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"Please give a file\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"Empty file\00"
@.str.3 = [28 x i8] c"Read line of length %ld:\0A%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i32 @feof(i32* %14)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %33
19: ; preds = %11
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16, %22
%23 = call i64 @grepline(i8** %3, i64* %4, i32* %14)
%24 = load i8*, i8** %3
%25 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %23, i8* %24)
%26 = call i32 @feof(i32* %14)
%27 = icmp eq i32 %26, 0
br i1 %27, label %22, label %28
28: ; preds = %22
%29 = load i8*, i8** %3
%30 = icmp eq i8* %29, null
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @free(i8* %29)
br label %33
33: ; preds = %16, %31, %28
%34 = call i32 @fclose(i32* %14)
ret i32 0
}