#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 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, "readToken");
    }
    buffer[index] = ch;
    ++index;
    ch = fgetc(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    0x2e8f(%rip),%rcx
cmp    (%rcx),%eax
jne    116e <readLine+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    1218 <readLine+0xe8>
mov    -0x24(%rbp),%edi
lea    0xe88(%rip),%rsi
call   1030 <allocateMsg@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x14(%rbp)
je     1204 <readLine+0xd4>
mov    -0x14(%rbp),%eax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%eax
jne    11a9 <readLine+0x79>
jmp    1204 <readLine+0xd4>
mov    -0x18(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jle    11da <readLine+0xaa>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    0xe38(%rip),%rdx
call   1050 <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    1188 <readLine+0x58>
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    0x2e9e(%rip),%r13
cmp    0x0(%r13),%eax
jne    1155 <readLine+0x25>
xor    %r15d,%r15d
jmp    11c6 <readLine+0x96>
mov    %eax,%ebp
lea    0xea2(%rip),%rsi
mov    $0x200,%edi
call   1030 <allocateMsg@plt>
mov    %rax,%r15
xor    %eax,%eax
cmp    $0xa,%ebp
je     11c1 <readLine+0x91>
cmp    0x0(%r13),%ebp
je     11c1 <readLine+0x91>
mov    $0x200,%ebx
xor    %r12d,%r12d
lea    -0x2(%rbx),%eax
cltq
cmp    %rax,%r12
jle    11a1 <readLine+0x71>
add    $0x1,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
lea    0xe70(%rip),%rdx
call   1050 <reallocateMsg@plt>
mov    %rax,%r15
mov    %bpl,(%r15,%r12,1)
add    $0x1,%r12
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    $0xa,%eax
je     11be <readLine+0x8e>
mov    %eax,%ebp
cmp    0x0(%r13),%eax
jne    1180 <readLine+0x50>
mov    %r12d,%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"
@.str.1 = [10 x i8] c"readToken\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 %53
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 ([10 x i8], [10 x i8]* @.str.1, 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 i8*, i8** %6
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %6
store i8* %52, i8** %2
br label %53
53: ; preds = %47, %13
%54 = load i8*, i8** %2
ret i8* %54
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [9 x i8] c"readLine\00"
@.str.1 = [10 x i8] c"readToken\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 %39, 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 %35, 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 ([10 x i8], [10 x i8]* @.str.1, 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 = and i64 %27, 4294967295
br label %35
35: ; preds = %33, %5
%36 = phi i64 [ 0, %5 ], [ %34, %33 ]
%37 = phi i8* [ %6, %5 ], [ %23, %33 ]
%38 = getelementptr inbounds i8, i8* %37, i64 %36
store i8 0, i8* %38
br label %39
39: ; preds = %1, %35
%40 = phi i8* [ %37, %35 ], [ null, %1 ]
ret i8* %40
}