#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*/ printbin(unsigned int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ println(int, char *, int, unsigned int);
void booth(unsigned int multiplier, int multiplicand) {
  unsigned int product = multiplier;
  product &= 0xFF;
  printf("\n___________________________________________________________________"
         "_________________________________________________\n");
  printf("| Iteration  |     Operation    | \t\tMultiplicand\t\t |    \t\t "
         "Product \t\t   |\n");
  printf("|____________|__________________|____________________________________"
         "____|_________________________________________|\n");
  product <<= 1;
  println(0, "Initialization", multiplicand, product);
  printf("|____________|__________________|____________________________________"
         "____|_________________________________________|\n");
  unsigned int add = (multiplicand << (8 + 1)) & 130560;
  unsigned int sub = (-multiplicand << (8 + 1)) & 130560;
  for (int i = 1; i <= 8; i++) {
    if ((product & 3) == 1) {
      product += add;
      println(i, "prod=prod+mcand", multiplicand, product);
    } else if ((product & 3) == 2) {
      product += sub;
      println(i, "prod=prod-mcand", multiplicand, product);
    } else
      println(i, "No Operation", multiplicand, product);
    product &= 0x1FFFF;
    if ((product & 65536) == 65536)
      product += 131072;
    product >>= 1;
    println(i, "Shift Right", multiplicand, product);
    printf("|____________|__________________|__________________________________"
           "______|_________________________________________|\n");
  }
  product = product >> 1;
  int val = product;
  printf("\n\nThe answer is: %d\n\nBinary: ", multiplicand * multiplier);
  printbin(product, 0, 32);
}


