#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*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("ERROR: Run as ./a.out <stringToReverse>\n");
    exit(-1);
  }
  char *orig = argv[1];
  int size = strlen(orig);
  char reverse[size];
  memset(reverse, 0, size);
  int i = size;
  while (i >= 0) {
    printf("Char = %c\n", orig[i]);
    char *tmp = malloc(2);
    tmp[0] = orig[i];
    tmp[1] = '\0';
    strcat(reverse, tmp);
    i = i - 1;
  }
  printf("Orig     : %s\n", orig);
  printf("Reverse  : %s\n", reverse);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1198 <main+0x38>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     124c <main+0xec>
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1060 <malloc@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x40(%rbp),%rax
mov    %cl,(%rax)
mov    -0x40(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x40(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11e6 <main+0x86>
mov    -0x18(%rbp),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x48(%rbp),%rsi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jle    1226 <main+0xb6>
mov    0x8(%rsi),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %eax,%ebx
mov    %rsp,%r14
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1060 <memset@plt>
test   %r15d,%r15d
js     11f5 <main+0x85>
lea    0xe41(%rip),%r15
nop
movsbl (%r12,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%edi
call   1070 <malloc@plt>
movzbl (%r12,%rbx,1),%ecx
mov    %cl,(%rax)
movb   $0x0,0x1(%rax)
mov    %r14,%rdi
mov    %rax,%rsi
call   1080 <strcat@plt>
add    $0xffffffffffffffff,%rbx
jb     11c0 <main+0x50>
lea    0xe0f(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe0d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"ERROR: Run as ./a.out <stringToReverse>\0A\00"
@.str.1 = [11 x i8] c"Char = %c\0A\00"
@.str.2 = [15 x i8] c"Orig : %s\0A\00"
@.str.3 = [15 x i8] c"Reverse : %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %8
%26 = alloca i8, i64 %24
store i64 %24, i64* %9
%27 = load i32, i32* %7
%28 = call i32 @memset(i8* %26, i32 0, i32 %27)
%29 = load i32, i32* %7
store i32 %29, i32* %10
br label %30
30: ; preds = %33, %17
%31 = load i32, i32* %10
%32 = icmp sge i32 %31, 0
br i1 %32, label %33, label %55
33: ; preds = %30
%34 = load i8*, i8** %6
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %39)
%41 = call i8* @malloc(i32 2)
store i8* %41, i8** %11
%42 = load i8*, i8** %6
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = load i8*, i8** %11
%48 = getelementptr inbounds i8, i8* %47, i64 0
store i8 %46, i8* %48
%49 = load i8*, i8** %11
%50 = getelementptr inbounds i8, i8* %49, i64 1
store i8 0, i8* %50
%51 = load i8*, i8** %11
%52 = call i32 @strcat(i8* %26, i8* %51)
%53 = load i32, i32* %10
%54 = sub nsw i32 %53, 1
store i32 %54, i32* %10
br label %30
55: ; preds = %30
%56 = load i8*, i8** %6
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %26)
store i32 0, i32* %3
%59 = load i8*, i8** %8
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"Char = %c\0A\00"
@.str.2 = [15 x i8] c"Orig : %s\0A\00"
@.str.3 = [15 x i8] c"Reverse : %s\0A\00"
@str = [40 x i8] c"ERROR: Run as ./a.out <stringToReverse>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strlen(i8* %9)
%11 = zext i32 %10 to i64
%12 = alloca i8, i64 %11
%13 = call i32 @memset(i8* %12, i32 0, i32 %10)
%14 = icmp sgt i32 %10, -1
br i1 %14, label %15, label %27
15: ; preds = %7, %15
%16 = phi i64 [ %25, %15 ], [ %11, %7 ]
%17 = getelementptr inbounds i8, i8* %9, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = call dereferenceable_or_null(2) i8* @malloc(i32 2)
%22 = load i8, i8* %17
store i8 %22, i8* %21
%23 = getelementptr inbounds i8, i8* %21, i64 1
store i8 0, i8* %23
%24 = call i32 @strcat(i8* %12, i8* %21)
%25 = add nsw i64 %16, -1
%26 = icmp eq i64 %16, 0
br i1 %26, label %27, label %15
27: ; preds = %15, %7
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %12)
ret i32 0
}