#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 EOF;
int /*<<< orphan*/ cmd_error(char *);
int fgetc(int /*<<< orphan*/ *);
char *realloc(char *, int);
char *get_line(FILE *fp) {
  static char *buf = NULL;
  static int len = 0;
  int i = 0, ch;
  while ((ch = fgetc(fp)) != EOF) {
    if (i == len) {
      len += 4096;
      if ((buf = realloc(buf, len)) == NULL) {
        cmd_error("out of memory\n");
      }
    }
    buf[i++] = ch;
    if (ch == '\n') {
      break;
    }
  }
  if (i == 0) {
    return NULL;
  }
  buf[i] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%eax
je     11e3 <get_line+0xb3>
mov    -0x14(%rbp),%eax
cmp    0x2ee1(%rip),%eax
jne    11b2 <get_line+0x82>
mov    0x2ed5(%rip),%eax
add    $0x1000,%eax
mov    %eax,0x2eca(%rip)
mov    0x2ebb(%rip),%rdi
mov    0x2ebd(%rip),%esi
call   1040 <realloc@plt>
mov    %rax,0x2ea9(%rip)
cmp    $0x0,%rax
jne    11ad <get_line+0x7d>
lea    0xe58(%rip),%rdi
call   1050 <cmd_error@plt>
jmp    11b2 <get_line+0x82>
mov    -0x18(%rbp),%eax
mov    %al,%dl
mov    0x2e82(%rip),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0xa,-0x18(%rbp)
jne    11de <get_line+0xae>
jmp    11e3 <get_line+0xb3>
jmp    1143 <get_line+0x13>
cmpl   $0x0,-0x14(%rbp)
jne    11fa <get_line+0xca>
movq   $0x0,-0x8(%rbp)
jmp    1214 <get_line+0xe4>
mov    0x2e3f(%rip),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e30(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
xor    %ebx,%ebx
mov    0x2ea4(%rip),%r12
lea    0xeb5(%rip),%r14
jmp    1164 <get_line+0x34>
nopl   (%rax)
mov    0x2ee9(%rip),%rax
mov    %bpl,(%rax,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%ebp
je     11b8 <get_line+0x88>
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    (%r12),%eax
je     11ad <get_line+0x7d>
mov    %eax,%ebp
mov    0x2ece(%rip),%esi
cmp    %rsi,%rbx
jne    1150 <get_line+0x20>
add    $0x1000,%esi
mov    %esi,0x2ebd(%rip)
mov    0x2eae(%rip),%rdi
call   1040 <realloc@plt>
mov    %rax,0x2ea2(%rip)
test   %rax,%rax
jne    1150 <get_line+0x20>
mov    %r14,%rdi
call   1050 <cmd_error@plt>
jmp    1150 <get_line+0x20>
test   %ebx,%ebx
je     11c1 <get_line+0x91>
mov    0x2e88(%rip),%rax
movslq %ebx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11c3 <get_line+0x93>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@get_line.buf = internal global i8* null
@get_line.len = internal global i32 0
@EOF = global i32 0
@.str = [15 x i8] c"out of memory\0A\00"
define i8* @get_line(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 0, i32* %4
br label %6
6: ; preds = %36, %1
%7 = load i32*, i32** %3
%8 = call i32 @fgetc(i32* %7)
store i32 %8, i32* %5
%9 = load i32, i32* @EOF
%10 = icmp ne i32 %8, %9
br i1 %10, label %11, label %37
11: ; preds = %6
%12 = load i32, i32* %4
%13 = load i32, i32* @get_line.len
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %11
%16 = load i32, i32* @get_line.len
%17 = add nsw i32 %16, 4096
store i32 %17, i32* @get_line.len
%18 = load i8*, i8** @get_line.buf
%19 = load i32, i32* @get_line.len
%20 = call i8* @realloc(i8* %18, i32 %19)
store i8* %20, i8** @get_line.buf
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @cmd_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
br label %25
25: ; preds = %24, %11
%26 = load i32, i32* %5
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** @get_line.buf
%29 = load i32, i32* %4
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 %27, i8* %32
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 10
br i1 %34, label %35, label %36
35: ; preds = %25
br label %37
36: ; preds = %25
br label %6
37: ; preds = %35, %6
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %37
store i8* null, i8** %2
br label %47
41: ; preds = %37
%42 = load i8*, i8** @get_line.buf
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** @get_line.buf
store i8* %46, i8** %2
br label %47
47: ; preds = %41, %40
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@get_line.buf = internal unnamed_addr global i8* null
@get_line.len = internal unnamed_addr global i32 0
@EOF = global i32 0
@.str = [15 x i8] c"out of memory\0A\00"
define i8* @get_line(i32* nocapture %0) {
br label %2
2: ; preds = %18, %1
%3 = phi i64 [ %21, %18 ], [ 0, %1 ]
%4 = tail call i32 @fgetc(i32* %0)
%5 = load i32, i32* @EOF
%6 = icmp eq i32 %4, %5
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = load i32, i32* @get_line.len
%9 = zext i32 %8 to i64
%10 = icmp eq i64 %3, %9
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = add nsw i32 %8, 4096
store i32 %12, i32* @get_line.len
%13 = load i8*, i8** @get_line.buf
%14 = tail call i8* @realloc(i8* %13, i32 %12)
store i8* %14, i8** @get_line.buf
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @cmd_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %11, %16, %7
%19 = trunc i32 %4 to i8
%20 = load i8*, i8** @get_line.buf
%21 = add nuw i64 %3, 1
%22 = getelementptr inbounds i8, i8* %20, i64 %3
store i8 %19, i8* %22
%23 = icmp eq i32 %4, 10
br i1 %23, label %29, label %2
24: ; preds = %2
%25 = trunc i64 %3 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %24
%28 = load i8*, i8** @get_line.buf
br label %29
29: ; preds = %18, %27
%30 = phi i8* [ %28, %27 ], [ %20, %18 ]
%31 = phi i64 [ %3, %27 ], [ %21, %18 ]
%32 = shl i64 %31, 32
%33 = ashr exact i64 %32, 32
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %24, %29
%36 = phi i8* [ %30, %29 ], [ null, %24 ]
ret i8* %36
}