#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*/ printf(char *, char *);
scalar_t__ strlen(char *);
int ddt_ternary(char *s) {
  int a, t, j, l;
  a = 0;
  t = 1;
  l = (int)strlen(s);
  for (j = l - 1; j >= 0; j--) {
    switch (s[j]) {
    case 'N':
    case 'n':
    case '2':
    case '-':
      a -= t;
      break;
    case 'O':
    case 'o':
    case '0':
      break;
    case 'P':
    case 'p':
    case '1':
    case '+':
      a += t;
      break;
    default:
      printf("ERROR: Wrong ternary string '%s'!\n", s);
    }
    t *= 3;
  }
  return a;
}


x86 assembly (clang, -O0):
<ddt_ternary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11d3 <ddt_ternary+0xb3>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
add    $0xffffffd5,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x45,%eax
ja     11ae <ddt_ternary+0x8e>
mov    -0x20(%rbp),%rax
lea    0xe7e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    11be <ddt_ternary+0x9e>
jmp    11be <ddt_ternary+0x9e>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11be <ddt_ternary+0x9e>
mov    -0x8(%rbp),%rsi
lea    0xf5f(%rip),%rdi
call   1040 <printf@plt>
imul   $0x3,-0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    114f <ddt_ternary+0x2f>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ddt_ternary>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
test   %eax,%eax
jle    119d <ddt_ternary+0x7d>
mov    %eax,%ebx
add    $0x1,%rbx
mov    $0x1,%ebp
xor    %r12d,%r12d
lea    0xfcc(%rip),%r14
lea    0xead(%rip),%r13
jmp    1171 <ddt_ternary+0x51>
cs nopw 0x0(%rax,%rax,1)
nop
add    %ebp,%r12d
lea    0x0(%rbp,%rbp,2),%ebp
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
jbe    11a0 <ddt_ternary+0x80>
lea    -0x2(%rbx),%eax
movsbl (%r15,%rax,1),%eax
add    $0xffffffd5,%eax
cmp    $0x45,%eax
ja     1190 <ddt_ternary+0x70>
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
sub    %ebp,%r12d
jmp    1163 <ddt_ternary+0x43>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
jmp    1163 <ddt_ternary+0x43>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"ERROR: Wrong ternary string '%s'!\0A\00"
define i32 @ddt_ternary(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 1, i32* %4
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
%9 = trunc i64 %8 to i32
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %5
br label %12
12: ; preds = %37, %1
%13 = load i32, i32* %5
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %40
15: ; preds = %12
%16 = load i8*, i8** %2
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
switch i32 %21, label %31 [
i32 78, label %22
i32 110, label %22
i32 50, label %22
i32 45, label %22
i32 79, label %26
i32 111, label %26
i32 48, label %26
i32 80, label %27
i32 112, label %27
i32 49, label %27
i32 43, label %27
]
22: ; preds = %15, %15, %15, %15
%23 = load i32, i32* %4
%24 = load i32, i32* %3
%25 = sub nsw i32 %24, %23
store i32 %25, i32* %3
br label %34
26: ; preds = %15, %15, %15
br label %34
27: ; preds = %15, %15, %15, %15
%28 = load i32, i32* %4
%29 = load i32, i32* %3
%30 = add nsw i32 %29, %28
store i32 %30, i32* %3
br label %34
31: ; preds = %15
%32 = load i8*, i8** %2
%33 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %27, %26, %22
%35 = load i32, i32* %4
%36 = mul nsw i32 %35, 3
store i32 %36, i32* %4
br label %37
37: ; preds = %34
%38 = load i32, i32* %5
%39 = add nsw i32 %38, -1
store i32 %39, i32* %5
br label %12
40: ; preds = %12
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [35 x i8] c"ERROR: Wrong ternary string '%s'!\0A\00"
define i32 @ddt_ternary(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = trunc i64 %2 to i32
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %26
5: ; preds = %1
%6 = and i64 %2, 4294967295
br label %7
7: ; preds = %5, %22
%8 = phi i64 [ %6, %5 ], [ %11, %22 ]
%9 = phi i32 [ 0, %5 ], [ %23, %22 ]
%10 = phi i32 [ 1, %5 ], [ %24, %22 ]
%11 = add nsw i64 %8, -1
%12 = and i64 %11, 4294967295
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
switch i32 %15, label %20 [
i32 78, label %16
i32 110, label %16
i32 50, label %16
i32 45, label %16
i32 79, label %22
i32 111, label %22
i32 48, label %22
i32 80, label %18
i32 112, label %18
i32 49, label %18
i32 43, label %18
]
16: ; preds = %7, %7, %7, %7
%17 = sub nsw i32 %9, %10
br label %22
18: ; preds = %7, %7, %7, %7
%19 = add nsw i32 %9, %10
br label %22
20: ; preds = %7
%21 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %7, %7, %7, %20, %18, %16
%23 = phi i32 [ %9, %20 ], [ %19, %18 ], [ %9, %7 ], [ %9, %7 ], [ %9, %7 ], [ %17, %16 ]
%24 = mul nsw i32 %10, 3
%25 = icmp ugt i64 %8, 1
br i1 %25, label %7, label %26
26: ; preds = %22, %1
%27 = phi i32 [ 0, %1 ], [ %23, %22 ]
ret i32 %27
}