#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 EOF;
char getchar();
char *malloc(int);
char *read(int m_size) {
  char *line;
  int size = 0;
  line = malloc(m_size + 1);
  while ((size < m_size) && (line[size] = getchar()) != EOF) {
    size++;
  }
  line[size] = 0;
  return line;
}


x86 assembly (clang, -O0):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
cmp    -0x4(%rbp),%ecx
mov    %al,-0x15(%rbp)
jge    1179 <read+0x59>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    -0x10(%rbp),%rcx
movslq -0x14(%rbp),%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%eax
mov    0x2e7a(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1189 <read+0x69>
jmp    1197 <read+0x77>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1141 <read+0x21>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
lea    0x1(%r14),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    116f <read+0x4f>
mov    %r14d,%r14d
mov    0x2ea2(%rip),%r12
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %al,(%r15,%rbx,1)
cmp    (%r12),%al
je     1171 <read+0x51>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1150 <read+0x30>
mov    %r14d,%ebx
jmp    1171 <read+0x51>
xor    %ebx,%ebx
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i8 0
define i8* @read(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%5 = load i32, i32* %2
%6 = add nsw i32 %5, 1
%7 = call i8* @malloc(i32 %6)
store i8* %7, i8** %3
br label %8
8: ; preds = %24, %1
%9 = load i32, i32* %4
%10 = load i32, i32* %2
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = call signext i8 (...) @getchar()
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8 %13, i8* %17
%18 = sext i8 %13 to i32
%19 = load i8, i8* @EOF
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %18, %20
br label %22
22: ; preds = %12, %8
%23 = phi i1 [ false, %8 ], [ %21, %12 ]
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %8
27: ; preds = %22
%28 = load i8*, i8** %3
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %3
ret i8* %32
}

LLVM IR (-O2):
@EOF = global i8 0
define noalias i8* @read(i32 %0) {
%2 = add nsw i32 %0, 1
%3 = tail call i8* @malloc(i32 %2)
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %18
5: ; preds = %1
%6 = zext i32 %0 to i64
br label %7
7: ; preds = %5, %13
%8 = phi i64 [ 0, %5 ], [ %14, %13 ]
%9 = tail call signext i8 (...) @getchar()
%10 = getelementptr inbounds i8, i8* %3, i64 %8
store i8 %9, i8* %10
%11 = load i8, i8* @EOF
%12 = icmp eq i8 %9, %11
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = add nuw nsw i64 %8, 1
%15 = icmp eq i64 %14, %6
br i1 %15, label %18, label %7
16: ; preds = %7
%17 = trunc i64 %8 to i32
br label %18
18: ; preds = %13, %16, %1
%19 = phi i32 [ 0, %1 ], [ %17, %16 ], [ %0, %13 ]
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %3, i64 %20
store i8 0, i8* %21
ret i8* %3
}