#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*/ part_buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *get_line(int /*<<< orphan*/ *, char *, int);
char *realloc(char *, size_t const);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *read_line(FILE *fp, char buf[]) {
  enum { PART_BUFFER_LEN = 512 };
  char part_buf[PART_BUFFER_LEN];
  char *last_allocated_block = NULL;
  size_t len = 0;
  while (get_line(fp, part_buf, sizeof(part_buf)) != NULL) {
    size_t part_len = strlen(part_buf);
    const int eol = (part_len > 0) && (part_buf[part_len - 1] == '\n');
    const size_t new_len = len + (part_len - eol);
    last_allocated_block = realloc(buf, new_len + 1);
    if (last_allocated_block == NULL) {
      break;
    }
    if (eol) {
      part_buf[--part_len] = '\0';
    }
    buf = last_allocated_block;
    strcpy(buf + len, part_buf);
    if (eol) {
      break;
    }
    len = new_len;
  }
  if (last_allocated_block == NULL) {
    free(buf);
    buf = NULL;
  }
  return buf;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x218(%rbp)
movq   $0x0,-0x220(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1050 <get_line@plt>
cmp    $0x0,%rax
je     12b2 <read_line+0x162>
lea    -0x210(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x228(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x228(%rbp)
mov    %al,-0x239(%rbp)
jbe    11e0 <read_line+0x90>
mov    -0x228(%rbp),%rax
sub    $0x1,%rax
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
sete   %al
mov    %al,-0x239(%rbp)
mov    -0x239(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x22c(%rbp)
mov    -0x220(%rbp),%rax
mov    -0x228(%rbp),%rcx
movslq -0x22c(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x238(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x238(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    1241 <read_line+0xf1>
jmp    12b2 <read_line+0x162>
cmpl   $0x0,-0x22c(%rbp)
je     126b <read_line+0x11b>
mov    -0x228(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x228(%rbp)
movb   $0x0,-0x211(%rbp,%rax,1)
mov    -0x218(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x220(%rbp),%rdi
lea    -0x210(%rbp),%rsi
call   1040 <strcpy@plt>
cmpl   $0x0,-0x22c(%rbp)
je     129f <read_line+0x14f>
jmp    12b2 <read_line+0x162>
mov    -0x238(%rbp),%rax
mov    %rax,-0x220(%rbp)
jmp    1179 <read_line+0x29>
cmpq   $0x0,-0x218(%rbp)
jne    12d1 <read_line+0x181>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rsi,0x8(%rsp)
lea    0x20(%rsp),%rbx
mov    %rdi,0x18(%rsp)
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <get_line@plt>
test   %rax,%rax
je     121d <read_line+0xcd>
mov    %rax,0x10(%rsp)
xor    %r15d,%r15d
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rax,%r14
test   %rax,%rax
je     11b0 <read_line+0x60>
cmpb   $0xa,-0x1(%r14,%rbx,1)
sete   %r12b
jmp    11b3 <read_line+0x63>
nopl   0x0(%rax)
xor    %r12d,%r12d
mov    %r14,%rbp
add    %r15,%rbp
movzbl %r12b,%eax
sub    %rax,%rbp
lea    0x1(%rbp),%rsi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     1222 <read_line+0xd2>
mov    %rax,%r13
test   %r12b,%r12b
jne    122f <read_line+0xdf>
mov    %r13,%rdi
add    %r15,%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x18(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <get_line@plt>
mov    %r13,0x8(%rsp)
mov    %rbp,%r15
test   %rax,%rax
jne    1190 <read_line+0x40>
mov    %r13,%rbx
cmpq   $0x0,0x10(%rsp)
jne    1245 <read_line+0xf5>
jmp    1222 <read_line+0xd2>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %r13d,%r13d
jmp    1245 <read_line+0xf5>
lea    0x20(%rsp),%rsi
movb   $0x0,-0x1(%r14,%rsi,1)
mov    %r13,%rdi
add    %r15,%rdi
call   1040 <strcpy@plt>
mov    %r13,%rax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @read_line(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca [512 x i8]
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
store i32* %0, i32** %3
store i8* %1, i8** %4
store i8* null, i8** %6
store i64 0, i64* %7
br label %11
11: ; preds = %61, %2
%12 = load i32*, i32** %3
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%14 = call i32* @get_line(i32* %12, i8* %13, i32 512)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %63
16: ; preds = %11
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp ugt i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = load i64, i64* %8
%23 = sub i64 %22, 1
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br label %28
28: ; preds = %21, %16
%29 = phi i1 [ false, %16 ], [ %27, %21 ]
%30 = zext i1 %29 to i32
store i32 %30, i32* %9
%31 = load i64, i64* %7
%32 = load i64, i64* %8
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = sub i64 %32, %34
%36 = add i64 %31, %35
store i64 %36, i64* %10
%37 = load i8*, i8** %4
%38 = load i64, i64* %10
%39 = add i64 %38, 1
%40 = call i8* @realloc(i8* %37, i64 %39)
store i8* %40, i8** %6
%41 = load i8*, i8** %6
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %44
43: ; preds = %28
br label %63
44: ; preds = %28
%45 = load i32, i32* %9
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i64, i64* %8
%49 = add i64 %48, -1
store i64 %49, i64* %8
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %49
store i8 0, i8* %50
br label %51
51: ; preds = %47, %44
%52 = load i8*, i8** %6
store i8* %52, i8** %4
%53 = load i8*, i8** %4
%54 = load i64, i64* %7
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%57 = call i32 @strcpy(i8* %55, i8* %56)
%58 = load i32, i32* %9
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %51
br label %63
61: ; preds = %51
%62 = load i64, i64* %10
store i64 %62, i64* %7
br label %11
63: ; preds = %60, %43, %11
%64 = load i8*, i8** %6
%65 = icmp eq i8* %64, null
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i8*, i8** %4
%68 = call i32 @free(i8* %67)
store i8* null, i8** %4
br label %69
69: ; preds = %66, %63
%70 = load i8*, i8** %4
ret i8* %70
}

LLVM IR (-O2):
define i8* @read_line(i32* %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = call i32* @get_line(i32* %0, i8* %4, i32 512)
%6 = icmp eq i32* %5, null
br i1 %6, label %37, label %7
7: ; preds = %2, %31
%8 = phi i8* [ %23, %31 ], [ %1, %2 ]
%9 = phi i64 [ %21, %31 ], [ 0, %2 ]
%10 = call i64 @strlen(i8* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %7
%13 = add i64 %10, -1
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br label %17
17: ; preds = %12, %7
%18 = phi i1 [ false, %7 ], [ %16, %12 ]
%19 = sext i1 %18 to i64
%20 = add i64 %10, %9
%21 = add i64 %20, %19
%22 = add i64 %21, 1
%23 = call i8* @realloc(i8* %8, i64 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %37, label %25
25: ; preds = %17
br i1 %18, label %26, label %31
26: ; preds = %25
%27 = add i64 %10, -1
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds i8, i8* %23, i64 %9
%30 = call i32 @strcpy(i8* %29, i8* %4)
br label %40
31: ; preds = %25
%32 = getelementptr inbounds i8, i8* %23, i64 %9
%33 = call i32 @strcpy(i8* %32, i8* %4)
%34 = call i32* @get_line(i32* %0, i8* %4, i32 512)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %7
36: ; preds = %31
br i1 %6, label %37, label %40
37: ; preds = %17, %2, %36
%38 = phi i8* [ %23, %36 ], [ %1, %2 ], [ %8, %17 ]
%39 = call i32 @free(i8* %38)
br label %40
40: ; preds = %26, %37, %36
%41 = phi i8* [ null, %37 ], [ %23, %36 ], [ %23, %26 ]
ret i8* %41
}