x86 assembly (clang, -O0):
<booth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0xc(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xf50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
xor    %edi,%edi
lea    0xfa7(%rip),%rsi
call   1040 <println@plt>
lea    0xf25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
shl    $0x9,%eax
and    $0x1fe00,%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
sub    -0x8(%rbp),%eax
shl    $0x9,%eax
and    $0x1fe00,%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x8,-0x18(%rbp)
jg     12b4 <booth+0x184>
mov    -0xc(%rbp),%eax
and    $0x3,%eax
cmp    $0x1,%eax
jne    1205 <booth+0xd5>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xf4c(%rip),%rsi
call   1040 <println@plt>
jmp    1251 <booth+0x121>
mov    -0xc(%rbp),%eax
and    $0x3,%eax
cmp    $0x2,%eax
jne    1237 <booth+0x107>
mov    -0x14(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xf2a(%rip),%rsi
call   1040 <println@plt>
jmp    124c <booth+0x11c>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xf20(%rip),%rsi
call   1040 <println@plt>
jmp    1251 <booth+0x121>
mov    -0xc(%rbp),%eax
and    $0x1ffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0x10000,%eax
cmp    $0x10000,%eax
jne    127a <booth+0x14a>
mov    -0xc(%rbp),%eax
add    $0x20000,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xee1(%rip),%rsi
call   1040 <println@plt>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c9 <booth+0x99>
mov    -0xc(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%esi
imul   -0x4(%rbp),%esi
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <printbin@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<booth>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %edi,%ebx
lea    0xf0f(%rip),%rdi
call   1030 <puts@plt>
lea    0xf79(%rip),%rdi
call   1030 <puts@plt>
lea    0xfb7(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,(%rsp)
movzbl %bl,%ebp
add    %ebp,%ebp
lea    0xe7a(%rip),%rsi
xor    %edi,%edi
mov    %r12d,%edx
mov    %ebp,%ecx
call   1050 <println@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r12d,%r15d
shl    $0x9,%r15d
mov    %r15d,%eax
and    $0x1fe00,%eax
mov    %eax,0x4(%rsp)
neg    %r15d
and    $0x1fe00,%r15d
mov    $0x1,%ebx
lea    0xe79(%rip),%r14
jmp    121c <booth+0xdc>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
lea    0xe56(%rip),%rsi
mov    %r12d,%edx
mov    %ebp,%ecx
call   1050 <println@plt>
mov    %ebp,%eax
and    $0x1fffe,%eax
and    $0x10000,%ebp
lea    (%rax,%rbp,2),%r13d
mov    %r13d,%ebp
shr    %ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12d,%edx
mov    %ebp,%ecx
call   1050 <println@plt>
lea    0xf17(%rip),%rdi
call   1030 <puts@plt>
add    $0x1,%ebx
cmp    $0x9,%ebx
je     124e <booth+0x10e>
mov    %ebp,%eax
and    $0x3,%eax
cmp    $0x2,%eax
je     1240 <booth+0x100>
cmp    $0x1,%eax
jne    11d0 <booth+0x90>
add    0x4(%rsp),%ebp
mov    %ebx,%edi
lea    0xdd7(%rip),%rsi
jmp    11d9 <booth+0x99>
nopw   0x0(%rax,%rax,1)
add    %r15d,%ebp
mov    %ebx,%edi
lea    0xdd3(%rip),%rsi
jmp    11d9 <booth+0x99>
shr    $0x2,%r13d
imul   (%rsp),%r12d
lea    0xdea(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,%edi
xor    %esi,%esi
mov    $0x20,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printbin@plt>

LLVM IR (-O0):
@.str = [119 x i8] c"\0A____________________________________________________________________________________________________________________\0A\00"
@.str.1 = [75 x i8] c"| Iteration | Operation | \09\09Multiplicand\09\09 | \09\09 Product \09\09 |\0A\00"
@.str.2 = [118 x i8] c"|____________|__________________|________________________________________|_________________________________________|\0A\00"
@.str.3 = [15 x i8] c"Initialization\00"
@.str.4 = [16 x i8] c"prod=prod+mcand\00"
@.str.5 = [16 x i8] c"prod=prod-mcand\00"
@.str.6 = [13 x i8] c"No Operation\00"
@.str.7 = [12 x i8] c"Shift Right\00"
@.str.8 = [30 x i8] c"\0A\0AThe answer is: %d\0A\0ABinary: \00"
define void @booth(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = load i32, i32* %3
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = and i32 %11, 255
store i32 %12, i32* %5
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([119 x i8], [119 x i8]* @.str, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([118 x i8], [118 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* %5
%17 = shl i32 %16, 1
store i32 %17, i32* %5
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = call i32 @println(i32 0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %18, i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([118 x i8], [118 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* %4
%23 = shl i32 %22, 9
%24 = and i32 %23, 130560
store i32 %24, i32* %6
%25 = load i32, i32* %4
%26 = sub nsw i32 0, %25
%27 = shl i32 %26, 9
%28 = and i32 %27, 130560
store i32 %28, i32* %7
store i32 1, i32* %8
br label %29
29: ; preds = %79, %2
%30 = load i32, i32* %8
%31 = icmp sle i32 %30, 8
br i1 %31, label %32, label %82
32: ; preds = %29
%33 = load i32, i32* %5
%34 = and i32 %33, 3
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %44
36: ; preds = %32
%37 = load i32, i32* %6
%38 = load i32, i32* %5
%39 = add i32 %38, %37
store i32 %39, i32* %5
%40 = load i32, i32* %8
%41 = load i32, i32* %4
%42 = load i32, i32* %5
%43 = call i32 @println(i32 %40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %41, i32 %42)
br label %62
44: ; preds = %32
%45 = load i32, i32* %5
%46 = and i32 %45, 3
%47 = icmp eq i32 %46, 2
br i1 %47, label %48, label %56
48: ; preds = %44
%49 = load i32, i32* %7
%50 = load i32, i32* %5
%51 = add i32 %50, %49
store i32 %51, i32* %5
%52 = load i32, i32* %8
%53 = load i32, i32* %4
%54 = load i32, i32* %5
%55 = call i32 @println(i32 %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %53, i32 %54)
br label %61
56: ; preds = %44
%57 = load i32, i32* %8
%58 = load i32, i32* %4
%59 = load i32, i32* %5
%60 = call i32 @println(i32 %57, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %58, i32 %59)
br label %61
61: ; preds = %56, %48
br label %62
62: ; preds = %61, %36
%63 = load i32, i32* %5
%64 = and i32 %63, 131071
store i32 %64, i32* %5
%65 = load i32, i32* %5
%66 = and i32 %65, 65536
%67 = icmp eq i32 %66, 65536
br i1 %67, label %68, label %71
68: ; preds = %62
%69 = load i32, i32* %5
%70 = add i32 %69, 131072
store i32 %70, i32* %5
br label %71
71: ; preds = %68, %62
%72 = load i32, i32* %5
%73 = lshr i32 %72, 1
store i32 %73, i32* %5
%74 = load i32, i32* %8
%75 = load i32, i32* %4
%76 = load i32, i32* %5
%77 = call i32 @println(i32 %74, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %75, i32 %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([118 x i8], [118 x i8]* @.str.2, i64 0, i64 0))
br label %79
79: ; preds = %71
%80 = load i32, i32* %8
%81 = add nsw i32 %80, 1
store i32 %81, i32* %8
br label %29
82: ; preds = %29
%83 = load i32, i32* %5
%84 = lshr i32 %83, 1
store i32 %84, i32* %5
%85 = load i32, i32* %5
store i32 %85, i32* %9
%86 = load i32, i32* %4
%87 = load i32, i32* %3
%88 = mul i32 %86, %87
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i32 %88)
%90 = load i32, i32* %5
%91 = call i32 @printbin(i32 %90, i32 0, i32 32)
ret void
}

LLVM IR (-O2):
@.str.3 = [15 x i8] c"Initialization\00"
@.str.4 = [16 x i8] c"prod=prod+mcand\00"
@.str.5 = [16 x i8] c"prod=prod-mcand\00"
@.str.6 = [13 x i8] c"No Operation\00"
@.str.7 = [12 x i8] c"Shift Right\00"
@.str.8 = [30 x i8] c"\0A\0AThe answer is: %d\0A\0ABinary: \00"
@str = [118 x i8] c"\0A____________________________________________________________________________________________________________________\00"
@str.9 = [74 x i8] c"| Iteration | Operation | \09\09Multiplicand\09\09 | \09\09 Product \09\09 |\00"
@str.12 = [117 x i8] c"|____________|__________________|________________________________________|_________________________________________|\00"
define void @booth(i32 %0, i32 %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([118 x i8], [118 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([74 x i8], [74 x i8]* @str.9, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([117 x i8], [117 x i8]* @str.12, i64 0, i64 0))
%6 = shl i32 %0, 1
%7 = and i32 %6, 510
%8 = tail call i32 @println(i32 0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %1, i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([117 x i8], [117 x i8]* @str.12, i64 0, i64 0))
%10 = shl i32 %1, 9
%11 = and i32 %10, 130560
%12 = mul i32 %1, -512
%13 = and i32 %12, 130560
br label %19
14: ; preds = %31
%15 = lshr i32 %36, 2
%16 = mul i32 %1, %0
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.8, i64 0, i64 0), i32 %16)
%18 = tail call i32 @printbin(i32 %15, i32 0, i32 32)
ret void
19: ; preds = %2, %31
%20 = phi i32 [ %7, %2 ], [ %37, %31 ]
%21 = phi i32 [ 1, %2 ], [ %40, %31 ]
%22 = and i32 %20, 3
switch i32 %22, label %29 [
i32 1, label %23
i32 2, label %26
]
23: ; preds = %19
%24 = add nuw nsw i32 %20, %11
%25 = tail call i32 @println(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %1, i32 %24)
br label %31
26: ; preds = %19
%27 = add nuw nsw i32 %20, %13
%28 = tail call i32 @println(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %1, i32 %27)
br label %31
29: ; preds = %19
%30 = tail call i32 @println(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i32 %1, i32 %20)
br label %31
31: ; preds = %26, %29, %23
%32 = phi i32 [ %24, %23 ], [ %27, %26 ], [ %20, %29 ]
%33 = and i32 %32, 131070
%34 = shl nsw i32 %32, 1
%35 = and i32 %34, 131072
%36 = or i32 %35, %33
%37 = lshr exact i32 %36, 1
%38 = tail call i32 @println(i32 %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %1, i32 %37)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([117 x i8], [117 x i8]* @str.12, i64 0, i64 0))
%40 = add nuw nsw i32 %21, 1
%41 = icmp eq i32 %40, 9
br i1 %41, label %14, label %19
}