#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 */
int getchar();
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
char *read_input(void) {
  char temp[1000];
  int c, n = 0;
  while ((c = getchar()) != '\n')
    temp[n++] = c;
  temp[n] = '\0';
  char *res = malloc(n + 1);
  strcpy(res, temp);
  return res;
}


x86 assembly (clang, -O0):
<read_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
movl   $0x0,-0x3f8(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x3f4(%rbp)
cmp    $0xa,%eax
je     1182 <read_input+0x52>
mov    -0x3f4(%rbp),%eax
mov    %al,%cl
mov    -0x3f8(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x3f8(%rbp)
cltq
mov    %cl,-0x3f0(%rbp,%rax,1)
jmp    1145 <read_input+0x15>
movslq -0x3f8(%rbp),%rax
movb   $0x0,-0x3f0(%rbp,%rax,1)
mov    -0x3f8(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x400(%rbp)
mov    -0x400(%rbp),%rdi
lea    -0x3f0(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x400(%rbp),%rax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_input>:
push   %rbx
sub    $0x3f0,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
je     1163 <read_input+0x33>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %al,(%rsp,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
jne    1150 <read_input+0x20>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
add    $0x1,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x3f0,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i8* @read_input() {
%1 = alloca [1000 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %3
br label %5
5: ; preds = %8, %0
%6 = call i32 (...) @getchar()
store i32 %6, i32* %2
%7 = icmp ne i32 %6, 10
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = load i32, i32* %2
%10 = trunc i32 %9 to i8
%11 = load i32, i32* %3
%12 = add nsw i32 %11, 1
store i32 %12, i32* %3
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %13
store i8 %10, i8* %14
br label %5
15: ; preds = %5
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 %17
store i8 0, i8* %18
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = load i8*, i8** %4
ret i8* %25
}

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