#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;
char *allocateMsg(int, char *);
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
char *reallocateMsg(char *, int, char *);
char *readLine(FILE *fp) {
  int ch, index;
  char *buffer;
  int size = 512;
  ch = fgetc(fp);
  if (ch == EOF)
    return 0;
  buffer = allocateMsg(size, "readLine");
  index = 0;
  while (ch != '\n') {
    if (ch == EOF)
      break;
    if (index > size - 2) {
      ++size;
      buffer = reallocateMsg(buffer, size, "readLine");
    }
    buffer[index] = ch;
    ++index;
    ch = fgetc(fp);
  }
  if (index > 0)
    clearerr(fp);
  buffer[index] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x200,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e7f(%rip),%rcx
cmp    (%rcx),%eax
jne    117e <readLine+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    123b <readLine+0xfb>
mov    -0x24(%rbp),%edi
lea    0xe78(%rip),%rsi
call   1030 <allocateMsg@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x14(%rbp)
je     1214 <readLine+0xd4>
mov    -0x14(%rbp),%eax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%eax
jne    11b9 <readLine+0x79>
jmp    1214 <readLine+0xd4>
mov    -0x18(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jle    11ea <readLine+0xaa>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    0xe1f(%rip),%rdx
call   1060 <reallocateMsg@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x14(%rbp)
jmp    1198 <readLine+0x58>
cmpl   $0x0,-0x18(%rbp)
jle    1227 <readLine+0xe7>
mov    -0x10(%rbp),%rdi
call   1050 <clearerr@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <fgetc@plt>
mov    0x2e8e(%rip),%r13
cmp    0x0(%r13),%eax
jne    1168 <readLine+0x28>
xor    %r15d,%r15d
jmp    11f7 <readLine+0xb7>
mov    %eax,%ebp
lea    0xe8f(%rip),%rsi
mov    $0x200,%edi
call   1030 <allocateMsg@plt>
mov    %rax,%r15
xor    %eax,%eax
cmp    $0xa,%ebp
je     11f2 <readLine+0xb2>
cmp    0x0(%r13),%ebp
je     11f2 <readLine+0xb2>
mov    $0x200,%ebx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    -0x2(%rbx),%eax
cltq
cmp    %rax,%r12
jle    11c1 <readLine+0x81>
add    $0x1,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
lea    0xe47(%rip),%rdx
call   1060 <reallocateMsg@plt>
mov    %rax,%r15
mov    %bpl,(%r15,%r12,1)
add    $0x1,%r12
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    $0xa,%eax
je     11de <readLine+0x9e>
mov    %eax,%ebp
cmp    0x0(%r13),%eax
jne    11a0 <readLine+0x60>
test   %r12d,%r12d
je     11f0 <readLine+0xb0>
mov    %r14,%rdi
call   1050 <clearerr@plt>
mov    %r12d,%eax
jmp    11f2 <readLine+0xb2>
xor    %eax,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@.str = [9 x i8] c"readLine\00"
define i8* @readLine(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32* %0, i32** %3
store i32 512, i32* %7
%8 = load i32*, i32** %3
%9 = call i32 @fgetc(i32* %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %59
14: ; preds = %1
%15 = load i32, i32* %7
%16 = call i8* @allocateMsg(i32 %15, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %6
store i32 0, i32* %5
br label %17
17: ; preds = %36, %14
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 10
br i1 %19, label %20, label %47
20: ; preds = %17
%21 = load i32, i32* %4
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %47
25: ; preds = %20
%26 = load i32, i32* %5
%27 = load i32, i32* %7
%28 = sub nsw i32 %27, 2
%29 = icmp sgt i32 %26, %28
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
%33 = load i8*, i8** %6
%34 = load i32, i32* %7
%35 = call i8* @reallocateMsg(i8* %33, i32 %34, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %35, i8** %6
br label %36
36: ; preds = %30, %25
%37 = load i32, i32* %4
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %6
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 %38, i8* %42
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
%45 = load i32*, i32** %3
%46 = call i32 @fgetc(i32* %45)
store i32 %46, i32* %4
br label %17
47: ; preds = %24, %17
%48 = load i32, i32* %5
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32*, i32** %3
%52 = call i32 @clearerr(i32* %51)
br label %53
53: ; preds = %50, %47
%54 = load i8*, i8** %6
%55 = load i32, i32* %5
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 0, i8* %57
%58 = load i8*, i8** %6
store i8* %58, i8** %2
br label %59
59: ; preds = %53, %13
%60 = load i8*, i8** %2
ret i8* %60
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [9 x i8] c"readLine\00"
define i8* @readLine(i32* nocapture %0) {
%2 = tail call i32 @fgetc(i32* %0)
%3 = load i32, i32* @EOF
%4 = icmp eq i32 %2, %3
br i1 %4, label %43, label %5
5: ; preds = %1
%6 = tail call i8* @allocateMsg(i32 512, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32 %2, 10
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %2, %8
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %39, label %11
11: ; preds = %5, %22
%12 = phi i64 [ %27, %22 ], [ 0, %5 ]
%13 = phi i32 [ %24, %22 ], [ 512, %5 ]
%14 = phi i8* [ %23, %22 ], [ %6, %5 ]
%15 = phi i32 [ %28, %22 ], [ %2, %5 ]
%16 = add nsw i32 %13, -2
%17 = sext i32 %16 to i64
%18 = icmp sgt i64 %12, %17
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = add nsw i32 %13, 1
%21 = tail call i8* @reallocateMsg(i8* %14, i32 %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %11
%23 = phi i8* [ %21, %19 ], [ %14, %11 ]
%24 = phi i32 [ %20, %19 ], [ %13, %11 ]
%25 = trunc i32 %15 to i8
%26 = getelementptr inbounds i8, i8* %23, i64 %12
store i8 %25, i8* %26
%27 = add nuw i64 %12, 1
%28 = tail call i32 @fgetc(i32* %0)
%29 = icmp eq i32 %28, 10
%30 = load i32, i32* @EOF
%31 = icmp eq i32 %28, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %33, label %11
33: ; preds = %22
%34 = trunc i64 %27 to i32
%35 = icmp eq i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = tail call i32 @clearerr(i32* %0)
%38 = and i64 %27, 4294967295
br label %39
39: ; preds = %5, %36, %33
%40 = phi i8* [ %23, %36 ], [ %23, %33 ], [ %6, %5 ]
%41 = phi i64 [ %38, %36 ], [ 0, %33 ], [ 0, %5 ]
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %1, %39
%44 = phi i8* [ %40, %39 ], [ null, %1 ]
ret i8* %44
}