#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 */
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/ *);
int getpagesize();
scalar_t__ high_water_alloc(void **, size_t *, int);
char *strrchr(char *, char);
char *next_line(FILE *file) {
  static char *line;
  static size_t line_size;
  char *c;
  int eol = 0;
  if (!line) {
    if (high_water_alloc((void **)&line, &line_size, getpagesize()))
      return NULL;
  }
  c = line;
  do {
    if (!fgets(c, line_size - (c - line), file))
      return NULL;
    c = strrchr(c, '\0');
    while (c > line && (*(c - 1) == '\n' || *(c - 1) == '\r')) {
      c--;
      *c = '\0';
      eol = 1;
    }
    if (feof(file))
      break;
    if (!eol) {
      if (high_water_alloc((void **)&line, &line_size, 2 * line_size))
        return NULL;
      c = strrchr(line, '\0');
    }
  } while (!eol);
  return line;
}


x86 assembly (clang, -O0):
<next_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,0x2ee5(%rip)
jne    11a9 <next_line+0x59>
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %eax,%edx
lea    0x2ecf(%rip),%rdi
lea    0x2ed0(%rip),%rsi
call   1060 <high_water_alloc@plt>
cmp    $0x0,%rax
je     11a4 <next_line+0x54>
movq   $0x0,-0x8(%rbp)
jmp    12fc <next_line+0x1ac>
jmp    11a9 <next_line+0x59>
mov    0x2ea0(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e99(%rip),%rsi
mov    -0x18(%rbp),%rax
mov    0x2e86(%rip),%rcx
sub    %rcx,%rax
sub    %rax,%rsi
mov    -0x10(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    11ef <next_line+0x9f>
movq   $0x0,-0x8(%rbp)
jmp    12fc <next_line+0x1ac>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    0x2e45(%rip),%rcx
mov    %al,-0x1d(%rbp)
jbe    1241 <next_line+0xf1>
mov    -0x18(%rbp),%rax
movsbl -0x1(%rax),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x1e(%rbp)
je     123b <next_line+0xeb>
mov    -0x18(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xd,%eax
sete   %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1251 <next_line+0x101>
jmp    1270 <next_line+0x120>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x1c(%rbp)
jmp    11fe <next_line+0xae>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     1288 <next_line+0x138>
jmp    12f1 <next_line+0x1a1>
cmpl   $0x0,-0x1c(%rbp)
jne    12db <next_line+0x18b>
mov    0x2dbf(%rip),%rax
shl    $0x1,%rax
mov    %eax,%edx
lea    0x2daa(%rip),%rdi
lea    0x2dab(%rip),%rsi
call   1060 <high_water_alloc@plt>
cmp    $0x0,%rax
je     12c9 <next_line+0x179>
movq   $0x0,-0x8(%rbp)
jmp    12fc <next_line+0x1ac>
mov    0x2d80(%rip),%rdi
xor    %esi,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x18(%rbp)
jmp    12e0 <next_line+0x190>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b4 <next_line+0x64>
mov    0x2d58(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<next_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
mov    0x2eeb(%rip),%rbx
test   %rbx,%rbx
je     1253 <next_line+0x103>
mov    0x2ee3(%rip),%rsi
mov    %rbx,%rdi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     1283 <next_line+0x133>
lea    0x2ec9(%rip),%r15
lea    0x2eba(%rip),%r12
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %rbx,%rax
mov    $0x1,%bl
cmp    0x2ea3(%rip),%rax
jbe    11e1 <next_line+0x91>
mov    $0x1,%bl
jmp    11d3 <next_line+0x83>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movb   $0x0,-0x1(%rax)
add    $0xffffffffffffffff,%rax
xor    %ebx,%ebx
cmp    0x2e7f(%rip),%rax
jbe    1242 <next_line+0xf2>
movzbl -0x1(%rax),%ecx
cmp    $0xd,%cl
je     11c0 <next_line+0x70>
cmp    $0xa,%cl
je     11c0 <next_line+0x70>
mov    %r13,%rdi
call   1050 <feof@plt>
test   $0x1,%bl
je     124a <next_line+0xfa>
test   %rax,%rax
jne    124a <next_line+0xfa>
mov    0x2e5f(%rip),%edx
add    %edx,%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <high_water_alloc@plt>
test   %rax,%rax
jne    1286 <next_line+0x136>
mov    0x2e3e(%rip),%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
add    %rbp,%rbx
mov    0x2e31(%rip),%rsi
sub    %rbx,%rsi
add    %rbp,%rsi
mov    %rbx,%rdi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
jne    1199 <next_line+0x49>
jmp    1286 <next_line+0x136>
mov    %r13,%rdi
call   1050 <feof@plt>
mov    0x2dff(%rip),%r14
jmp    1286 <next_line+0x136>
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <getpagesize@plt>
lea    0x2dec(%rip),%rdi
lea    0x2ded(%rip),%rsi
mov    %eax,%edx
call   1060 <high_water_alloc@plt>
test   %rax,%rax
jne    1286 <next_line+0x136>
mov    0x2dd2(%rip),%rbx
jmp    116e <next_line+0x1e>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@next_line.line = internal global i8* null
@next_line.line_size = internal global i64 0
define i8* @next_line(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i32* %0, i32** %3
store i32 0, i32* %5
%6 = load i8*, i8** @next_line.line
%7 = icmp ne i8* %6, null
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = call i32 (...) @getpagesize()
%10 = call i64 @high_water_alloc(i8** @next_line.line, i64* @next_line.line_size, i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
store i8* null, i8** %2
br label %81
13: ; preds = %8
br label %14
14: ; preds = %13, %1
%15 = load i8*, i8** @next_line.line
store i8* %15, i8** %4
br label %16
16: ; preds = %75, %14
%17 = load i8*, i8** %4
%18 = load i64, i64* @next_line.line_size
%19 = load i8*, i8** %4
%20 = load i8*, i8** @next_line.line
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %20 to i64
%23 = sub i64 %21, %22
%24 = sub i64 %18, %23
%25 = load i32*, i32** %3
%26 = call i32 @fgets(i8* %17, i64 %24, i32* %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %16
store i8* null, i8** %2
br label %81
29: ; preds = %16
%30 = load i8*, i8** %4
%31 = call i8* @strrchr(i8* %30, i8 signext 0)
store i8* %31, i8** %4
br label %32
32: ; preds = %52, %29
%33 = load i8*, i8** %4
%34 = load i8*, i8** @next_line.line
%35 = icmp ugt i8* %33, %34
br i1 %35, label %36, label %50
36: ; preds = %32
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 -1
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %48, label %42
42: ; preds = %36
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i64 -1
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 13
br label %48
48: ; preds = %42, %36
%49 = phi i1 [ true, %36 ], [ %47, %42 ]
br label %50
50: ; preds = %48, %32
%51 = phi i1 [ false, %32 ], [ %49, %48 ]
br i1 %51, label %52, label %56
52: ; preds = %50
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i32 -1
store i8* %54, i8** %4
%55 = load i8*, i8** %4
store i8 0, i8* %55
store i32 1, i32* %5
br label %32
56: ; preds = %50
%57 = load i32*, i32** %3
%58 = call i64 @feof(i32* %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %56
br label %79
61: ; preds = %56
%62 = load i32, i32* %5
%63 = icmp ne i32 %62, 0
br i1 %63, label %74, label %64
64: ; preds = %61
%65 = load i64, i64* @next_line.line_size
%66 = mul i64 2, %65
%67 = trunc i64 %66 to i32
%68 = call i64 @high_water_alloc(i8** @next_line.line, i64* @next_line.line_size, i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %64
store i8* null, i8** %2
br label %81
71: ; preds = %64
%72 = load i8*, i8** @next_line.line
%73 = call i8* @strrchr(i8* %72, i8 signext 0)
store i8* %73, i8** %4
br label %74
74: ; preds = %71, %61
br label %75
75: ; preds = %74
%76 = load i32, i32* %5
%77 = icmp ne i32 %76, 0
%78 = xor i1 %77, true
br i1 %78, label %16, label %79
79: ; preds = %75, %60
%80 = load i8*, i8** @next_line.line
store i8* %80, i8** %2
br label %81
81: ; preds = %79, %70, %28, %12
%82 = load i8*, i8** %2
ret i8* %82
}

LLVM IR (-O2):
@next_line.line = internal global i8* null
@next_line.line_size = internal global i64 0
define i8* @next_line(i32* nocapture %0) {
%2 = load i8*, i8** @next_line.line
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %10
4: ; preds = %1
%5 = tail call i32 (...) @getpagesize()
%6 = tail call i64 @high_water_alloc(i8** @next_line.line, i64* @next_line.line_size, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %55
8: ; preds = %4
%9 = load i8*, i8** @next_line.line
br label %10
10: ; preds = %8, %1
%11 = phi i8* [ %9, %8 ], [ %2, %1 ]
%12 = load i64, i64* @next_line.line_size
%13 = tail call i32 @fgets(i8* %11, i64 %12, i32* %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %55, label %15
15: ; preds = %10, %42
%16 = phi i8* [ %45, %42 ], [ %11, %10 ]
%17 = tail call i64 @strlen(i8* dereferenceable(1) %16)
%18 = getelementptr i8, i8* %16, i64 %17
%19 = load i8*, i8** @next_line.line
%20 = icmp ugt i8* %18, %19
br i1 %20, label %21, label %31
21: ; preds = %15, %26
%22 = phi i1 [ false, %26 ], [ true, %15 ]
%23 = phi i8* [ %24, %26 ], [ %18, %15 ]
%24 = getelementptr inbounds i8, i8* %23, i64 -1
%25 = load i8, i8* %24
switch i8 %25, label %31 [
i8 10, label %26
i8 13, label %26
]
26: ; preds = %21, %21
store i8 0, i8* %24
%27 = load i8*, i8** @next_line.line
%28 = icmp ugt i8* %24, %27
br i1 %28, label %21, label %29
29: ; preds = %26
%30 = tail call i64 @feof(i32* %0)
br label %53
31: ; preds = %21, %15
%32 = phi i1 [ true, %15 ], [ %22, %21 ]
%33 = tail call i64 @feof(i32* %0)
%34 = icmp eq i64 %33, 0
%35 = and i1 %32, %34
br i1 %35, label %36, label %53
36: ; preds = %31
%37 = load i64, i64* @next_line.line_size
%38 = trunc i64 %37 to i32
%39 = shl i32 %38, 1
%40 = tail call i64 @high_water_alloc(i8** @next_line.line, i64* @next_line.line_size, i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %55
42: ; preds = %36
%43 = load i8*, i8** @next_line.line
%44 = tail call i64 @strlen(i8* dereferenceable(1) %43)
%45 = getelementptr i8, i8* %43, i64 %44
%46 = load i64, i64* @next_line.line_size
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %43 to i64
%49 = sub i64 %46, %47
%50 = add i64 %49, %48
%51 = tail call i32 @fgets(i8* %45, i64 %50, i32* %0)
%52 = icmp eq i32 %51, 0
br i1 %52, label %55, label %15
53: ; preds = %31, %29
%54 = load i8*, i8** @next_line.line
br label %55
55: ; preds = %42, %36, %10, %4, %53
%56 = phi i8* [ %54, %53 ], [ null, %4 ], [ null, %10 ], [ null, %36 ], [ null, %42 ]
ret i8* %56
}