#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__ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *get_line(FILE *fd) {
  char *line;
  int i = 0;
  line = (char *)malloc(sizeof(char) * 1024);
  memset(line, 0, 1024);
  while ((line[i] = fgetc(fd)) != '\n') {
    if (feof(fd)) {
      return (0);
    }
    i++;
  }
  return (line);
}


x86 assembly (clang, -O0):
<get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%eax
cmp    $0xa,%eax
je     11bf <get_line+0x7f>
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%rax
je     11b1 <get_line+0x71>
movq   $0x0,-0x8(%rbp)
jmp    11c7 <get_line+0x87>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1171 <get_line+0x31>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_line>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1030 <memset@plt>
mov    %r15,%rdi
call   1040 <fgetc@plt>
mov    %al,(%r14)
cmp    $0xa,%al
je     11a4 <get_line+0x64>
mov    %r14,%rbx
add    $0x1,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <feof@plt>
test   %rax,%rax
jne    11a1 <get_line+0x61>
mov    %r15,%rdi
call   1040 <fgetc@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
cmp    $0xa,%al
jne    1180 <get_line+0x40>
jmp    11a4 <get_line+0x64>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @get_line(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
store i32* %0, i32** %3
store i32 0, i32* %5
%6 = call i64 @malloc(i32 1024)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 @memset(i8* %8, i32 0, i32 1024)
br label %10
10: ; preds = %24, %1
%11 = load i32*, i32** %3
%12 = call signext i8 @fgetc(i32* %11)
%13 = load i8*, i8** %4
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 %12, i8* %16
%17 = sext i8 %12 to i32
%18 = icmp ne i32 %17, 10
br i1 %18, label %19, label %27
19: ; preds = %10
%20 = load i32*, i32** %3
%21 = call i64 @feof(i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i8* null, i8** %2
br label %29
24: ; preds = %19
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %10
27: ; preds = %10
%28 = load i8*, i8** %4
store i8* %28, i8** %2
br label %29
29: ; preds = %27, %23
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
define i8* @get_line(i32* nocapture %0) {
%2 = tail call i64 @malloc(i32 1024)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @memset(i8* %3, i32 0, i32 1024)
%5 = tail call signext i8 @fgetc(i32* %0)
store i8 %5, i8* %3
%6 = icmp eq i8 %5, 10
br i1 %6, label %16, label %11
7: ; preds = %11
%8 = tail call signext i8 @fgetc(i32* %0)
%9 = getelementptr inbounds i8, i8* %3, i64 %15
store i8 %8, i8* %9
%10 = icmp eq i8 %8, 10
br i1 %10, label %16, label %11
11: ; preds = %1, %7
%12 = phi i64 [ %15, %7 ], [ 0, %1 ]
%13 = tail call i64 @feof(i32* %0)
%14 = icmp eq i64 %13, 0
%15 = add nuw i64 %12, 1
br i1 %14, label %7, label %16
16: ; preds = %11, %7, %1
%17 = phi i8* [ %3, %1 ], [ %3, %7 ], [ null, %11 ]
ret i8* %17
}