#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*/ error(char *);
int fgetc(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
char *realloc(char *, int);
char *read_line(FILE *file) {
  char *line;
  int buffer_sizes, total_read, this_lb;
  if (file == NULL)
    error("reading line from null file pointer");
  buffer_sizes = 1;
  if ((line = (char *)malloc(128 * buffer_sizes * sizeof(char))) == NULL)
    error("allocating memory for line");
  total_read = 0;
  this_lb = 0;
  int c;
  while ((c = fgetc(file)) != EOF) {
    if (this_lb == 127) {
      if ((line = realloc(line, ((++buffer_sizes) * 128 * sizeof(char)))) ==
          NULL)
        error("reallocating memory for line");
      this_lb = 0;
    }
    if ((char)c == '\n') {
      line[total_read] = '\0';
      return line;
    }
    line[total_read] = (char)c;
    total_read++;
  }
  if (total_read == 0)
    return NULL;
  line[total_read] = '\0';
  return line;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <read_line+0x23>
lea    0xea2(%rip),%rdi
call   1060 <error@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x7,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1197 <read_line+0x57>
lea    0xe94(%rip),%rdi
call   1060 <error@plt>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
je     124e <read_line+0x10e>
cmpl   $0x7f,-0x24(%rbp)
jne    1208 <read_line+0xc8>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
shl    $0x7,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1201 <read_line+0xc1>
lea    0xe28(%rip),%rdi
call   1060 <error@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x28(%rbp),%eax
movsbl %al,%eax
cmp    $0xa,%eax
jne    1230 <read_line+0xf0>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1279 <read_line+0x139>
mov    -0x28(%rbp),%eax
mov    %al,%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)
jmp    11a5 <read_line+0x65>
cmpl   $0x0,-0x20(%rbp)
jne    1265 <read_line+0x125>
movq   $0x0,-0x8(%rbp)
jmp    1279 <read_line+0x139>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
test   %rdi,%rdi
jne    114c <read_line+0x1c>
lea    0xeb9(%rip),%rdi
call   1050 <error@plt>
mov    $0x80,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    116a <read_line+0x3a>
lea    0xebf(%rip),%rdi
call   1050 <error@plt>
mov    %r15,%rdi
call   1030 <fgetc@plt>
mov    0x2e6f(%rip),%r12
cmp    (%r12),%eax
jne    1184 <read_line+0x54>
xor    %r14d,%r14d
jmp    11b5 <read_line+0x85>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
cmp    $0xa,%al
je     11ae <read_line+0x7e>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    (%r12),%eax
jne    1190 <read_line+0x60>
test   %ebx,%ebx
je     117f <read_line+0x4f>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [36 x i8] c"reading line from null file pointer\00"
@.str.1 = [27 x i8] c"allocating memory for line\00"
@EOF = global i32 0
@.str.2 = [29 x i8] c"reallocating memory for line\00"
define i8* @read_line(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
store i32 1, i32* %5
%14 = load i32, i32* %5
%15 = mul nsw i32 128, %14
%16 = sext i32 %15 to i64
%17 = mul i64 %16, 1
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %4
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %13
store i32 0, i32* %6
store i32 0, i32* %7
br label %25
25: ; preds = %57, %24
%26 = load i32*, i32** %3
%27 = call i32 @fgetc(i32* %26)
store i32 %27, i32* %8
%28 = load i32, i32* @EOF
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %66
30: ; preds = %25
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 127
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load i8*, i8** %4
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
%37 = mul nsw i32 %36, 128
%38 = sext i32 %37 to i64
%39 = mul i64 %38, 1
%40 = trunc i64 %39 to i32
%41 = call i8* @realloc(i8* %34, i32 %40)
store i8* %41, i8** %4
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %33
%44 = call i32 @error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %33
store i32 0, i32* %7
br label %46
46: ; preds = %45, %30
%47 = load i32, i32* %8
%48 = trunc i32 %47 to i8
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 10
br i1 %50, label %51, label %57
51: ; preds = %46
%52 = load i8*, i8** %4
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
%56 = load i8*, i8** %4
store i8* %56, i8** %2
br label %76
57: ; preds = %46
%58 = load i32, i32* %8
%59 = trunc i32 %58 to i8
%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
br label %25
66: ; preds = %25
%67 = load i32, i32* %6
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %66
store i8* null, i8** %2
br label %76
70: ; preds = %66
%71 = load i8*, i8** %4
%72 = load i32, i32* %6
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
store i8 0, i8* %74
%75 = load i8*, i8** %4
store i8* %75, i8** %2
br label %76
76: ; preds = %70, %69, %51
%77 = load i8*, i8** %2
ret i8* %77
}

LLVM IR (-O2):
@.str = [36 x i8] c"reading line from null file pointer\00"
@.str.1 = [27 x i8] c"allocating memory for line\00"
@EOF = global i32 0
define i8* @read_line(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %3, %1
%6 = tail call i64 @malloc(i32 128)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %5
%12 = tail call i32 @fgetc(i32* %0)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %34, label %15
15: ; preds = %11, %20
%16 = phi i64 [ %23, %20 ], [ 0, %11 ]
%17 = phi i32 [ %24, %20 ], [ %12, %11 ]
%18 = and i32 %17, 255
%19 = icmp eq i32 %18, 10
br i1 %19, label %30, label %20
20: ; preds = %15
%21 = trunc i32 %17 to i8
%22 = getelementptr inbounds i8, i8* %7, i64 %16
store i8 %21, i8* %22
%23 = add nuw i64 %16, 1
%24 = tail call i32 @fgetc(i32* %0)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %15
27: ; preds = %20
%28 = trunc i64 %23 to i32
%29 = icmp eq i32 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %15, %27
%31 = phi i64 [ %23, %27 ], [ %16, %15 ]
%32 = and i64 %31, 4294967295
%33 = getelementptr inbounds i8, i8* %7, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %30, %11, %27
%35 = phi i8* [ null, %27 ], [ null, %11 ], [ %7, %30 ]
ret i8* %35
}