#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 getc(int /*<<< orphan*/ *);
char *try_realloc(char *, size_t);
char *getaline_filehandle(FILE *filehandle) {
  char *buff = 0;
  size_t buffsize = 0;
  char c = 0;
  while ((c = getc(filehandle)) != '\n') {
    buff = try_realloc(buff, buffsize + 1);
    buff[buffsize++] = c;
  }
  buff = try_realloc(buff, buffsize + 1);
  buff[buffsize + 1] = 0;
  return buff;
}


x86 assembly (clang, -O0):
<getaline_filehandle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <getc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     118b <getaline_filehandle+0x6b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1030 <try_realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x18(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    1140 <getaline_filehandle+0x20>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1030 <try_realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getaline_filehandle>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <getc@plt>
cmp    $0xa,%al
jne    113d <getaline_filehandle+0x1d>
mov    $0x1,%ebx
xor    %r15d,%r15d
jmp    1175 <getaline_filehandle+0x55>
mov    %eax,%ebp
mov    $0x1,%ebx
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <try_realloc@plt>
mov    %rax,%r15
mov    %bpl,-0x1(%rax,%rbx,1)
mov    %r14,%rdi
call   1040 <getc@plt>
mov    %eax,%ebp
add    $0x1,%rbx
cmp    $0xa,%al
jne    1150 <getaline_filehandle+0x30>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <try_realloc@plt>
movb   $0x0,(%rax,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @getaline_filehandle(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8
store i32* %0, i32** %2
store i8* null, i8** %3
store i64 0, i64* %4
store i8 0, i8* %5
br label %6
6: ; preds = %11, %1
%7 = load i32*, i32** %2
%8 = call signext i8 @getc(i32* %7)
store i8 %8, i8* %5
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 10
br i1 %10, label %11, label %21
11: ; preds = %6
%12 = load i8*, i8** %3
%13 = load i64, i64* %4
%14 = add i64 %13, 1
%15 = call i8* @try_realloc(i8* %12, i64 %14)
store i8* %15, i8** %3
%16 = load i8, i8* %5
%17 = load i8*, i8** %3
%18 = load i64, i64* %4
%19 = add i64 %18, 1
store i64 %19, i64* %4
%20 = getelementptr inbounds i8, i8* %17, i64 %18
store i8 %16, i8* %20
br label %6
21: ; preds = %6
%22 = load i8*, i8** %3
%23 = load i64, i64* %4
%24 = add i64 %23, 1
%25 = call i8* @try_realloc(i8* %22, i64 %24)
store i8* %25, i8** %3
%26 = load i8*, i8** %3
%27 = load i64, i64* %4
%28 = add i64 %27, 1
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
define i8* @getaline_filehandle(i32* nocapture %0) {
%2 = tail call signext i8 @getc(i32* %0)
%3 = icmp eq i8 %2, 10
br i1 %3, label %15, label %4
4: ; preds = %1, %4
%5 = phi i8 [ %11, %4 ], [ %2, %1 ]
%6 = phi i64 [ %8, %4 ], [ 0, %1 ]
%7 = phi i8* [ %9, %4 ], [ null, %1 ]
%8 = add i64 %6, 1
%9 = tail call i8* @try_realloc(i8* %7, i64 %8)
%10 = getelementptr inbounds i8, i8* %9, i64 %6
store i8 %5, i8* %10
%11 = tail call signext i8 @getc(i32* %0)
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %4
13: ; preds = %4
%14 = add i64 %6, 2
br label %15
15: ; preds = %13, %1
%16 = phi i8* [ null, %1 ], [ %9, %13 ]
%17 = phi i64 [ 1, %1 ], [ %14, %13 ]
%18 = tail call i8* @try_realloc(i8* %16, i64 %17)
%19 = getelementptr inbounds i8, i8* %18, i64 %17
store i8 0, i8* %19
ret i8* %18
}