#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*/ fpos_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fsetpos(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ realloc(char *, int);
int strlen(char *);
char *read_line(FILE *f) {
  int j;
  char *buffer = (char *)calloc(1000, sizeof(char));
  fpos_t pos;
  fgetpos(f, &pos);
  j = 1;
  fgets(buffer, 1000 * j, f);
  while (buffer[strlen(buffer) - 1] != '\n' && !feof(f)) {
    buffer = (char *)realloc(buffer, 1000 * ++j * sizeof(char));
    fsetpos(f, &pos);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x3e8,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1040 <fgetpos@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
imul   $0x3e8,-0xc(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1050 <fgets@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x1d(%rbp)
je     11fa <read_line+0x8a>
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    120a <read_line+0x9a>
jmp    1240 <read_line+0xd0>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
imul   $0x3e8,%eax,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1090 <fsetpos@plt>
jmp    11b7 <read_line+0x47>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x3e8,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1040 <fgetpos@plt>
mov    %rbx,%rdi
mov    $0x3e8,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     120b <read_line+0x9b>
mov    $0x7d0,%ebp
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    120b <read_line+0x9b>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <fsetpos@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
add    $0x3e8,%ebp
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11d0 <read_line+0x60>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @read_line(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32* %0, i32** %2
%6 = call i64 @calloc(i32 1000, i32 1)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %4
%8 = load i32*, i32** %2
%9 = call i32 @fgetpos(i32* %8, i32* %5)
store i32 1, i32* %3
%10 = load i8*, i8** %4
%11 = load i32, i32* %3
%12 = mul nsw i32 1000, %11
%13 = load i32*, i32** %2
%14 = call i32 @fgets(i8* %10, i32 %12, i32* %13)
br label %15
15: ; preds = %32, %1
%16 = load i8*, i8** %4
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %16, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 10
br i1 %24, label %25, label %30
25: ; preds = %15
%26 = load i32*, i32** %2
%27 = call i32 @feof(i32* %26)
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br label %30
30: ; preds = %25, %15
%31 = phi i1 [ false, %15 ], [ %29, %25 ]
br i1 %31, label %32, label %44
32: ; preds = %30
%33 = load i8*, i8** %4
%34 = load i32, i32* %3
%35 = add nsw i32 %34, 1
store i32 %35, i32* %3
%36 = mul nsw i32 1000, %35
%37 = sext i32 %36 to i64
%38 = mul i64 %37, 1
%39 = trunc i64 %38 to i32
%40 = call i64 @realloc(i8* %33, i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %4
%42 = load i32*, i32** %2
%43 = call i32 @fsetpos(i32* %42, i32* %5)
br label %15
44: ; preds = %30
%45 = load i8*, i8** %4
ret i8* %45
}

LLVM IR (-O2):
define i8* @read_line(i32* nocapture %0) {
%2 = alloca i32
%3 = tail call i64 @calloc(i32 1000, i32 1)
%4 = inttoptr i64 %3 to i8*
%5 = bitcast i32* %2 to i8*
%6 = call i32 @fgetpos(i32* %0, i32* %2)
%7 = tail call i32 @fgets(i8* %4, i32 1000, i32* %0)
%8 = call i32 @strlen(i8* %4)
%9 = add nsw i32 %8, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %4, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %33, label %14
14: ; preds = %1, %19
%15 = phi i8* [ %23, %19 ], [ %4, %1 ]
%16 = phi i32 [ %20, %19 ], [ 1, %1 ]
%17 = call i32 @feof(i32* %0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %14
%20 = add nuw nsw i32 %16, 1
%21 = mul nsw i32 %20, 1000
%22 = call i64 @realloc(i8* %15, i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = call i32 @fsetpos(i32* %0, i32* %2)
%25 = call i32 @strlen(i8* %23)
%26 = add nsw i32 %25, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %23, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 10
br i1 %30, label %31, label %14
31: ; preds = %19
%32 = inttoptr i64 %22 to i8*
br label %33
33: ; preds = %14, %31, %1
%34 = phi i8* [ %4, %1 ], [ %32, %31 ], [ %15, %14 ]
ret i8* %34
}