#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 */
int atoi(char *);
int /*<<< orphan*/ is_prime(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char *);
int prime_backwards(char *prime) {
  int length = strlen(prime);
  char backwards[length + 1];
  memset(backwards, '0', length);
  backwards[length] = '\0';
  for (int i = length - 1; i >= 0; i--) {
    backwards[i] = prime[i];
    int prime_slice = atoi(backwards);
    if (!is_prime(prime_slice))
      return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<prime_backwards>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edx
mov    $0x30,%esi
call   1040 <memset@plt>
mov    -0x40(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1202 <prime_backwards+0xc2>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
movslq -0x2c(%rbp),%rax
mov    %cl,(%rdi,%rax,1)
call   1060 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <is_prime@plt>
cmp    $0x0,%eax
jne    11ef <prime_backwards+0xaf>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1210 <prime_backwards+0xd0>
jmp    11f4 <prime_backwards+0xb4>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a3 <prime_backwards+0x63>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prime_backwards>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rdi
mov    $0x30,%esi
mov    %r15d,%edx
call   1040 <memset@plt>
movslq %r15d,%rax
movb   $0x0,(%rax,%r12,1)
mov    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%r15d
test   %ebx,%ebx
jle    11b9 <prime_backwards+0x79>
movzbl -0x1(%r14,%rbx,1),%eax
mov    %al,-0x1(%r12,%rbx,1)
mov    %r12,%rdi
call   1060 <atoi@plt>
mov    %eax,%edi
call   1050 <is_prime@plt>
add    $0xffffffffffffffff,%rbx
test   %eax,%eax
jne    1190 <prime_backwards+0x50>
xor    %eax,%eax
test   %r15d,%r15d
setle  %al
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @prime_backwards(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = add nsw i32 %12, 1
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %5
%16 = alloca i8, i64 %14
store i64 %14, i64* %6
%17 = load i32, i32* %4
%18 = call i32 @memset(i8* %16, i32 48, i32 %17)
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %16, i64 %20
store i8 0, i8* %21
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %7
br label %24
24: ; preds = %42, %1
%25 = load i32, i32* %7
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %45
27: ; preds = %24
%28 = load i8*, i8** %3
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %16, i64 %34
store i8 %32, i8* %35
%36 = call i32 @atoi(i8* %16)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = call i32 @is_prime(i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %27
store i32 0, i32* %2
store i32 1, i32* %9
br label %46
41: ; preds = %27
br label %42
42: ; preds = %41
%43 = load i32, i32* %7
%44 = add nsw i32 %43, -1
store i32 %44, i32* %7
br label %24
45: ; preds = %24
store i32 1, i32* %2
store i32 1, i32* %9
br label %46
46: ; preds = %45, %40
%47 = load i8*, i8** %5
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
define i32 @prime_backwards(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @memset(i8* %5, i32 48, i32 %2)
%7 = sext i32 %2 to i64
%8 = getelementptr inbounds i8, i8* %5, i64 %7
store i8 0, i8* %8
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %15, %1
%11 = phi i64 [ %12, %15 ], [ %9, %1 ]
%12 = add nsw i64 %11, -1
%13 = trunc i64 %11 to i32
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %0, i64 %12
%17 = load i8, i8* %16
%18 = getelementptr inbounds i8, i8* %5, i64 %12
store i8 %17, i8* %18
%19 = call i32 @atoi(i8* %5)
%20 = call i32 @is_prime(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %10
22: ; preds = %10, %15
%23 = xor i1 %14, true
%24 = zext i1 %23 to i32
ret i32 %24
}