#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 *calloc(int, int);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *reverse(char *stringRev) {
  int length = strlen(stringRev) - 1;
  char *ret = calloc(length, sizeof(char));
  int i;
  for (i = 0; i < length + 1; i++) {
    strncat(ret, &stringRev[length - i], 1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    119f <reverse+0x6f>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%eax
sub    -0x1c(%rbp),%eax
cltq
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <strncat@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1163 <reverse+0x33>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    -0x1(%r15),%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
test   %r15d,%r15d
jle    118a <reverse+0x5a>
mov    %ebp,%eax
mov    %r15d,%ebp
add    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <strncat@plt>
add    $0xffffffffffffffff,%rbx
add    $0xffffffffffffffff,%rbp
jne    1170 <reverse+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @reverse(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
%8 = sub nsw i32 %7, 1
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = call i8* @calloc(i32 %9, i32 1)
store i8* %10, i8** %4
store i32 0, i32* %5
br label %11
11: ; preds = %25, %1
%12 = load i32, i32* %5
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
%15 = icmp slt i32 %12, %14
br i1 %15, label %16, label %28
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = load i8*, i8** %2
%19 = load i32, i32* %3
%20 = load i32, i32* %5
%21 = sub nsw i32 %19, %20
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %18, i64 %22
%24 = call i32 @strncat(i8* %17, i8* %23, i32 1)
br label %25
25: ; preds = %16
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %11
28: ; preds = %11
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
define i8* @reverse(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = tail call i8* @calloc(i32 %3, i32 1)
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %16
6: ; preds = %1
%7 = zext i32 %3 to i64
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %6, %9
%10 = phi i64 [ 0, %6 ], [ %14, %9 ]
%11 = sub nsw i64 %7, %10
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 @strncat(i8* %4, i8* %12, i32 1)
%14 = add nuw nsw i64 %10, 1
%15 = icmp eq i64 %14, %8
br i1 %15, label %16, label %9
16: ; preds = %9, %1
ret i8* %4
}