#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 */
char EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
char getc(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
const char *readLine(FILE *file) {
  if (file == NULL) {
    printf("Error: file pointer is null.");
    exit(1);
  }
  int maximumLineLength = 128;
  char *lineBuffer = (char *)malloc(sizeof(char) * maximumLineLength);
  if (lineBuffer == NULL) {
    printf("Error allocating memory for line buffer.");
    exit(1);
  }
  char ch = getc(file);
  int count = 0;
  while ((ch != '\n') && (ch != EOF)) {
    if (count == maximumLineLength) {
      maximumLineLength += 128;
      lineBuffer = realloc(lineBuffer, maximumLineLength);
      if (lineBuffer == NULL) {
        printf("Error reallocating space for line buffer.");
        exit(1);
      }
    }
    lineBuffer[count] = ch;
    count++;
    ch = getc(file);
  }
  lineBuffer[count] = '\0';
  char line[count + 1];
  strncpy(line, lineBuffer, (count + 1));
  free(lineBuffer);
  const char *constLine = line;
  return constLine;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119d <readLine+0x2d>
lea    0xe72(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x80,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <readLine+0x68>
lea    0xe54(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1090 <getc@plt>
mov    %al,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
movsbl -0x19(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x39(%rbp)
je     1213 <readLine+0xa3>
movsbl -0x19(%rbp),%eax
mov    0x2de0(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1223 <readLine+0xb3>
jmp    1298 <readLine+0x128>
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    1270 <readLine+0x100>
mov    -0xc(%rbp),%eax
add    $0x80,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    126b <readLine+0xfb>
lea    0xdea(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1270 <readLine+0x100>
mov    -0x19(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <getc@plt>
mov    %al,-0x19(%rbp)
jmp    11eb <readLine+0x7b>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     126a <readLine+0xfa>
mov    %rdi,%r15
mov    $0x80,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1273 <readLine+0x103>
mov    %rax,%r14
mov    %r15,%rdi
call   1090 <getc@plt>
xor    %ebx,%ebx
cmp    $0xa,%al
je     1212 <readLine+0xa2>
mov    %eax,%r12d
mov    0x2e30(%rip),%rax
cmp    (%rax),%r12b
je     1212 <readLine+0xa2>
mov    $0x80,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%eax
cmp    %rax,%rbx
jne    11ef <readLine+0x7f>
sub    $0xffffff80,%r13d
mov    %r14,%rdi
mov    %r13d,%esi
call   1070 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1254 <readLine+0xe4>
mov    %r12b,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1090 <getc@plt>
cmp    $0xa,%al
je     1212 <readLine+0xa2>
mov    %eax,%r12d
mov    0x2ddb(%rip),%rax
cmp    (%rax),%r12b
jne    11d0 <readLine+0x60>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
add    $0x1,%ebx
mov    %rsp,%r15
add    $0x10,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <strncpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdeb(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd8f(%rip),%rdi
jmp    125b <readLine+0xeb>
lea    0xda3(%rip),%rdi
jmp    125b <readLine+0xeb>

LLVM IR (-O0):
@.str = [29 x i8] c"Error: file pointer is null.\00"
@.str.1 = [41 x i8] c"Error allocating memory for line buffer.\00"
@EOF = global i8 0
@.str.2 = [42 x i8] c"Error reallocating space for line buffer.\00"
define i8* @readLine(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i32* %0, i32** %2
%10 = load i32*, i32** %2
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
store i32 128, i32* %3
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = mul i64 1, %17
%19 = trunc i64 %18 to i32
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %15
%28 = load i32*, i32** %2
%29 = call signext i8 @getc(i32* %28)
store i8 %29, i8* %5
store i32 0, i32* %6
br label %30
30: ; preds = %58, %27
%31 = load i8, i8* %5
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 10
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i8, i8* %5
%36 = sext i8 %35 to i32
%37 = load i8, i8* @EOF
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %36, %38
br label %40
40: ; preds = %34, %30
%41 = phi i1 [ false, %30 ], [ %39, %34 ]
br i1 %41, label %42, label %68
42: ; preds = %40
%43 = load i32, i32* %6
%44 = load i32, i32* %3
%45 = icmp eq i32 %43, %44
br i1 %45, label %46, label %58
46: ; preds = %42
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 128
store i32 %48, i32* %3
%49 = load i8*, i8** %4
%50 = load i32, i32* %3
%51 = call i8* @realloc(i8* %49, i32 %50)
store i8* %51, i8** %4
%52 = load i8*, i8** %4
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %46
br label %58
58: ; preds = %57, %42
%59 = load i8, i8* %5
%60 = load i8*, i8** %4
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 %59, i8* %63
%64 = load i32, i32* %6
%65 = add nsw i32 %64, 1
store i32 %65, i32* %6
%66 = load i32*, i32** %2
%67 = call signext i8 @getc(i32* %66)
store i8 %67, i8* %5
br label %30
68: ; preds = %40
%69 = load i8*, i8** %4
%70 = load i32, i32* %6
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 0, i8* %72
%73 = load i32, i32* %6
%74 = add nsw i32 %73, 1
%75 = zext i32 %74 to i64
%76 = call i8* @llvm.stacksave()
store i8* %76, i8** %7
%77 = alloca i8, i64 %75
store i64 %75, i64* %8
%78 = load i8*, i8** %4
%79 = load i32, i32* %6
%80 = add nsw i32 %79, 1
%81 = call i32 @strncpy(i8* %77, i8* %78, i32 %80)
%82 = load i8*, i8** %4
%83 = call i32 @free(i8* %82)
store i8* %77, i8** %9
%84 = load i8*, i8** %9
%85 = load i8*, i8** %7
ret i8* %84
}

LLVM IR (-O2):
@.str = [29 x i8] c"Error: file pointer is null.\00"
@.str.1 = [41 x i8] c"Error allocating memory for line buffer.\00"
@EOF = global i8 0
@.str.2 = [42 x i8] c"Error reallocating space for line buffer.\00"
define i8* @readLine(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %1
%7 = tail call i64 @malloc(i32 128)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6
%13 = inttoptr i64 %7 to i8*
%14 = tail call signext i8 @getc(i32* %0)
%15 = icmp ne i8 %14, 10
%16 = load i8, i8* @EOF
%17 = icmp ne i8 %14, %16
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %45
19: ; preds = %12, %33
%20 = phi i64 [ %37, %33 ], [ 0, %12 ]
%21 = phi i32 [ %35, %33 ], [ 128, %12 ]
%22 = phi i8* [ %34, %33 ], [ %13, %12 ]
%23 = phi i8 [ %38, %33 ], [ %14, %12 ]
%24 = zext i32 %21 to i64
%25 = icmp eq i64 %20, %24
br i1 %25, label %26, label %33
26: ; preds = %19
%27 = add nsw i32 %21, 128
%28 = tail call i8* @realloc(i8* %22, i32 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %26, %19
%34 = phi i8* [ %28, %26 ], [ %22, %19 ]
%35 = phi i32 [ %27, %26 ], [ %21, %19 ]
%36 = getelementptr inbounds i8, i8* %34, i64 %20
store i8 %23, i8* %36
%37 = add nuw i64 %20, 1
%38 = tail call signext i8 @getc(i32* %0)
%39 = icmp ne i8 %38, 10
%40 = load i8, i8* @EOF
%41 = icmp ne i8 %38, %40
%42 = select i1 %39, i1 %41, i1 false
br i1 %42, label %19, label %43
43: ; preds = %33
%44 = trunc i64 %37 to i32
br label %45
45: ; preds = %43, %12
%46 = phi i32 [ 0, %12 ], [ %44, %43 ]
%47 = phi i8* [ %13, %12 ], [ %34, %43 ]
%48 = zext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 0, i8* %49
%50 = add nuw nsw i32 %46, 1
%51 = zext i32 %50 to i64
%52 = alloca i8, i64 %51
%53 = call i32 @strncpy(i8* %52, i8* %47, i32 %50)
%54 = call i32 @free(i8* %47)
ret i8* %52
}