#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 *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *realloc(char *, int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ strrchr(char *, char);
char *xxstrdup(char *);
char *get_line(FILE *fp) {
  static char buffer[1048576];
  char *other = NULL;
  if (!fgets(buffer, 1048576, fp)) {
    return NULL;
  }
  if (!strrchr(buffer, '\n') && strlen(buffer) == 1048576 - 1) {
    int s = 1048576;
    do {
      char *tmp = realloc(other, 2 * s * sizeof(char));
      if (!tmp) {
        free(other);
        other = NULL;
        return NULL;
      } else {
        if (!other)
          strncpy(tmp, buffer, strlen(buffer));
        other = tmp;
      }
      tmp = fgets(other + s - 1, s + 1, fp);
      if (!tmp) {
        return other;
      }
      s *= 2;
    } while (!strrchr(other, '\n'));
    return other;
  } else {
    return xxstrdup(buffer);
  }
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdx
lea    0x2ee1(%rip),%rdi
mov    $0x100000,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    11b0 <get_line+0x40>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <get_line+0x173>
lea    0x2eb9(%rip),%rdi
mov    $0xa,%esi
call   1060 <strrchr@plt>
cmp    $0x0,%eax
jne    12d3 <get_line+0x163>
lea    0x2e9f(%rip),%rdi
call   1050 <strlen@plt>
cmp    $0xfffff,%eax
jne    12d3 <get_line+0x163>
movl   $0x100000,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122c <get_line+0xbc>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x8(%rbp)
jmp    12e3 <get_line+0x173>
cmpq   $0x0,-0x18(%rbp)
jne    125d <get_line+0xed>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0x2e2a(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0x2e18(%rip),%rsi
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
add    $0xffffffffffffffff,%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rdx
call   1070 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    129f <get_line+0x12f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12e3 <get_line+0x173>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strrchr@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11e8 <get_line+0x78>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12e3 <get_line+0x173>
lea    0x2d96(%rip),%rdi
call   1090 <xxstrdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0x2eeb(%rip),%rdi
mov    $0x100000,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     126a <get_line+0xfa>
lea    0x2ece(%rip),%rdi
mov    $0xa,%esi
call   1060 <strrchr@plt>
test   %eax,%eax
jne    1248 <get_line+0xd8>
lea    0x2eb5(%rip),%rdi
call   1050 <strlen@plt>
cmp    $0xfffff,%eax
jne    1248 <get_line+0xd8>
mov    $0x100000,%ebp
xor    %r13d,%r13d
lea    0x2e96(%rip),%r14
nopw   0x0(%rax,%rax,1)
lea    0x0(%rbp,%rbp,1),%r12d
mov    %r13,%rdi
mov    %r12d,%esi
call   1080 <realloc@plt>
test   %rax,%rax
je     1262 <get_line+0xf2>
mov    %rax,%rbx
test   %r13,%r13
jne    1212 <get_line+0xa2>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
movslq %ebp,%rax
lea    (%rbx,%rax,1),%rdi
add    $0xffffffffffffffff,%rdi
or     $0x1,%ebp
mov    %ebp,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     126c <get_line+0xfc>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1060 <strrchr@plt>
mov    %rbx,%r13
mov    %r12d,%ebp
test   %eax,%eax
je     11e0 <get_line+0x70>
jmp    126c <get_line+0xfc>
lea    0x2e21(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <xxstrdup@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@get_line.buffer = internal global [1048576 x i8] zeroinitializer
define i8* @get_line(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i32* %0, i32** %3
store i8* null, i8** %4
%7 = load i32*, i32** %3
%8 = call i8* @fgets(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0), i32 1048576, i32* %7)
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %66
11: ; preds = %1
%12 = call i32 @strrchr(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0), i8 signext 10)
%13 = icmp ne i32 %12, 0
br i1 %13, label %64, label %14
14: ; preds = %11
%15 = call i32 @strlen(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0))
%16 = icmp eq i32 %15, 1048575
br i1 %16, label %17, label %64
17: ; preds = %14
store i32 1048576, i32* %5
br label %18
18: ; preds = %57, %17
%19 = load i8*, i8** %4
%20 = load i32, i32* %5
%21 = mul nsw i32 2, %20
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 1
%24 = trunc i64 %23 to i32
%25 = call i8* @realloc(i8* %19, i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %18
%29 = load i8*, i8** %4
%30 = call i32 @free(i8* %29)
store i8* null, i8** %4
store i8* null, i8** %2
br label %66
31: ; preds = %18
%32 = load i8*, i8** %4
%33 = icmp ne i8* %32, null
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = load i8*, i8** %6
%36 = call i32 @strlen(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0))
%37 = call i32 @strncpy(i8* %35, i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %31
%39 = load i8*, i8** %6
store i8* %39, i8** %4
br label %40
40: ; preds = %38
%41 = load i8*, i8** %4
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = getelementptr inbounds i8, i8* %44, i64 -1
%46 = load i32, i32* %5
%47 = add nsw i32 %46, 1
%48 = load i32*, i32** %3
%49 = call i8* @fgets(i8* %45, i32 %47, i32* %48)
store i8* %49, i8** %6
%50 = load i8*, i8** %6
%51 = icmp ne i8* %50, null
br i1 %51, label %54, label %52
52: ; preds = %40
%53 = load i8*, i8** %4
store i8* %53, i8** %2
br label %66
54: ; preds = %40
%55 = load i32, i32* %5
%56 = mul nsw i32 %55, 2
store i32 %56, i32* %5
br label %57
57: ; preds = %54
%58 = load i8*, i8** %4
%59 = call i32 @strrchr(i8* %58, i8 signext 10)
%60 = icmp ne i32 %59, 0
%61 = xor i1 %60, true
br i1 %61, label %18, label %62
62: ; preds = %57
%63 = load i8*, i8** %4
store i8* %63, i8** %2
br label %66
64: ; preds = %14, %11
%65 = call i8* @xxstrdup(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0))
store i8* %65, i8** %2
br label %66
66: ; preds = %64, %62, %52, %28, %10
%67 = load i8*, i8** %2
ret i8* %67
}

