#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(scalar_t__, int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ realloc(char *, scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
scalar_t__ strlen(char *);
char *get_line(FILE *fp) {
  char s[1024], *line, *cp;
  line = NULL;
  do {
    if (fgets(s, 512, fp) == NULL)
      break;
    cp = strchr(s, '\n');
    if (cp != NULL)
      *cp = '\0';
    if (line == NULL)
      line = calloc(1 + strlen(s), sizeof(char));
    else
      line = (char *)realloc(line, 1 + strlen(s) + strlen(line));
    strcat(line, s);
  } while (cp == NULL);
  return line;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    119e <get_line+0x3e>
jmp    1274 <get_line+0x114>
lea    -0x410(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
je     11ce <get_line+0x6e>
mov    -0x420(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x418(%rbp)
jne    1205 <get_line+0xa5>
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x418(%rbp)
jmp    1253 <get_line+0xf3>
mov    -0x418(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x430(%rbp)
mov    -0x418(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x430(%rbp),%rsi
mov    -0x428(%rbp),%rdi
add    %rax,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1080 <strcat@plt>
cmpq   $0x0,-0x420(%rbp)
je     117a <get_line+0x1a>
mov    -0x418(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r14
xor    %r12d,%r12d
mov    %rsp,%r15
jmp    11a0 <get_line+0x40>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
test   %r13,%r13
jne    11fb <get_line+0x9b>
mov    %r15,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11fb <get_line+0x9b>
mov    %r15,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,%r13
test   %rax,%rax
je     11cf <get_line+0x6f>
movb   $0x0,0x0(%r13)
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
add    $0x1,%rbx
test   %r12,%r12
je     1180 <get_line+0x20>
mov    %r12,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <realloc@plt>
jmp    118d <get_line+0x2d>
mov    %r12,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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