#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*/ ReverseStr(char *);
int StripLeadingZeros(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
int BoothDecode(char *booth, char *original, int pos) {
  int i;
  ReverseStr(booth);
  ReverseStr(original);
  printf("%s\n%s\n", booth, original);
  for (i = 0; booth[i]; i++) {
    if (booth[i] == 'P')
      booth[i] = '1';
    if (booth[i] == 'N') {
      booth[i++] = '1';
      while ((booth[i] == '0' || !booth[i]) && original[i + pos] == '0')
        booth[i++] = '1';
      booth[i] = '0';
    }
  }
  ReverseStr(booth);
  ReverseStr(original);
  return StripLeadingZeros(booth);
}


x86 assembly (clang, -O0):
<BoothDecode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <ReverseStr@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ReverseStr@plt>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe9c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     125e <BoothDecode+0x12e>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x50,%eax
jne    11a3 <BoothDecode+0x73>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x31,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x4e,%eax
jne    124b <BoothDecode+0x11b>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x31,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
je     11fa <BoothDecode+0xca>
mov    -0x8(%rbp),%rcx
movslq -0x18(%rbp),%rdx
xor    %eax,%eax
cmpb   $0x0,(%rcx,%rdx,1)
mov    %al,-0x19(%rbp)
jne    1214 <BoothDecode+0xe4>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1224 <BoothDecode+0xf4>
jmp    123f <BoothDecode+0x10f>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
movslq %ecx,%rcx
movb   $0x31,(%rax,%rcx,1)
jmp    11ce <BoothDecode+0x9e>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x30,(%rax,%rcx,1)
jmp    1250 <BoothDecode+0x120>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1170 <BoothDecode+0x40>
mov    -0x8(%rbp),%rdi
call   1030 <ReverseStr@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ReverseStr@plt>
mov    -0x8(%rbp),%rdi
call   1050 <StripLeadingZeros@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BoothDecode>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <ReverseStr@plt>
mov    %r14,%rdi
call   1030 <ReverseStr@plt>
lea    0xeb0(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <printf@plt>
movslq %ebp,%rbp
add    %r14,%rbp
xor    %ecx,%ecx
jmp    1177 <BoothDecode+0x47>
cs nopw 0x0(%rax,%rax,1)
nop
test   %al,%al
je     11d6 <BoothDecode+0xa6>
add    $0x1,%ecx
movslq %ecx,%rsi
lea    (%rbx,%rsi,1),%rdx
mov    (%rbx,%rsi,1),%al
cmp    $0x4e,%al
je     11a0 <BoothDecode+0x70>
cmp    $0x50,%al
jne    1170 <BoothDecode+0x40>
movb   $0x31,(%rdx)
add    $0x1,%ecx
jmp    1177 <BoothDecode+0x47>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %rbp,%rsi
mov    $0x1,%eax
jmp    11ba <BoothDecode+0x8a>
nopw   0x0(%rax,%rax,1)
lea    0x1(%rdi),%rax
cmpb   $0x30,(%rsi,%rdi,1)
jne    11ce <BoothDecode+0x9e>
mov    %rax,%rdi
movb   $0x31,-0x1(%rdx,%rax,1)
movzbl (%rdx,%rax,1),%eax
cmp    $0x30,%al
je     11b0 <BoothDecode+0x80>
test   %al,%al
je     11b0 <BoothDecode+0x80>
add    %edi,%ecx
movb   $0x30,(%rdx,%rdi,1)
jmp    1174 <BoothDecode+0x44>
mov    %rbx,%rdi
call   1030 <ReverseStr@plt>
mov    %r14,%rdi
call   1030 <ReverseStr@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <StripLeadingZeros@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%s\0A%s\0A\00"
define i32 @BoothDecode(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @ReverseStr(i8* %8)
%10 = load i8*, i8** %5
%11 = call i32 @ReverseStr(i8* %10)
%12 = load i8*, i8** %4
%13 = load i8*, i8** %5
%14 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %13)
store i32 0, i32* %7
br label %15
15: ; preds = %88, %3
%16 = load i8*, i8** %4
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %91
22: ; preds = %15
%23 = load i8*, i8** %4
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 80
br i1 %29, label %30, label %35
30: ; preds = %22
%31 = load i8*, i8** %4
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 49, i8* %34
br label %35
35: ; preds = %30, %22
%36 = load i8*, i8** %4
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 78
br i1 %42, label %43, label %87
43: ; preds = %35
%44 = load i8*, i8** %4
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
store i8 49, i8* %48
br label %49
49: ; preds = %76, %43
%50 = load i8*, i8** %4
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 48
br i1 %56, label %64, label %57
57: ; preds = %49
%58 = load i8*, i8** %4
%59 = load i32, i32* %7
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i8, i8* %61
%63 = icmp ne i8 %62, 0
br i1 %63, label %74, label %64
64: ; preds = %57, %49
%65 = load i8*, i8** %5
%66 = load i32, i32* %7
%67 = load i32, i32* %6
%68 = add nsw i32 %66, %67
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %65, i64 %69
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = icmp eq i32 %72, 48
br label %74
74: ; preds = %64, %57
%75 = phi i1 [ false, %57 ], [ %73, %64 ]
br i1 %75, label %76, label %82
76: ; preds = %74
%77 = load i8*, i8** %4
%78 = load i32, i32* %7
%79 = add nsw i32 %78, 1
store i32 %79, i32* %7
%80 = sext i32 %78 to i64
%81 = getelementptr inbounds i8, i8* %77, i64 %80
store i8 49, i8* %81
br label %49
82: ; preds = %74
%83 = load i8*, i8** %4
%84 = load i32, i32* %7
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %83, i64 %85
store i8 48, i8* %86
br label %87
87: ; preds = %82, %35
br label %88
88: ; preds = %87
%89 = load i32, i32* %7
%90 = add nsw i32 %89, 1
store i32 %90, i32* %7
br label %15
91: ; preds = %15
%92 = load i8*, i8** %4
%93 = call i32 @ReverseStr(i8* %92)
%94 = load i8*, i8** %5
%95 = call i32 @ReverseStr(i8* %94)
%96 = load i8*, i8** %4
%97 = call i32 @StripLeadingZeros(i8* %96)
ret i32 %97
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s\0A%s\0A\00"
define i32 @BoothDecode(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @ReverseStr(i8* %0)
%5 = tail call i32 @ReverseStr(i8* %1)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%7 = sext i32 %2 to i64
br label %8
8: ; preds = %27, %3
%9 = phi i32 [ 0, %3 ], [ %29, %27 ]
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
switch i8 %12, label %27 [
i8 0, label %30
i8 80, label %13
i8 78, label %14
]
13: ; preds = %8
store i8 49, i8* %11
br label %27
14: ; preds = %8, %20
%15 = phi i8* [ %18, %20 ], [ %11, %8 ]
%16 = phi i64 [ %17, %20 ], [ %10, %8 ]
store i8 49, i8* %15
%17 = add i64 %16, 1
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
switch i8 %19, label %25 [
i8 48, label %20
i8 0, label %20
]
20: ; preds = %14, %14
%21 = add nsw i64 %17, %7
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 48
br i1 %24, label %14, label %25
25: ; preds = %14, %20
%26 = trunc i64 %17 to i32
store i8 48, i8* %18
br label %27
27: ; preds = %8, %13, %25
%28 = phi i32 [ %26, %25 ], [ %9, %13 ], [ %9, %8 ]
%29 = add nsw i32 %28, 1
br label %8
30: ; preds = %8
%31 = tail call i32 @ReverseStr(i8* %0)
%32 = tail call i32 @ReverseStr(i8* %1)
%33 = tail call i32 @StripLeadingZeros(i8* %0)
ret i32 %33
}