#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 /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ ReverseStringInPlace(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char getchar();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ realloc(char *, size_t);
int /*<<< orphan*/ strlen(char *);
int main(int argc, const char **argv) {
  size_t bufferCapacity = 128;
  char *buffer = (char *)malloc(sizeof(char) * 128);
  if (!buffer) {
    errno = ENOMEM;
    return 1;
  }
  size_t bufferCount = 0;
  char c = 0;
  while (1) {
    bufferCount = 0;
    printf(">>> ");
    while ((c = getchar()) != '\n') {
      if (bufferCount >= bufferCapacity - 1) {
        bufferCapacity = (bufferCapacity > 0) ? bufferCapacity * 2 : 128;
        buffer = (char *)realloc(buffer, bufferCapacity);
        if (!buffer) {
          errno = ENOMEM;
          return 1;
        }
      }
      buffer[bufferCount] = c;
      ++bufferCount;
    }
    buffer[bufferCount] = 0;
    ReverseStringInPlace(buffer, strlen(buffer));
    printf("%s\n", buffer);
  }
  free(buffer);
  buffer = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x80,-0x18(%rbp)
mov    $0x80,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <main+0x55>
mov    0x2e42(%rip),%rax
mov    (%rax),%ecx
mov    0x2e31(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12d1 <main+0x171>
movq   $0x0,-0x28(%rbp)
movb   $0x0,-0x29(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %al,-0x29(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     1292 <main+0x132>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     1273 <main+0x113>
cmpq   $0x0,-0x18(%rbp)
jbe    121e <main+0xbe>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    122c <main+0xcc>
mov    $0x80,%eax
mov    %rax,-0x38(%rbp)
jmp    122c <main+0xcc>
mov    -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    126e <main+0x10e>
mov    0x2d89(%rip),%rax
mov    (%rax),%ecx
mov    0x2d78(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12d1 <main+0x171>
jmp    1273 <main+0x113>
mov    -0x29(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11d7 <main+0x77>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
call   1030 <ReverseStringInPlace@plt>
mov    -0x20(%rbp),%rsi
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11c1 <main+0x61>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x80,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1231 <main+0xc1>
mov    %rax,%r15
mov    $0x80,%r12d
mov    $0x80,%r13d
jmp    11c2 <main+0x52>
nop
xor    %r14d,%r14d
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <ReverseStringInPlace@plt>
mov    %r15,%rdi
call   1040 <puts@plt>
lea    0xe37(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
cmp    $0xa,%al
je     11a0 <main+0x30>
mov    %eax,%ebp
xor    %r14d,%r14d
jmp    1205 <main+0x95>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %bpl,(%r15,%r14,1)
add    $0x1,%r14
xor    %eax,%eax
call   1070 <getchar@plt>
mov    %eax,%ebp
cmp    $0xa,%al
je     11a3 <main+0x33>
lea    -0x1(%r13),%rax
cmp    %rax,%r14
jb     11f0 <main+0x80>
mov    %r13,%rbx
add    %r13,%rbx
test   %r13,%r13
cmove  %r12,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <realloc@plt>
mov    %rax,%r15
mov    %rbx,%r13
test   %rax,%rax
jne    11f0 <main+0x80>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    0x2d97(%rip),%rcx
mov    %eax,(%rcx)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [5 x i8] c">>> \00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 128, i64* %6
%10 = call i64 @malloc(i32 128)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = load i32, i32* @ENOMEM
store i32 %15, i32* @errno
store i32 1, i32* %3
br label %63
16: ; preds = %2
store i64 0, i64* %8
store i8 0, i8* %9
br label %17
17: ; preds = %16, %53
store i64 0, i64* %8
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %46, %17
%20 = call signext i8 (...) @getchar()
store i8 %20, i8* %9
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 10
br i1 %22, label %23, label %53
23: ; preds = %19
%24 = load i64, i64* %8
%25 = load i64, i64* %6
%26 = sub i64 %25, 1
%27 = icmp uge i64 %24, %26
br i1 %27, label %28, label %46
28: ; preds = %23
%29 = load i64, i64* %6
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i64, i64* %6
%33 = mul i64 %32, 2
br label %35
34: ; preds = %28
br label %35
35: ; preds = %34, %31
%36 = phi i64 [ %33, %31 ], [ 128, %34 ]
store i64 %36, i64* %6
%37 = load i8*, i8** %7
%38 = load i64, i64* %6
%39 = call i64 @realloc(i8* %37, i64 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = icmp ne i8* %41, null
br i1 %42, label %45, label %43
43: ; preds = %35
%44 = load i32, i32* @ENOMEM
store i32 %44, i32* @errno
store i32 1, i32* %3
br label %63
45: ; preds = %35
br label %46
46: ; preds = %45, %23
%47 = load i8, i8* %9
%48 = load i8*, i8** %7
%49 = load i64, i64* %8
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 %47, i8* %50
%51 = load i64, i64* %8
%52 = add i64 %51, 1
store i64 %52, i64* %8
br label %19
53: ; preds = %19
%54 = load i8*, i8** %7
%55 = load i64, i64* %8
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 0, i8* %56
%57 = load i8*, i8** %7
%58 = load i8*, i8** %7
%59 = call i32 @strlen(i8* %58)
%60 = call i32 @ReverseStringInPlace(i8* %57, i32 %59)
%61 = load i8*, i8** %7
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %61)
br label %17
63: ; preds = %43, %14
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@.str = [5 x i8] c">>> \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 @malloc(i32 128)
%4 = icmp eq i64 %3, 0
br i1 %4, label %42, label %5
5: ; preds = %2
%6 = inttoptr i64 %3 to i8*
br label %7
7: ; preds = %34, %5
%8 = phi i64 [ 128, %5 ], [ %35, %34 ]
%9 = phi i8* [ %6, %5 ], [ %36, %34 ]
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = tail call signext i8 (...) @getchar()
%12 = icmp eq i8 %11, 10
br i1 %12, label %34, label %13
13: ; preds = %7, %27
%14 = phi i8 [ %32, %27 ], [ %11, %7 ]
%15 = phi i64 [ %31, %27 ], [ 0, %7 ]
%16 = phi i8* [ %29, %27 ], [ %9, %7 ]
%17 = phi i64 [ %28, %27 ], [ %8, %7 ]
%18 = add i64 %17, -1
%19 = icmp ult i64 %15, %18
br i1 %19, label %27, label %20
20: ; preds = %13
%21 = icmp eq i64 %17, 0
%22 = shl i64 %17, 1
%23 = select i1 %21, i64 128, i64 %22
%24 = tail call i64 @realloc(i8* %16, i64 %23)
%25 = inttoptr i64 %24 to i8*
%26 = icmp eq i64 %24, 0
br i1 %26, label %42, label %27
27: ; preds = %20, %13
%28 = phi i64 [ %23, %20 ], [ %17, %13 ]
%29 = phi i8* [ %25, %20 ], [ %16, %13 ]
%30 = getelementptr inbounds i8, i8* %29, i64 %15
store i8 %14, i8* %30
%31 = add i64 %15, 1
%32 = tail call signext i8 (...) @getchar()
%33 = icmp eq i8 %32, 10
br i1 %33, label %34, label %13
34: ; preds = %27, %7
%35 = phi i64 [ %8, %7 ], [ %28, %27 ]
%36 = phi i8* [ %9, %7 ], [ %29, %27 ]
%37 = phi i64 [ 0, %7 ], [ %31, %27 ]
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8 0, i8* %38
%39 = tail call i32 @strlen(i8* %36)
%40 = tail call i32 @ReverseStringInPlace(i8* %36, i32 %39)
%41 = tail call i32 @puts(i8* dereferenceable(1) %36)
br label %7
42: ; preds = %20, %2
%43 = load i32, i32* @ENOMEM
store i32 %43, i32* @errno
ret i32 1
}