#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ floor(int /*<<< orphan*/);
int /*<<< orphan*/ is_prime(int);
int /*<<< orphan*/ log10(int);
int /*<<< orphan*/ printf(char *, int, int, int, int);
int is_both_ways_truncatable(int i) {
  int left, right;
  int iter = 1, mask = 1;
  int len;
  if (!is_prime(i))
    return 0;
  len = (int)floor(log10(i)) + 1;
  while (iter < len) {
    mask *= 10;
    left = i % mask;
    right = i / mask;
    if (right == 0 || left == 0) {
      printf("Serious bug in logic: i: %d, mask: %d, left: %d, right :%d \n", i,
             mask, left, right);
      exit(0);
    }
    if (!is_prime(left) || !is_prime(right))
      return 0;
    iter++;
  }
  return 1;
}


x86 assembly (clang, -O0):
<is_both_ways_truncatable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <is_prime@plt>
cmp    $0x0,%eax
jne    1186 <is_both_ways_truncatable+0x36>
movl   $0x0,-0x4(%rbp)
jmp    1239 <is_both_ways_truncatable+0xe9>
mov    -0x8(%rbp),%edi
call   1050 <log10@plt>
mov    %eax,%edi
call   1070 <floor@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1232 <is_both_ways_truncatable+0xe2>
imul   $0xa,-0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11d6 <is_both_ways_truncatable+0x86>
cmpl   $0x0,-0xc(%rbp)
jne    11f6 <is_both_ways_truncatable+0xa6>
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
lea    0xe16(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <is_prime@plt>
cmp    $0x0,%eax
je     1218 <is_both_ways_truncatable+0xc8>
mov    -0x10(%rbp),%edi
call   1040 <is_prime@plt>
cmp    $0x0,%eax
jne    1224 <is_both_ways_truncatable+0xd4>
movl   $0x0,-0x4(%rbp)
jmp    1239 <is_both_ways_truncatable+0xe9>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119b <is_both_ways_truncatable+0x4b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_both_ways_truncatable>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r15d
call   1040 <is_prime@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     11c9 <is_both_ways_truncatable+0x79>
mov    %r15d,%edi
call   1050 <log10@plt>
mov    %eax,%edi
call   1070 <floor@plt>
mov    %rax,%r12
test   %r12d,%r12d
jle    11c3 <is_both_ways_truncatable+0x73>
mov    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %ebx,%ebx
lea    (%rbx,%rbx,4),%ebx
mov    %r15d,%eax
cltd
idiv   %ebx
mov    %edx,%ecx
mov    %eax,%ebp
test   %edx,%edx
je     11d5 <is_both_ways_truncatable+0x85>
test   %ebp,%ebp
je     11d5 <is_both_ways_truncatable+0x85>
mov    %ecx,%edi
call   1040 <is_prime@plt>
test   %eax,%eax
je     11c9 <is_both_ways_truncatable+0x79>
mov    %ebp,%edi
call   1040 <is_prime@plt>
test   %eax,%eax
je     11c9 <is_both_ways_truncatable+0x79>
add    $0xffffffff,%r12d
jne    1190 <is_both_ways_truncatable+0x40>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe24(%rip),%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %ebp,%r8d
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [61 x i8] c"Serious bug in logic: i: %d, mask: %d, left: %d, right :%d \0A\00"
define i32 @is_both_ways_truncatable(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %6
store i32 1, i32* %7
%9 = load i32, i32* %3
%10 = call i32 @is_prime(i32 %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %1
store i32 0, i32* %2
br label %57
13: ; preds = %1
%14 = load i32, i32* %3
%15 = call i32 @log10(i32 %14)
%16 = call i64 @floor(i32 %15)
%17 = trunc i64 %16 to i32
%18 = add nsw i32 %17, 1
store i32 %18, i32* %8
br label %19
19: ; preds = %53, %13
%20 = load i32, i32* %6
%21 = load i32, i32* %8
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %56
23: ; preds = %19
%24 = load i32, i32* %7
%25 = mul nsw i32 %24, 10
store i32 %25, i32* %7
%26 = load i32, i32* %3
%27 = load i32, i32* %7
%28 = srem i32 %26, %27
store i32 %28, i32* %4
%29 = load i32, i32* %3
%30 = load i32, i32* %7
%31 = sdiv i32 %29, %30
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %23
%35 = load i32, i32* %4
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %34, %23
%38 = load i32, i32* %3
%39 = load i32, i32* %7
%40 = load i32, i32* %4
%41 = load i32, i32* %5
%42 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %38, i32 %39, i32 %40, i32 %41)
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %34
%45 = load i32, i32* %4
%46 = call i32 @is_prime(i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i32, i32* %5
%50 = call i32 @is_prime(i32 %49)
%51 = icmp ne i32 %50, 0
br i1 %51, label %53, label %52
52: ; preds = %48, %44
store i32 0, i32* %2
br label %57
53: ; preds = %48
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
br label %19
56: ; preds = %19
store i32 1, i32* %2
br label %57
57: ; preds = %56, %52, %12
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@.str = [61 x i8] c"Serious bug in logic: i: %d, mask: %d, left: %d, right :%d \0A\00"
define i32 @is_both_ways_truncatable(i32 %0) {
%2 = tail call i32 @is_prime(i32 %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %30, label %4
4: ; preds = %1
%5 = tail call i32 @log10(i32 %0)
%6 = tail call i64 @floor(i32 %5)
%7 = trunc i64 %6 to i32
%8 = icmp slt i32 %7, 1
br i1 %8, label %30, label %12
9: ; preds = %27
%10 = add nuw i32 %14, 1
%11 = icmp eq i32 %14, %7
br i1 %11, label %30, label %12
12: ; preds = %4, %9
%13 = phi i32 [ %15, %9 ], [ 1, %4 ]
%14 = phi i32 [ %10, %9 ], [ 1, %4 ]
%15 = mul nuw nsw i32 %13, 10
%16 = srem i32 %0, %15
%17 = sdiv i32 %0, %15
%18 = icmp eq i32 %17, 0
%19 = icmp eq i32 %16, 0
%20 = or i1 %19, %18
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = tail call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %15, i32 %16, i32 %17)
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %12
%25 = tail call i32 @is_prime(i32 %16)
%26 = icmp eq i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = tail call i32 @is_prime(i32 %17)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %9
30: ; preds = %27, %24, %9, %4, %1
%31 = phi i32 [ 0, %1 ], [ 1, %4 ], [ 0, %27 ], [ 0, %24 ], [ 1, %9 ]
ret i32 %31
}