#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 *calloc(int, int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
char *realloc(char *, size_t);
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char *);
char *strrchr(char *, char);
char *line_in(FILE *fp) {
  size_t len = 16;
  char buf[16];
  char *end = NULL;
  char *ret = calloc(16, 1);
  while (fgets(buf, 16, fp)) {
    if (len - strlen(ret) < 16)
      ret = realloc(ret, len *= 2);
    strcat(ret, buf);
    if ((end = strrchr(ret, '\n')) != NULL)
      break;
  }
  if (end)
    *end = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<line_in>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x10,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x10,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x10,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     121c <line_in+0xbc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
cmp    $0x10,%rax
jae    11e9 <line_in+0x89>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
shl    $0x1,%rsi
mov    %rsi,-0x10(%rbp)
call   1070 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1217 <line_in+0xb7>
jmp    121c <line_in+0xbc>
jmp    118f <line_in+0x2f>
cmpq   $0x0,-0x28(%rbp)
je     122e <line_in+0xce>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<line_in>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    $0x10,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    $0x10,%r15d
mov    %rsp,%r12
jmp    11ad <line_in+0x4d>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
jne    11e9 <line_in+0x89>
mov    %r12,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11ec <line_in+0x8c>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r15,%rcx
sub    %rax,%rcx
cmp    $0xf,%rcx
ja     1190 <line_in+0x30>
add    %r15,%r15
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <realloc@plt>
mov    %rax,%rbx
jmp    1190 <line_in+0x30>
movb   $0x0,(%rax)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @line_in(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca [16 x i8]
%5 = alloca i8*
%6 = alloca i8*
store i32* %0, i32** %2
store i64 16, i64* %3
store i8* null, i8** %5
%7 = call i8* @calloc(i32 16, i32 1)
store i8* %7, i8** %6
br label %8
8: ; preds = %32, %1
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%10 = load i32*, i32** %2
%11 = call i64 @fgets(i8* %9, i32 16, i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %8
%14 = load i64, i64* %3
%15 = load i8*, i8** %6
%16 = call i64 @strlen(i8* %15)
%17 = sub i64 %14, %16
%18 = icmp ult i64 %17, 16
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load i8*, i8** %6
%21 = load i64, i64* %3
%22 = mul i64 %21, 2
store i64 %22, i64* %3
%23 = call i8* @realloc(i8* %20, i64 %22)
store i8* %23, i8** %6
br label %24
24: ; preds = %19, %13
%25 = load i8*, i8** %6
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%27 = call i32 @strcat(i8* %25, i8* %26)
%28 = load i8*, i8** %6
%29 = call i8* @strrchr(i8* %28, i8 signext 10)
store i8* %29, i8** %5
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %32
31: ; preds = %24
br label %33
32: ; preds = %24
br label %8
33: ; preds = %31, %8
%34 = load i8*, i8** %5
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = load i8*, i8** %5
store i8 0, i8* %37
br label %38
38: ; preds = %36, %33
%39 = load i8*, i8** %6
ret i8* %39
}

LLVM IR (-O2):
define i8* @line_in(i32* nocapture %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = tail call dereferenceable_or_null(16) i8* @calloc(i32 16, i32 1)
br label %5
5: ; preds = %17, %1
%6 = phi i64 [ 16, %1 ], [ %18, %17 ]
%7 = phi i8* [ %4, %1 ], [ %19, %17 ]
%8 = call i64 @fgets(i8* %3, i32 16, i32* %0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %5
%11 = call i64 @strlen(i8* dereferenceable(1) %7)
%12 = sub i64 %6, %11
%13 = icmp ult i64 %12, 16
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = shl i64 %6, 1
%16 = call i8* @realloc(i8* %7, i64 %15)
br label %17
17: ; preds = %14, %10
%18 = phi i64 [ %15, %14 ], [ %6, %10 ]
%19 = phi i8* [ %16, %14 ], [ %7, %10 ]
%20 = call i32 @strcat(i8* %19, i8* %3)
%21 = call i8* @strrchr(i8* dereferenceable(1) %19, i8 signext 10)
%22 = icmp eq i8* %21, null
br i1 %22, label %5, label %23
23: ; preds = %17
store i8 0, i8* %21
br label %24
24: ; preds = %5, %23
%25 = phi i8* [ %19, %23 ], [ %7, %5 ]
ret i8* %25
}