#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 */

/* Variables and functions */
char *newbuf();
scalar_t__ read(int, char *, int);
char *sizeset(char *, size_t);
char *readline(int f) {
  char *buf;
  size_t i;
  for (buf = newbuf(), i = 0;; i++) {
    buf = sizeset(buf, i + 1);
    if (read(f, buf + i, 1) == 0)
      return 0;
    if (buf[i] == '\n')
      break;
  }
  buf[i] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x0,%al
call   1030 <newbuf@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1050 <sizeset@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jne    118f <readline+0x5f>
movq   $0x0,-0x8(%rbp)
jmp    11d3 <readline+0xa3>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11a9 <readline+0x79>
jmp    11bf <readline+0x8f>
jmp    11ae <readline+0x7e>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114e <readline+0x1e>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <newbuf@plt>
mov    %rax,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r15,%rbx
add    $0x1,%r15
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <sizeset@plt>
mov    %rax,%rbp
add    %rax,%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
je     118a <readline+0x5a>
cmpb   $0xa,(%rbx)
jne    1150 <readline+0x20>
movb   $0x0,-0x1(%rbp,%r15,1)
jmp    118c <readline+0x5c>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @readline(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i32 %0, i32* %3
%6 = call i8* (...) @newbuf()
store i8* %6, i8** %4
store i64 0, i64* %5
br label %7
7: ; preds = %28, %1
%8 = load i8*, i8** %4
%9 = load i64, i64* %5
%10 = add i64 %9, 1
%11 = call i8* @sizeset(i8* %8, i64 %10)
store i8* %11, i8** %4
%12 = load i32, i32* %3
%13 = load i8*, i8** %4
%14 = load i64, i64* %5
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = call i64 @read(i32 %12, i8* %15, i32 1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %7
store i8* null, i8** %2
br label %36
19: ; preds = %7
%20 = load i8*, i8** %4
%21 = load i64, i64* %5
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %26, label %27
26: ; preds = %19
br label %31
27: ; preds = %19
br label %28
28: ; preds = %27
%29 = load i64, i64* %5
%30 = add i64 %29, 1
store i64 %30, i64* %5
br label %7
31: ; preds = %26
%32 = load i8*, i8** %4
%33 = load i64, i64* %5
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %4
store i8* %35, i8** %2
br label %36
36: ; preds = %31, %18
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
define i8* @readline(i32 %0) {
%2 = tail call i8* (...) @newbuf()
br label %3
3: ; preds = %11, %1
%4 = phi i8* [ %2, %1 ], [ %7, %11 ]
%5 = phi i64 [ 0, %1 ], [ %6, %11 ]
%6 = add i64 %5, 1
%7 = tail call i8* @sizeset(i8* %4, i64 %6)
%8 = getelementptr inbounds i8, i8* %7, i64 %5
%9 = tail call i64 @read(i32 %0, i8* %8, i32 1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %3
%12 = load i8, i8* %8
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %3
14: ; preds = %11
store i8 0, i8* %8
br label %15
15: ; preds = %3, %14
%16 = phi i8* [ %7, %14 ], [ null, %3 ]
ret i8* %16
}