#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 lookup(char);
char *malloc(int);
int /*<<< orphan*/ reverse(char *);
int strlen(char *);
char *mirror(char *str) {
  int len;
  char *str2;
  len = strlen(str);
  str2 = malloc(sizeof(char) * len + 1);
  for (int i = 0; i < len; i++)
    str2[i] = lookup(str[i]);
  str2[len] = '\0';
  reverse(str2);
  return str2;
}


x86 assembly (clang, -O0):
<mirror>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ae <mirror+0x6e>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <lookup@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1176 <mirror+0x36>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1030 <reverse@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mirror>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%r14d
lea    0x1(%r14),%edi
call   1060 <malloc@plt>
mov    %rax,%r12
test   %r14d,%r14d
jle    1187 <mirror+0x47>
mov    %r14d,%r13d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1050 <lookup@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1170 <mirror+0x30>
movslq %r14d,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rdi
call   1030 <reverse@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @mirror(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)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = mul i64 1, %9
%11 = add i64 %10, 1
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %4
store i32 0, i32* %5
br label %14
14: ; preds = %29, %1
%15 = load i32, i32* %5
%16 = load i32, i32* %3
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %32
18: ; preds = %14
%19 = load i8*, i8** %2
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = call signext i8 @lookup(i8 signext %23)
%25 = load i8*, i8** %4
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 %24, i8* %28
br label %29
29: ; preds = %18
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %14
32: ; preds = %14
%33 = load i8*, i8** %4
%34 = load i32, i32* %3
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %4
%38 = call i32 @reverse(i8* %37)
%39 = load i8*, i8** %4
ret i8* %39
}

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