#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 *malloc(unsigned long);
int /*<<< orphan*/ mgetchar();
unsigned long mgetuleb128();
char *mgetstring(void) {
  unsigned long len = mgetuleb128();
  char *ret = malloc(len + 1);
  char *p = ret;
  unsigned long i = 0;
  while (i++ < len)
    *p++ = mgetchar();
  ret[len] = 0;
  return ret;
}


x86 assembly (clang, -O0):
<mgetstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <mgetuleb128@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
cmp    -0x8(%rbp),%rax
jae    119c <mgetstring+0x6c>
mov    $0x0,%al
call   1030 <mgetchar@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1164 <mgetstring+0x34>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mgetstring>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <mgetuleb128@plt>
mov    %rax,%r14
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %r14,%r14
je     1177 <mgetstring+0x47>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%rbx),%r12
xor    %eax,%eax
call   1030 <mgetchar@plt>
mov    %al,(%r15,%rbx,1)
mov    %r12,%rbx
cmp    %r12,%r14
jne    1160 <mgetstring+0x30>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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