#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 */
scalar_t__ malloc(size_t);
size_t strlen(char *);
char *substr(char *, size_t);
char *str_reverse(char *str) {
  size_t len = strlen(str);
  static char *reverse = "";
  static int i = 0;
  if (i == 0) {
    reverse = (char *)malloc(len * sizeof(char));
  }
  if (len < 1) {
    reverse[i] = '\0';
    i = 0;
    return reverse;
  }
  reverse[i++] = str[len - 1];
  return str_reverse(substr(str, len));
}


x86 assembly (clang, -O0):
<str_reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,0x2eec(%rip)
jne    117a <str_reverse+0x3a>
mov    -0x18(%rbp),%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,0x2ec6(%rip)
cmpq   $0x1,-0x18(%rbp)
jae    11b1 <str_reverse+0x71>
mov    0x2eb4(%rip),%rax
movslq 0x2eb9(%rip),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,0x2eab(%rip)
mov    0x2e98(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11f7 <str_reverse+0xb7>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,1),%dl
mov    0x2e79(%rip),%rax
mov    0x2e7f(%rip),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,0x2e74(%rip)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <substr@plt>
mov    %rax,%rdi
call   1050 <str_reverse@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_reverse>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
cmpl   $0x0,0x2ef2(%rip)
jne    1163 <str_reverse+0x33>
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,0x2ed5(%rip)
test   %rbx,%rbx
je     1198 <str_reverse+0x68>
movzbl -0x1(%rbx,%r14,1),%eax
mov    0x2ec3(%rip),%rcx
movslq 0x2ec8(%rip),%rdx
lea    0x1(%rdx),%esi
mov    %esi,0x2ebf(%rip)
mov    %al,(%rcx,%rdx,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <substr@plt>
mov    %rax,%r14
jmp    1140 <str_reverse+0x10>
mov    0x2e99(%rip),%rax
movslq 0x2e9e(%rip),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,0x2e90(%rip)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@str_reverse.reverse = internal global i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i32 0, i32 0)
@.str = [1 x i8] zeroinitializer
@str_reverse.i = internal global i32 0
define i8* @str_reverse(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @strlen(i8* %5)
store i64 %6, i64* %4
%7 = load i32, i32* @str_reverse.i
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i64, i64* %4
%11 = mul i64 %10, 1
%12 = call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** @str_reverse.reverse
br label %14
14: ; preds = %9, %1
%15 = load i64, i64* %4
%16 = icmp ult i64 %15, 1
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i8*, i8** @str_reverse.reverse
%19 = load i32, i32* @str_reverse.i
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
store i32 0, i32* @str_reverse.i
%22 = load i8*, i8** @str_reverse.reverse
store i8* %22, i8** %2
br label %38
23: ; preds = %14
%24 = load i8*, i8** %3
%25 = load i64, i64* %4
%26 = sub i64 %25, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** @str_reverse.reverse
%30 = load i32, i32* @str_reverse.i
%31 = add nsw i32 %30, 1
store i32 %31, i32* @str_reverse.i
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 %28, i8* %33
%34 = load i8*, i8** %3
%35 = load i64, i64* %4
%36 = call i8* @substr(i8* %34, i64 %35)
%37 = call i8* @str_reverse(i8* %36)
store i8* %37, i8** %2
br label %38
38: ; preds = %23, %17
%39 = load i8*, i8** %2
ret i8* %39
}

LLVM IR (-O2):
@str_reverse.reverse = internal unnamed_addr global i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0)
@.str = [1 x i8] zeroinitializer
@str_reverse.i = internal unnamed_addr global i32 0
define i8* @str_reverse(i8* %0) {
br label %2
2: ; preds = %17, %1
%3 = phi i8* [ %0, %1 ], [ %26, %17 ]
%4 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%5 = load i32, i32* @str_reverse.i
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i64 @malloc(i64 %4)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** @str_reverse.reverse
br label %10
10: ; preds = %7, %2
%11 = icmp eq i64 %4, 0
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = load i8*, i8** @str_reverse.reverse
%14 = load i32, i32* @str_reverse.i
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
store i32 0, i32* @str_reverse.i
ret i8* %13
17: ; preds = %10
%18 = add i64 %4, -1
%19 = getelementptr inbounds i8, i8* %3, i64 %18
%20 = load i8, i8* %19
%21 = load i8*, i8** @str_reverse.reverse
%22 = load i32, i32* @str_reverse.i
%23 = add nsw i32 %22, 1
store i32 %23, i32* @str_reverse.i
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %20, i8* %25
%26 = tail call i8* @substr(i8* %3, i64 %4)
br label %2
}