#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 getchar();
scalar_t__ malloc(int);
char *getstring() {
  char *buf = (char *)malloc(sizeof(char) * 2048);
  int i;
  for (i = 0; i < 2048; i++) {
    buf[i] = getchar();
    if (buf[i] == '\n') {
      buf[i] = '\0';
      break;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<getstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x800,%edi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x800,-0xc(%rbp)
jge    1197 <getstring+0x77>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1184 <getstring+0x64>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1197 <getstring+0x77>
jmp    1189 <getstring+0x69>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113d <getstring+0x1d>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstring>:
push   %r14
push   %rbx
push   %rax
mov    $0x800,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     115e <getstring+0x3e>
add    $0x1,%rbx
cmp    $0x800,%rbx
jne    1140 <getstring+0x20>
jmp    1163 <getstring+0x43>
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @getstring() {
%1 = tail call i64 @malloc(i32 2048)
%2 = inttoptr i64 %1 to i8*
br label %3
3: ; preds = %0, %11
%4 = phi i64 [ 0, %0 ], [ %12, %11 ]
%5 = tail call signext i8 (...) @getchar()
%6 = getelementptr inbounds i8, i8* %2, i64 %4
store i8 %5, i8* %6
%7 = icmp eq i8 %5, 10
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = and i64 %4, 4294967295
%10 = getelementptr inbounds i8, i8* %2, i64 %9
store i8 0, i8* %10
br label %14
11: ; preds = %3
%12 = add nuw nsw i64 %4, 1
%13 = icmp eq i64 %12, 2048
br i1 %13, label %14, label %3
14: ; preds = %11, %8
ret i8* %2
}