#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_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, size_t);
char *strerror(int /*<<< orphan*/);
size_t strlen(char *);
char *get_line(FILE *f, char *filename) {
  size_t size = 0;
  size_t len = 0;
  size_t last = 0;
  char *buf = NULL;
  do {
    size += 2048;
    buf = realloc(buf, size);
    if (buf == NULL) {
      printf("FATAL ERROR: get_line: couldn't allocate %lu butes for buf\n",
             size);
      exit(EXIT_FAILURE);
    }
    if (fgets(buf + len, 2048, f) == (char *)0) {
      printf("FATAL ERROR: get_line failed to read line from file '%s': %s\n",
             filename, strerror(errno));
      exit(EXIT_FAILURE);
    }
    len = strlen(buf);
    last = len - 1;
  } while (!feof(f) && buf[last] != '\n');
  return buf;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    $0x800,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ea <get_line+0x7a>
mov    -0x18(%rbp),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x800,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    1243 <get_line+0xd3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x39(%rbp)
jne    128a <get_line+0x11a>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11a0 <get_line+0x30>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x800,%r12d
xor    %r13d,%r13d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <realloc@plt>
test   %rax,%rax
je     11ee <get_line+0x7e>
mov    %rax,%rbx
add    %rax,%r13
mov    %r13,%rdi
mov    $0x800,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1201 <get_line+0x91>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11e1 <get_line+0x71>
add    $0x800,%r12
cmpb   $0xa,-0x1(%r13,%rbx,1)
jne    1190 <get_line+0x20>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe0b(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1223 <get_line+0xb3>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe26(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [60 x i8] c"FATAL ERROR: get_line: couldn't allocate %lu butes for buf\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [62 x i8] c"FATAL ERROR: get_line failed to read line from file '%s': %s\0A\00"
@errno = global i32 0
define i8* @get_line(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i32* %0, i32** %3
store i8* %1, i8** %4
store i64 0, i64* %5
store i64 0, i64* %6
store i64 0, i64* %7
store i8* null, i8** %8
br label %9
9: ; preds = %52, %2
%10 = load i64, i64* %5
%11 = add i64 %10, 2048
store i64 %11, i64* %5
%12 = load i8*, i8** %8
%13 = load i64, i64* %5
%14 = call i8* @realloc(i8* %12, i64 %13)
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %9
%18 = load i64, i64* %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i64 %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %9
%23 = load i8*, i8** %8
%24 = load i64, i64* %6
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = load i32*, i32** %3
%27 = call i8* @fgets(i8* %25, i32 2048, i32* %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %36
29: ; preds = %22
%30 = load i8*, i8** %4
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %22
%37 = load i8*, i8** %8
%38 = call i64 @strlen(i8* %37)
store i64 %38, i64* %6
%39 = load i64, i64* %6
%40 = sub i64 %39, 1
store i64 %40, i64* %7
br label %41
41: ; preds = %36
%42 = load i32*, i32** %3
%43 = call i32 @feof(i32* %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %52, label %45
45: ; preds = %41
%46 = load i8*, i8** %8
%47 = load i64, i64* %7
%48 = getelementptr inbounds i8, i8* %46, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 10
br label %52
52: ; preds = %45, %41
%53 = phi i1 [ false, %41 ], [ %51, %45 ]
br i1 %53, label %9, label %54
54: ; preds = %52
%55 = load i8*, i8** %8
ret i8* %55
}

LLVM IR (-O2):
@.str = [60 x i8] c"FATAL ERROR: get_line: couldn't allocate %lu butes for buf\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [62 x i8] c"FATAL ERROR: get_line failed to read line from file '%s': %s\0A\00"
@errno = global i32 0
define i8* @get_line(i32* nocapture %0, i8* %1) {
br label %3
3: ; preds = %28, %2
%4 = phi i64 [ 0, %2 ], [ %7, %28 ]
%5 = phi i64 [ 0, %2 ], [ %25, %28 ]
%6 = phi i8* [ null, %2 ], [ %8, %28 ]
%7 = add i64 %4, 2048
%8 = tail call i8* @realloc(i8* %6, i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i64 %7)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %3
%15 = getelementptr inbounds i8, i8* %8, i64 %5
%16 = tail call i8* @fgets(i8* %15, i32 2048, i32* %0)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = tail call i64 @strlen(i8* %8)
%26 = tail call i32 @feof(i32* %0)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = add i64 %25, -1
%30 = getelementptr inbounds i8, i8* %8, i64 %29
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 10
br i1 %32, label %33, label %3
33: ; preds = %24, %28
ret i8* %8
}