#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char *realloc(char *, size_t);
int /*<<< orphan*/ strchr(char *, char);
int /*<<< orphan*/ warnx(char *, size_t);
char *read_line(FILE *file) {
  size_t chunk = 8192;
  size_t bytes = chunk;
  char *line = malloc(bytes + 1);
  for (; !(line); exit(EXIT_FAILURE))
    warnx("malloc fail %lu", bytes + 1);
  line[0] = 0;
  while (!feof(file) && !ferror(file) &&
         fgets(line + bytes - chunk, chunk + 1, file) &&
         !strchr(line + bytes - chunk, '\n')) {
    bytes += chunk;
    line = realloc(line, bytes + 1);
    for (; !(line); exit(EXIT_FAILURE))
      warnx("realloc fail %lu", bytes + 1);
  }
  if (ferror(file) || (!line[0] && feof(file))) {
    free(line);
    line = NULL;
  }
  return line;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x2000,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d4 <read_line+0x44>
jmp    11f6 <read_line+0x66>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
lea    0xe1d(%rip),%rdi
call   1060 <warnx@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1080 <feof@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
jne    128d <read_line+0xfd>
mov    -0x8(%rbp),%rdi
call   1040 <ferror@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
jne    128d <read_line+0xfd>
mov    -0x20(%rbp),%rdi
add    -0x18(%rbp),%rdi
xor    %eax,%eax
sub    -0x10(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    -0x8(%rbp),%rdx
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x21(%rbp)
je     128d <read_line+0xfd>
mov    -0x20(%rbp),%rdi
add    -0x18(%rbp),%rdi
xor    %eax,%eax
sub    -0x10(%rbp),%rax
add    %rax,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    129d <read_line+0x10d>
jmp    12fc <read_line+0x16c>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   10a0 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12d5 <read_line+0x145>
jmp    12f7 <read_line+0x167>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
lea    0xd2c(%rip),%rdi
call   1060 <warnx@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    11fd <read_line+0x6d>
mov    -0x8(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
jne    132f <read_line+0x19f>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1340 <read_line+0x1b0>
mov    -0x8(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
je     1340 <read_line+0x1b0>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%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,%r14
mov    $0x2001,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1285 <read_line+0xf5>
mov    %rax,%r15
movb   $0x0,(%rax)
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1080 <feof@plt>
test   %rax,%rax
jne    1247 <read_line+0xb7>
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1247 <read_line+0xb7>
lea    (%r15,%r12,1),%rbx
mov    $0x2001,%esi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1247 <read_line+0xb7>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %eax,%eax
jne    1247 <read_line+0xb7>
lea    0x4001(%r12),%rsi
mov    %r15,%rdi
call   10a0 <realloc@plt>
mov    %rax,%r15
add    $0x2000,%r12
test   %rax,%rax
jne    11c0 <read_line+0x30>
add    $0x2001,%r12
lea    0xddf(%rip),%rdi
mov    %r12,%rsi
call   1060 <warnx@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1261 <read_line+0xd1>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
jmp    1267 <read_line+0xd7>
cmpb   $0x0,(%r15)
je     1276 <read_line+0xe6>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1080 <feof@plt>
test   %rax,%rax
jne    1254 <read_line+0xc4>
jmp    1267 <read_line+0xd7>
lea    0xd74(%rip),%rdi
mov    $0x2001,%esi
jmp    1234 <read_line+0xa4>

LLVM IR (-O0):
@.str = [16 x i8] c"malloc fail %lu\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"realloc fail %lu\00"
define i8* @read_line(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
store i32* %0, i32** %2
store i64 8192, i64* %3
%6 = load i64, i64* %3
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = add i64 %7, 1
%9 = call i8* @malloc(i64 %8)
store i8* %9, i8** %5
br label %10
10: ; preds = %1
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
%13 = xor i1 %12, true
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = load i64, i64* %4
%16 = add i64 %15, 1
%17 = call i32 @warnx(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %16)
br label %18
18: ; preds = %14
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
br label %24
24: ; preds = %75, %21
%25 = load i32*, i32** %2
%26 = call i64 @feof(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %54, label %28
28: ; preds = %24
%29 = load i32*, i32** %2
%30 = call i64 @ferror(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %54, label %32
32: ; preds = %28
%33 = load i8*, i8** %5
%34 = load i64, i64* %4
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = load i64, i64* %3
%37 = sub i64 0, %36
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i64, i64* %3
%40 = add i64 %39, 1
%41 = load i32*, i32** %2
%42 = call i64 @fgets(i8* %38, i64 %40, i32* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %32
%45 = load i8*, i8** %5
%46 = load i64, i64* %4
%47 = getelementptr inbounds i8, i8* %45, i64 %46
%48 = load i64, i64* %3
%49 = sub i64 0, %48
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = call i32 @strchr(i8* %50, i8 signext 10)
%52 = icmp ne i32 %51, 0
%53 = xor i1 %52, true
br label %54
54: ; preds = %44, %32, %28, %24
%55 = phi i1 [ false, %32 ], [ false, %28 ], [ false, %24 ], [ %53, %44 ]
br i1 %55, label %56, label %76
56: ; preds = %54
%57 = load i64, i64* %3
%58 = load i64, i64* %4
%59 = add i64 %58, %57
store i64 %59, i64* %4
%60 = load i8*, i8** %5
%61 = load i64, i64* %4
%62 = add i64 %61, 1
%63 = call i8* @realloc(i8* %60, i64 %62)
store i8* %63, i8** %5
br label %64
64: ; preds = %56
%65 = load i8*, i8** %5
%66 = icmp ne i8* %65, null
%67 = xor i1 %66, true
br i1 %67, label %68, label %75
68: ; preds = %64
%69 = load i64, i64* %4
%70 = add i64 %69, 1
%71 = call i32 @warnx(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %70)
br label %72
72: ; preds = %68
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %64
br label %24
76: ; preds = %54
%77 = load i32*, i32** %2
%78 = call i64 @ferror(i32* %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %89, label %80
80: ; preds = %76
%81 = load i8*, i8** %5
%82 = getelementptr inbounds i8, i8* %81, i64 0
%83 = load i8, i8* %82
%84 = icmp ne i8 %83, 0
br i1 %84, label %92, label %85
85: ; preds = %80
%86 = load i32*, i32** %2
%87 = call i64 @feof(i32* %86)
%88 = icmp ne i64 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %85, %76
%90 = load i8*, i8** %5
%91 = call i32 @free(i8* %90)
store i8* null, i8** %5
br label %92
92: ; preds = %89, %85, %80
%93 = load i8*, i8** %5
ret i8* %93
}

LLVM IR (-O2):
@.str = [16 x i8] c"malloc fail %lu\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [17 x i8] c"realloc fail %lu\00"
define i8* @read_line(i32* nocapture %0) {
%2 = tail call dereferenceable_or_null(8193) i8* @malloc(i64 8193)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = tail call i32 @warnx(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 8193)
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %1
store i8 0, i8* %2
br label %9
9: ; preds = %25, %8
%10 = phi i64 [ 8192, %8 ], [ %26, %25 ]
%11 = phi i8* [ %2, %8 ], [ %28, %25 ]
%12 = tail call i64 @feof(i32* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %34
14: ; preds = %9
%15 = tail call i64 @ferror(i32* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %11, i64 -8192
%19 = getelementptr inbounds i8, i8* %18, i64 %10
%20 = tail call i64 @fgets(i8* %19, i64 8193, i32* %0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %17
%23 = tail call i32 @strchr(i8* %19, i8 signext 10)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %22
%26 = add i64 %10, 8192
%27 = add i64 %10, 8193
%28 = tail call i8* @realloc(i8* %11, i64 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %9
30: ; preds = %25
%31 = tail call i32 @warnx(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %27)
%32 = load i32, i32* @EXIT_FAILURE
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %9, %14, %17, %22
%35 = tail call i64 @ferror(i32* %0)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i8, i8* %11
%39 = icmp eq i8 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = tail call i64 @feof(i32* %0)
%42 = icmp eq i64 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %40, %34
%44 = tail call i32 @free(i8* %11)
br label %45
45: ; preds = %43, %40, %37
%46 = phi i8* [ null, %43 ], [ %11, %37 ], [ %11, %40 ]
ret i8* %46
}