#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, 1048576);
        }
        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    12d2 <get_line+0x162>
lea    0x2eb9(%rip),%rdi
mov    $0xa,%esi
call   1060 <strrchr@plt>
cmp    $0x0,%eax
jne    12c2 <get_line+0x152>
lea    0x2e9f(%rip),%rdi
call   1050 <strlen@plt>
cmp    $0xfffff,%eax
jne    12c2 <get_line+0x152>
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    12d2 <get_line+0x162>
cmpq   $0x0,-0x18(%rbp)
jne    124c <get_line+0xdc>
mov    -0x28(%rbp),%rdi
lea    0x2e2e(%rip),%rsi
mov    $0x100000,%edx
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    128e <get_line+0x11e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d2 <get_line+0x162>
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    12d2 <get_line+0x162>
lea    0x2da7(%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     1265 <get_line+0xf5>
lea    0x2ece(%rip),%rdi
mov    $0xa,%esi
call   1060 <strrchr@plt>
test   %eax,%eax
jne    1243 <get_line+0xd3>
lea    0x2eb5(%rip),%rdi
call   1050 <strlen@plt>
cmp    $0xfffff,%eax
jne    1243 <get_line+0xd3>
mov    $0x100000,%ebp
xor    %r13d,%r13d
lea    0x2e9a(%rip),%r14
cs 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     125d <get_line+0xed>
mov    %rax,%rbx
test   %r13,%r13
jne    120d <get_line+0x9d>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100000,%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     1267 <get_line+0xf7>
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    1267 <get_line+0xf7>
lea    0x2e26(%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 %65
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 %63, 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 %63
17: ; preds = %14
store i32 1048576, i32* %5
br label %18
18: ; preds = %56, %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 %65
31: ; preds = %18
%32 = load i8*, i8** %4
%33 = icmp ne i8* %32, null
br i1 %33, label %37, label %34
34: ; preds = %31
%35 = load i8*, i8** %6
%36 = call i32 @strncpy(i8* %35, i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0), i32 1048576)
br label %37
37: ; preds = %34, %31
%38 = load i8*, i8** %6
store i8* %38, i8** %4
br label %39
39: ; preds = %37
%40 = load i8*, i8** %4
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = getelementptr inbounds i8, i8* %43, i64 -1
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
%47 = load i32*, i32** %3
%48 = call i8* @fgets(i8* %44, i32 %46, i32* %47)
store i8* %48, i8** %6
%49 = load i8*, i8** %6
%50 = icmp ne i8* %49, null
br i1 %50, label %53, label %51
51: ; preds = %39
%52 = load i8*, i8** %4
store i8* %52, i8** %2
br label %65
53: ; preds = %39
%54 = load i32, i32* %5
%55 = mul nsw i32 %54, 2
store i32 %55, i32* %5
br label %56
56: ; preds = %53
%57 = load i8*, i8** %4
%58 = call i32 @strrchr(i8* %57, i8 signext 10)
%59 = icmp ne i32 %58, 0
%60 = xor i1 %59, true
br i1 %60, label %18, label %61
61: ; preds = %56
%62 = load i8*, i8** %4
store i8* %62, i8** %2
br label %65
63: ; preds = %14, %11
%64 = call i8* @xxstrdup(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0))
store i8* %64, i8** %2
br label %65
65: ; preds = %63, %61, %51, %28, %10
%66 = load i8*, i8** %2
ret i8* %66
}

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 %34, 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 %32
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 %32
10: ; preds = %7, %29
%11 = phi i8* [ %14, %29 ], [ null, %7 ]
%12 = phi i32 [ %13, %29 ], [ 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 %34
18: ; preds = %10
%19 = icmp eq i8* %11, null
br i1 %19, label %20, label %22
20: ; preds = %18
%21 = tail call i32 @strncpy(i8* %14, i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0), i32 1048576)
br label %22
22: ; preds = %18, %20
%23 = sext i32 %12 to i64
%24 = getelementptr inbounds i8, i8* %14, i64 -1
%25 = getelementptr inbounds i8, i8* %24, i64 %23
%26 = or i32 %12, 1
%27 = tail call i8* @fgets(i8* %25, i32 %26, i32* %0)
%28 = icmp eq i8* %27, null
br i1 %28, label %34, label %29
29: ; preds = %22
%30 = tail call i32 @strrchr(i8* %14, i8 signext 10)
%31 = icmp eq i32 %30, 0
br i1 %31, label %10, label %34
32: ; preds = %7, %4
%33 = tail call i8* @xxstrdup(i8* getelementptr inbounds ([1048576 x i8], [1048576 x i8]* @get_line.buffer, i64 0, i64 0))
br label %34
34: ; preds = %22, %29, %16, %1, %32
%35 = phi i8* [ %33, %32 ], [ null, %1 ], [ null, %16 ], [ %14, %29 ], [ %14, %22 ]
ret i8* %35
}