#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*/ *);
scalar_t__ realloc(char *, scalar_t__);
scalar_t__ space(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
scalar_t__ strlen(char *);
char *get_line(FILE *fp) {
  char s[512], *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 = (char *)space(strlen(s) + 1);
    else
      line = (char *)realloc(line, strlen(s) + strlen(line) + 1);
    strcat(line, s);
  } while (cp == NULL);
  return line;
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x218(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x200,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    119e <get_line+0x3e>
jmp    126f <get_line+0x10f>
lea    -0x210(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x220(%rbp)
cmpq   $0x0,-0x220(%rbp)
je     11ce <get_line+0x6e>
mov    -0x220(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x218(%rbp)
jne    1200 <get_line+0xa0>
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1030 <space@plt>
mov    %rax,-0x218(%rbp)
jmp    124e <get_line+0xee>
mov    -0x218(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x230(%rbp)
mov    -0x218(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x230(%rbp),%rsi
mov    -0x228(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
lea    -0x210(%rbp),%rsi
call   1080 <strcat@plt>
cmpq   $0x0,-0x220(%rbp)
je     117a <get_line+0x1a>
mov    -0x218(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rdi,%r14
xor    %r12d,%r12d
mov    %rsp,%r15
jmp    119f <get_line+0x3f>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %rbx,%rdi
call   1030 <space@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
test   %r13,%r13
jne    11f8 <get_line+0x98>
mov    %r15,%rdi
mov    $0x200,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f8 <get_line+0x98>
mov    %r15,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,%r13
test   %rax,%rax
je     11ce <get_line+0x6e>
movb   $0x0,0x0(%r13)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
test   %r12,%r12
je     1180 <get_line+0x20>
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1070 <realloc@plt>
jmp    118c <get_line+0x2c>
mov    %r12,%rax
add    $0x200,%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 [512 x i8]
%4 = alloca i8*
%5 = alloca i8*
store i32* %0, i32** %2
store i8* null, i8** %4
br label %6
6: ; preds = %42, %1
%7 = getelementptr inbounds [512 x i8], [512 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 %45
12: ; preds = %6
%13 = getelementptr inbounds [512 x i8], [512 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 %28
22: ; preds = %19
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%24 = call i64 @strlen(i8* %23)
%25 = add nsw i64 %24, 1
%26 = call i64 @space(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %4
br label %38
28: ; preds = %19
%29 = load i8*, i8** %4
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%31 = call i64 @strlen(i8* %30)
%32 = load i8*, i8** %4
%33 = call i64 @strlen(i8* %32)
%34 = add nsw i64 %31, %33
%35 = add nsw i64 %34, 1
%36 = call i64 @realloc(i8* %29, i64 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %4
br label %38
38: ; preds = %28, %22
%39 = load i8*, i8** %4
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%41 = call i32 @strcat(i8* %39, i8* %40)
br label %42
42: ; preds = %38
%43 = load i8*, i8** %5
%44 = icmp eq i8* %43, null
br i1 %44, label %6, label %45
45: ; preds = %42, %11
%46 = load i8*, i8** %4
ret i8* %46
}

LLVM IR (-O2):
define i8* @get_line(i32* nocapture %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %23, %1
%5 = phi i8* [ null, %1 ], [ %25, %23 ]
%6 = call i32* @fgets(i8* %3, i32 512, i32* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %29, 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)
br i1 %13, label %15, label %18
15: ; preds = %12
%16 = add nsw i64 %14, 1
%17 = call i64 @space(i64 %16)
br label %23
18: ; preds = %12
%19 = call i64 @strlen(i8* %5)
%20 = add i64 %14, 1
%21 = add i64 %20, %19
%22 = call i64 @realloc(i8* %5, i64 %21)
br label %23
23: ; preds = %18, %15
%24 = phi i64 [ %17, %15 ], [ %22, %18 ]
%25 = inttoptr i64 %24 to i8*
%26 = call i32 @strcat(i8* %25, i8* %3)
br i1 %10, label %4, label %27
27: ; preds = %23
%28 = inttoptr i64 %24 to i8*
br label %29
29: ; preds = %4, %27
%30 = phi i8* [ %28, %27 ], [ %5, %4 ]
ret i8* %30
}