#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 *fgets(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/);
char *malloc(size_t);
char *realloc(char *, size_t);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strchr(char *, char);
char *read_line(const char *prompt) {
  char *buf, *c;
  size_t buf_size = 0;
  size_t chunk_size = 128;
  buf_size = chunk_size;
  buf = malloc(buf_size);
  fputs(prompt, stdout);
  c = buf;
  while ((c = fgets(c, chunk_size, stdin))) {
    c = strchr(buf, '\0');
    if (c > buf && *(c - 1) == '\n') {
      *(c - 1) = 0;
      return buf;
    }
    buf_size += chunk_size;
    buf = realloc(buf, buf_size);
  }
  return buf;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x80,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1224 <read_line+0xd4>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1202 <read_line+0xb2>
mov    -0x20(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
jne    1202 <read_line+0xb2>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122c <read_line+0xdc>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    119b <read_line+0x4b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x80,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <fputs@plt>
mov    0x2e68(%rip),%r12
mov    (%r12),%edx
mov    $0x80,%esi
mov    %r14,%rdi
call   1050 <fgets@plt>
test   %rax,%rax
je     11e5 <read_line+0x95>
mov    $0x100,%r15d
jmp    11c8 <read_line+0x78>
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <realloc@plt>
mov    %rax,%r14
mov    (%r12),%edx
mov    $0x80,%esi
mov    %rbx,%rdi
call   1050 <fgets@plt>
sub    $0xffffffffffffff80,%r15
test   %rax,%rax
je     11e5 <read_line+0x95>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
add    %r14,%rbx
cmp    %r14,%rbx
jbe    11a0 <read_line+0x50>
cmpb   $0xa,-0x1(%rbx)
jne    11a0 <read_line+0x50>
movb   $0x0,-0x1(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdout = global i32 0
@stdin = global i32 0
define i8* @read_line(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* %6
store i64 128, i64* %7
%8 = load i64, i64* %7
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %3
%12 = load i32, i32* @stdout
%13 = call i32 @fputs(i8* %11, i32 %12)
%14 = load i8*, i8** %4
store i8* %14, i8** %5
br label %15
15: ; preds = %37, %1
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = load i32, i32* @stdin
%19 = call i8* @fgets(i8* %16, i64 %17, i32 %18)
store i8* %19, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %44
21: ; preds = %15
%22 = load i8*, i8** %4
%23 = call i8* @strchr(i8* %22, i8 signext 0)
store i8* %23, i8** %5
%24 = load i8*, i8** %5
%25 = load i8*, i8** %4
%26 = icmp ugt i8* %24, %25
br i1 %26, label %27, label %37
27: ; preds = %21
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i64 -1
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i64 -1
store i8 0, i8* %35
%36 = load i8*, i8** %4
store i8* %36, i8** %2
br label %46
37: ; preds = %27, %21
%38 = load i64, i64* %7
%39 = load i64, i64* %6
%40 = add i64 %39, %38
store i64 %40, i64* %6
%41 = load i8*, i8** %4
%42 = load i64, i64* %6
%43 = call i8* @realloc(i8* %41, i64 %42)
store i8* %43, i8** %4
br label %15
44: ; preds = %15
%45 = load i8*, i8** %4
store i8* %45, i8** %2
br label %46
46: ; preds = %44, %33
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@stdout = global i32 0
@stdin = global i32 0
define i8* @read_line(i8* %0) {
%2 = tail call dereferenceable_or_null(128) i8* @malloc(i64 128)
%3 = load i32, i32* @stdout
%4 = tail call i32 @fputs(i8* %0, i32 %3)
%5 = load i32, i32* @stdin
%6 = tail call i8* @fgets(i8* %2, i64 128, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %26, label %8
8: ; preds = %1, %20
%9 = phi i64 [ %21, %20 ], [ 128, %1 ]
%10 = phi i8* [ %22, %20 ], [ %2, %1 ]
%11 = tail call i64 @strlen(i8* dereferenceable(1) %10)
%12 = getelementptr i8, i8* %10, i64 %11
%13 = icmp ugt i8* %12, %10
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = getelementptr inbounds i8, i8* %12, i64 -1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %12, i64 -1
store i8 0, i8* %19
br label %26
20: ; preds = %14, %8
%21 = add i64 %9, 128
%22 = tail call i8* @realloc(i8* %10, i64 %21)
%23 = load i32, i32* @stdin
%24 = tail call i8* @fgets(i8* %12, i64 128, i32 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %8
26: ; preds = %20, %1, %18
%27 = phi i8* [ %10, %18 ], [ %2, %1 ], [ %22, %20 ]
ret i8* %27
}