#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();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, int);
char *get_message(char *message) {
  int c;
  int i = 0;
  printf("Give your message: ");
  message = (char *)malloc(sizeof(char));
  c = getchar();
  while ((c = getchar()) != '\n') {
    *(message + i) = c;
    i++;
    message = realloc(message, (i + 1) * sizeof(char));
  }
  message[i] = '\0';
  return message;
}


x86 assembly (clang, -O0):
<get_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xea6(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xa,%eax
je     11c3 <get_message+0x83>
mov    -0xc(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    1177 <get_message+0x37>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_message>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getchar@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
je     11a7 <get_message+0x67>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rsi
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
add    $0x2,%esi
mov    %r14,%rdi
call   1060 <realloc@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%eax
jne    1180 <get_message+0x40>
mov    %ebx,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Give your message: \00"
define i8* @get_message(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
%5 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%6 = call i64 @malloc(i32 1)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %2
%8 = call i32 (...) @getchar()
store i32 %8, i32* %3
br label %9
9: ; preds = %12, %1
%10 = call i32 (...) @getchar()
store i32 %10, i32* %3
%11 = icmp ne i32 %10, 10
br i1 %11, label %12, label %28
12: ; preds = %9
%13 = load i32, i32* %3
%14 = trunc i32 %13 to i8
%15 = load i8*, i8** %2
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 %14, i8* %18
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
store i32 %20, i32* %4
%21 = load i8*, i8** %2
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = mul i64 %24, 1
%26 = trunc i64 %25 to i32
%27 = call i8* @realloc(i8* %21, i32 %26)
store i8* %27, i8** %2
br label %9
28: ; preds = %9
%29 = load i8*, i8** %2
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
@.str = [20 x i8] c"Give your message: \00"
define i8* @get_message(i8* nocapture readnone %0) {
%2 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%3 = tail call i64 @malloc(i32 1)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 (...) @getchar()
%6 = tail call i32 (...) @getchar()
%7 = icmp eq i32 %6, 10
br i1 %7, label %22, label %8
8: ; preds = %1, %8
%9 = phi i64 [ %14, %8 ], [ 0, %1 ]
%10 = phi i32 [ %18, %8 ], [ %6, %1 ]
%11 = phi i8* [ %17, %8 ], [ %4, %1 ]
%12 = trunc i32 %10 to i8
%13 = getelementptr inbounds i8, i8* %11, i64 %9
store i8 %12, i8* %13
%14 = add nuw i64 %9, 1
%15 = trunc i64 %9 to i32
%16 = add i32 %15, 2
%17 = tail call i8* @realloc(i8* %11, i32 %16)
%18 = tail call i32 (...) @getchar()
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %8
20: ; preds = %8
%21 = and i64 %14, 4294967295
br label %22
22: ; preds = %20, %1
%23 = phi i8* [ %4, %1 ], [ %17, %20 ]
%24 = phi i64 [ 0, %1 ], [ %21, %20 ]
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
ret i8* %23
}