#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 */
scalar_t__ 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 = (char *)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 = %42, %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 %45
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 %28
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 i64 @calloc(i64 %25, i32 1)
%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 [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 1, %31
%33 = load i8*, i8** %4
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %32, %34
%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 [1024 x i8], [1024 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 [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %22, %1
%5 = phi i8* [ null, %1 ], [ %24, %22 ]
%6 = call i32* @fgets(i8* %3, i32 512, i32* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %28, 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 i64 @calloc(i64 %15, i32 1)
br label %22
18: ; preds = %12
%19 = call i64 @strlen(i8* %5)
%20 = add nsw i64 %15, %19
%21 = call i64 @realloc(i8* %5, i64 %20)
br label %22
22: ; preds = %18, %16
%23 = phi i64 [ %17, %16 ], [ %21, %18 ]
%24 = inttoptr i64 %23 to i8*
%25 = call i32 @strcat(i8* %24, i8* %3)
br i1 %10, label %4, label %26
26: ; preds = %22
%27 = inttoptr i64 %23 to i8*
br label %28
28: ; preds = %4, %26
%29 = phi i8* [ %27, %26 ], [ %5, %4 ]
ret i8* %29
}