#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 *malloc(int);
int strlen(char *);
char *reverse_string(char *s) {
  int n = strlen(s);
  char *t = malloc(n + 1);
  char *to = t;
  char *from = s + n - 1;
  while (1) {
    *to = *from;
    to++;
    from--;
    if (from < s) {
      break;
    }
  }
  return t;
}


x86 assembly (clang, -O0):
<reverse_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1199 <reverse_string+0x79>
jmp    119e <reverse_string+0x7e>
jmp    1162 <reverse_string+0x42>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_string>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1040 <malloc@plt>
movslq %r14d,%rcx
add    %r15,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rax,%rdx
cs nopw 0x0(%rax,%rax,1)
movzbl (%rcx),%ebx
mov    %bl,(%rdx)
add    $0x1,%rdx
add    $0xffffffffffffffff,%rcx
cmp    %r15,%rcx
jae    1150 <reverse_string+0x30>
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define noalias i8* @reverse_string(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = sext i32 %2 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = getelementptr inbounds i8, i8* %6, i64 -1
br label %8
8: ; preds = %8, %1
%9 = phi i8* [ %4, %1 ], [ %12, %8 ]
%10 = phi i8* [ %7, %1 ], [ %13, %8 ]
%11 = load i8, i8* %10
store i8 %11, i8* %9
%12 = getelementptr inbounds i8, i8* %9, i64 1
%13 = getelementptr inbounds i8, i8* %10, i64 -1
%14 = icmp ult i8* %13, %0
br i1 %14, label %15, label %8
15: ; preds = %8
ret i8* %4
}