#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 */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
char *realloc(char *, size_t);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *get_string_from_file(FILE *fp) {
  char buf[2048];
  char *out = NULL;
  size_t size = 0;
  while (fgets(buf, 2048, fp) != NULL) {
    size_t len = strlen(buf);
    char *tmp = realloc(out, size + len + 1);
    if (tmp == NULL)
      break;
    strcpy(tmp + size, buf);
    out = tmp;
    size += len;
    if (buf[len - 1] == '\n')
      buf[len - 1] = '\0';
  }
  return out;
}


x86 assembly (clang, -O0):
<get_string_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x818(%rbp)
movq   $0x0,-0x820(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x800,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1240 <get_string_from_file+0x100>
lea    -0x810(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x828(%rbp)
mov    -0x818(%rbp),%rdi
mov    -0x820(%rbp),%rsi
add    -0x828(%rbp),%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x830(%rbp)
cmpq   $0x0,-0x830(%rbp)
jne    11cf <get_string_from_file+0x8f>
jmp    1240 <get_string_from_file+0x100>
mov    -0x830(%rbp),%rdi
add    -0x820(%rbp),%rdi
lea    -0x810(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x830(%rbp),%rax
mov    %rax,-0x818(%rbp)
mov    -0x828(%rbp),%rax
add    -0x820(%rbp),%rax
mov    %rax,-0x820(%rbp)
mov    -0x828(%rbp),%rax
sub    $0x1,%rax
movsbl -0x810(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    123b <get_string_from_file+0xfb>
mov    -0x828(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x810(%rbp,%rax,1)
jmp    1165 <get_string_from_file+0x25>
mov    -0x818(%rbp),%rax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_string_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x800,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     11d9 <get_string_from_file+0x99>
xor    %ebp,%ebp
jmp    119b <get_string_from_file+0x5b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %rbx,%rbp
mov    %r15,%rdi
mov    $0x800,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
mov    %r13,%r12
test   %rax,%rax
je     11d9 <get_string_from_file+0x99>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
lea    (%rax,%rbp,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     11d9 <get_string_from_file+0x99>
mov    %rax,%r13
lea    (%rax,%rbp,1),%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
cmpb   $0xa,-0x1(%rbx,%r15,1)
jne    1180 <get_string_from_file+0x40>
movb   $0x0,-0x1(%rsp,%rbx,1)
jmp    1180 <get_string_from_file+0x40>
mov    %r12,%rax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @get_string_from_file(i32* %0) {
%2 = alloca i32*
%3 = alloca [2048 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store i32* %0, i32** %2
store i8* null, i8** %4
store i64 0, i64* %5
br label %8
8: ; preds = %45, %1
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%10 = load i32*, i32** %2
%11 = call i32* @fgets(i8* %9, i32 2048, i32* %10)
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %46
13: ; preds = %8
%14 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %6
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = load i64, i64* %6
%19 = add i64 %17, %18
%20 = add i64 %19, 1
%21 = call i8* @realloc(i8* %16, i64 %20)
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %13
br label %46
25: ; preds = %13
%26 = load i8*, i8** %7
%27 = load i64, i64* %5
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %7
store i8* %31, i8** %4
%32 = load i64, i64* %6
%33 = load i64, i64* %5
%34 = add i64 %33, %32
store i64 %34, i64* %5
%35 = load i64, i64* %6
%36 = sub i64 %35, 1
%37 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 10
br i1 %40, label %41, label %45
41: ; preds = %25
%42 = load i64, i64* %6
%43 = sub i64 %42, 1
%44 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %43
store i8 0, i8* %44
br label %45
45: ; preds = %41, %25
br label %8
46: ; preds = %24, %8
%47 = load i8*, i8** %4
ret i8* %47
}

LLVM IR (-O2):
define i8* @get_string_from_file(i32* nocapture %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = call i32* @fgets(i8* %3, i32 2048, i32* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %25, label %6
6: ; preds = %1, %22
%7 = phi i8* [ %12, %22 ], [ null, %1 ]
%8 = phi i64 [ %10, %22 ], [ 0, %1 ]
%9 = call i64 @strlen(i8* %3)
%10 = add i64 %9, %8
%11 = add i64 %10, 1
%12 = call i8* @realloc(i8* %7, i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %25, label %14
14: ; preds = %6
%15 = getelementptr inbounds i8, i8* %12, i64 %8
%16 = call i32 @strcpy(i8* %15, i8* %3)
%17 = add i64 %9, -1
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %22
21: ; preds = %14
store i8 0, i8* %18
br label %22
22: ; preds = %14, %21
%23 = call i32* @fgets(i8* %3, i32 2048, i32* %0)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %6
25: ; preds = %22, %6, %1
%26 = phi i8* [ null, %1 ], [ %7, %6 ], [ %12, %22 ]
ret i8* %26
}