#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 */
typedef int wint_t;
typedef char wchar_t;

/* Variables and functions */
int MB_CUR_MAX;
char WEOF;
int /*<<< orphan*/ free(char *);
char getwchar();
scalar_t__ malloc(size_t);
int wcslen(char *);
int /*<<< orphan*/ wcstombs(char *, char *, size_t);
char *getstr() {
  wchar_t *str;
  char *ret;
  wint_t i = 1;
  size_t j;
  size_t WCHAR_MAX_SIZE = sizeof(wchar_t) * 200;
  str = (wchar_t *)malloc(WCHAR_MAX_SIZE);
  while ('\n' != (str[i - 1] = getwchar())) {
    if (str[i - 1] == WEOF)
      break;
    i++;
  }
  str[i - 1] = L'\0';
  j = MB_CUR_MAX * (wcslen(str) + 1);
  ret = (char *)malloc(j);
  wcstombs(ret, str, j);
  ret[j - 1] = '\0';
  free(str);
  return ret;
}


x86 assembly (clang, -O0):
<getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x1,-0x14(%rbp)
movq   $0xc8,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getwchar@plt>
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
je     11d1 <getstr+0x81>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
mov    0x2e25(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11c3 <getstr+0x73>
jmp    11d1 <getstr+0x81>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1174 <getstr+0x24>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <wcslen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    $0x1,%ecx
imul   %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <wcstombs@plt>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstr>:
push   %r15
push   %r14
push   %rbx
mov    $0xc8,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    0x2e6f(%rip),%r15
mov    %rax,%rbx
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <getwchar@plt>
mov    %al,(%rbx)
add    $0x1,%rbx
cmp    $0xa,%al
je     1186 <getstr+0x36>
cmp    (%r15),%al
jne    1170 <getstr+0x20>
movb   $0x0,-0x1(%rbx)
mov    0x2e67(%rip),%rax
movslq (%rax),%r15
mov    %r14,%rdi
call   1050 <wcslen@plt>
movslq %eax,%rbx
add    $0x1,%rbx
imul   %r15,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <wcstombs@plt>
movb   $0x0,-0x1(%rbx,%r15,1)
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@WEOF = global i8 0
@MB_CUR_MAX = global i32 0
define i8* @getstr() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
store i32 1, i32* %3
store i64 200, i64* %5
%6 = load i64, i64* %5
%7 = call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %1
br label %9
9: ; preds = %30, %0
%10 = call signext i8 (...) @getwchar()
%11 = load i8*, i8** %1
%12 = load i32, i32* %3
%13 = sub nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
store i8 %10, i8* %15
%16 = sext i8 %10 to i32
%17 = icmp ne i32 10, %16
br i1 %17, label %18, label %33
18: ; preds = %9
%19 = load i8*, i8** %1
%20 = load i32, i32* %3
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = load i8, i8* @WEOF
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %25, %27
br i1 %28, label %29, label %30
29: ; preds = %18
br label %33
30: ; preds = %18
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
br label %9
33: ; preds = %29, %9
%34 = load i8*, i8** %1
%35 = load i32, i32* %3
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* @MB_CUR_MAX
%40 = load i8*, i8** %1
%41 = call i32 @wcslen(i8* %40)
%42 = add nsw i32 %41, 1
%43 = mul nsw i32 %39, %42
%44 = sext i32 %43 to i64
store i64 %44, i64* %4
%45 = load i64, i64* %4
%46 = call i64 @malloc(i64 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %2
%48 = load i8*, i8** %2
%49 = load i8*, i8** %1
%50 = load i64, i64* %4
%51 = call i32 @wcstombs(i8* %48, i8* %49, i64 %50)
%52 = load i8*, i8** %2
%53 = load i64, i64* %4
%54 = sub i64 %53, 1
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
%56 = load i8*, i8** %1
%57 = call i32 @free(i8* %56)
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@WEOF = global i8 0
@MB_CUR_MAX = global i32 0
define i8* @getstr() {
%1 = tail call i64 @malloc(i64 200)
%2 = inttoptr i64 %1 to i8*
br label %3
3: ; preds = %3, %0
%4 = phi i64 [ %12, %3 ], [ 1, %0 ]
%5 = tail call signext i8 (...) @getwchar()
%6 = add nsw i64 %4, -1
%7 = getelementptr inbounds i8, i8* %2, i64 %6
store i8 %5, i8* %7
%8 = icmp eq i8 %5, 10
%9 = load i8, i8* @WEOF
%10 = icmp eq i8 %5, %9
%11 = select i1 %8, i1 true, i1 %10
%12 = add nuw i64 %4, 1
br i1 %11, label %13, label %3
13: ; preds = %3
%14 = getelementptr inbounds i8, i8* %2, i64 %6
store i8 0, i8* %14
%15 = load i32, i32* @MB_CUR_MAX
%16 = tail call i32 @wcslen(i8* %2)
%17 = add nsw i32 %16, 1
%18 = mul nsw i32 %17, %15
%19 = sext i32 %18 to i64
%20 = tail call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
%22 = tail call i32 @wcstombs(i8* %21, i8* %2, i64 %19)
%23 = add nsw i64 %19, -1
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 0, i8* %24
%25 = tail call i32 @free(i8* %2)
ret i8* %21
}