LLVM IR (-O2):
@get_line.buffer = internal global [1048576 x i8] zeroinitializer
define i8* @get_line(i32* nocapture %0) {
%2 = tail call i8* @fgets(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0), i32 1048576, i32* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %35, label %4
4: ; preds = %1
%5 = tail call i32 @strrchr(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0), i8 signext 10)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %33
7: ; preds = %4
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0))
%9 = icmp eq i32 %8, 1048575
br i1 %9, label %10, label %33
10: ; preds = %7, %30
%11 = phi i8* [ %14, %30 ], [ null, %7 ]
%12 = phi i32 [ %13, %30 ], [ 1048576, %7 ]
%13 = shl nsw i32 %12, 1
%14 = tail call i8* @realloc(i8* %11, i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @free(i8* %11)
br label %35
18: ; preds = %10
%19 = icmp eq i8* %11, null
br i1 %19, label %20, label %23
20: ; preds = %18
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0))
%22 = tail call i32 @strncpy(i8* %14, i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %18, %20
%24 = sext i32 %12 to i64
%25 = getelementptr inbounds i8, i8* %14, i64 -1
%26 = getelementptr inbounds i8, i8* %25, i64 %24
%27 = or i32 %12, 1
%28 = tail call i8* @fgets(i8* %26, i32 %27, i32* %0)
%29 = icmp eq i8* %28, null
br i1 %29, label %35, label %30
30: ; preds = %23
%31 = tail call i32 @strrchr(i8* %14, i8 signext 10)
%32 = icmp eq i32 %31, 0
br i1 %32, label %10, label %35
33: ; preds = %7, %4
%34 = tail call i8* @xxstrdup(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0))
br label %35
35: ; preds = %23, %30, %16, %1, %33
%36 = phi i8* [ %34, %33 ], [ null, %1 ], [ null, %16 ], [ %14, %30 ], [ %14, %23 ]
ret i8* %36
}