#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(int);
size_t strlen(char const *);
char *astrrev(const char *s) {
  char *result = NULL;
  size_t ii;
  result = (char *)malloc((strlen(s) + 1) * sizeof(char));
  result[strlen(s)] = '\0';
  for (ii = 0; ii < strlen(s); ++ii) {
    result[ii] = s[strlen(s) - ii - 1];
  }
  return result;
}


x86 assembly (clang, -O0):
<astrrev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
jae    11d4 <astrrev+0xb4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
sub    -0x18(%rbp),%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1174 <astrrev+0x54>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<astrrev>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    0x1(%rax),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
movb   $0x0,(%r15,%rax,1)
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     117f <astrrev+0x5f>
xor    %ebx,%ebx
mov    %r14,%r12
nopw   0x0(%rax,%rax,1)
movzbl -0x1(%rax,%r12,1),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
add    $0xffffffffffffffff,%r12
cmp    %rbx,%rax
ja     1160 <astrrev+0x40>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @astrrev(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %2
store i8* null, i8** %3
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
%7 = add i64 %6, 1
%8 = mul i64 %7, 1
%9 = trunc i64 %8 to i32
%10 = call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = load i8*, i8** %2
%14 = call i64 @strlen(i8* %13)
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8 0, i8* %15
store i64 0, i64* %4
br label %16
16: ; preds = %33, %1
%17 = load i64, i64* %4
%18 = load i8*, i8** %2
%19 = call i64 @strlen(i8* %18)
%20 = icmp ult i64 %17, %19
br i1 %20, label %21, label %36
21: ; preds = %16
%22 = load i8*, i8** %2
%23 = load i8*, i8** %2
%24 = call i64 @strlen(i8* %23)
%25 = load i64, i64* %4
%26 = sub i64 %24, %25
%27 = sub i64 %26, 1
%28 = getelementptr inbounds i8, i8* %22, i64 %27
%29 = load i8, i8* %28
%30 = load i8*, i8** %3
%31 = load i64, i64* %4
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 %29, i8* %32
br label %33
33: ; preds = %21
%34 = load i64, i64* %4
%35 = add i64 %34, 1
store i64 %35, i64* %4
br label %16
36: ; preds = %16
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
define i8* @astrrev(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = trunc i64 %2 to i32
%4 = add i32 %3, 1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = getelementptr inbounds i8, i8* %6, i64 %7
store i8 0, i8* %8
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = icmp eq i64 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %1, %11
%12 = phi i64 [ %20, %11 ], [ %9, %1 ]
%13 = phi i64 [ %19, %11 ], [ 0, %1 ]
%14 = xor i64 %13, -1
%15 = add i64 %12, %14
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = getelementptr inbounds i8, i8* %6, i64 %13
store i8 %17, i8* %18
%19 = add nuw i64 %13, 1
%20 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%21 = icmp ugt i64 %20, %19
br i1 %21, label %11, label %22
22: ; preds = %11, %1
ret i8* %6
}