#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 */
typedef scalar_t__ m;

/* Variables and functions */
int /*<<< orphan*/ print_binary(short);
int /*<<< orphan*/ printf(char *, ...);
signed short int booth(signed char multiplier, signed char multiplicand) {
  signed short int result = 0, m = multiplier, n = multiplicand;
  m *= 2;
  printf("Multiplier:%sMultiplicand:%sResult:\n", "\t\t", "\t\t");
  while (m != 0) {
    if ((((m) & ((signed short int)1 << (0))) && 1) &&
        !(((m) & ((signed short int)1 << (1))) && 1))
      result += n;
    else if (!(((m) & ((signed short int)1 << (0))) && 1) &&
             (((m) & ((signed short int)1 << (1))) && 1))
      result -= n;
    print_binary(m);
    printf("\t");
    print_binary(n);
    printf("\t");
    print_binary(result);
    printf("\n");
    m /= 2;
    n *= 2;
  }
  return result;
}


x86 assembly (clang, -O0):
<booth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x1(%rbp)
mov    %al,-0x2(%rbp)
movw   $0x0,-0x4(%rbp)
movsbl -0x1(%rbp),%eax
mov    %ax,-0x6(%rbp)
movsbl -0x2(%rbp),%eax
mov    %ax,-0x8(%rbp)
movswl -0x6(%rbp),%eax
shl    $0x1,%eax
mov    %ax,-0x6(%rbp)
lea    0xea4(%rip),%rdi
lea    0xec2(%rip),%rdx
mov    %rdx,%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movswl -0x6(%rbp),%eax
cmp    $0x0,%eax
je     1245 <booth+0x125>
movswl -0x6(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11ad <booth+0x8d>
movswl -0x6(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
jne    11ad <booth+0x8d>
movswl -0x8(%rbp),%ecx
movswl -0x4(%rbp),%eax
add    %ecx,%eax
mov    %ax,-0x4(%rbp)
jmp    11e0 <booth+0xc0>
movswl -0x6(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11db <booth+0xbb>
movswl -0x6(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11db <booth+0xbb>
movswl -0x8(%rbp),%ecx
movswl -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %ax,-0x4(%rbp)
jmp    11e0 <booth+0xc0>
movswl -0x6(%rbp),%edi
call   1040 <print_binary@plt>
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movswl -0x8(%rbp),%edi
call   1040 <print_binary@plt>
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movswl -0x4(%rbp),%edi
call   1040 <print_binary@plt>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movswl -0x6(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %ax,-0x6(%rbp)
movswl -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %ax,-0x8(%rbp)
jmp    116d <booth+0x4d>
movswl -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<booth>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xebd(%rip),%rdi
lea    0xedb(%rip),%rdx
xor    %r15d,%r15d
mov    %rdx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %bpl,%bpl
je     11dc <booth+0xac>
movsbl %bpl,%eax
add    %eax,%eax
movsbl %r14b,%ebp
nopw   0x0(%rax,%rax,1)
movzwl %ax,%ebx
mov    %ebx,%eax
and    $0x3,%eax
cmp    $0x2,%ax
mov    $0x0,%ecx
cmove  %ebp,%ecx
neg    %ecx
cmp    $0x1,%ax
cmove  %ebp,%ecx
add    %ecx,%r15d
movswl %bx,%edi
call   1050 <print_binary@plt>
mov    $0x9,%edi
call   1030 <putchar@plt>
movswl %bp,%edi
call   1050 <print_binary@plt>
mov    $0x9,%edi
call   1030 <putchar@plt>
movswl %r15w,%edi
call   1050 <print_binary@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebx,%eax
shr    $0xf,%eax
add    %ebx,%eax
cwtl
shr    %eax
add    %ebp,%ebp
add    $0x1,%ebx
cmp    $0x2,%bx
ja     1170 <booth+0x40>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Multiplier:%sMultiplicand:%sResult:\0A\00"
@.str.1 = [3 x i8] c"\09\09\00"
@.str.2 = [2 x i8] c"\09\00"
@.str.3 = [2 x i8] c"\0A\00"
define signext i16 @booth(i8 signext %0, i8 signext %1) {
%3 = alloca i8
%4 = alloca i8
%5 = alloca i16
%6 = alloca i16
%7 = alloca i16
store i8 %0, i8* %3
store i8 %1, i8* %4
store i16 0, i16* %5
%8 = load i8, i8* %3
%9 = sext i8 %8 to i16
store i16 %9, i16* %6
%10 = load i8, i8* %4
%11 = sext i8 %10 to i16
store i16 %11, i16* %7
%12 = load i16, i16* %6
%13 = sext i16 %12 to i32
%14 = mul nsw i32 %13, 2
%15 = trunc i32 %14 to i16
store i16 %15, i16* %6
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %56, %2
%18 = load i16, i16* %6
%19 = sext i16 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %74
21: ; preds = %17
%22 = load i16, i16* %6
%23 = sext i16 %22 to i32
%24 = and i32 %23, 1
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %38
26: ; preds = %21
%27 = load i16, i16* %6
%28 = sext i16 %27 to i32
%29 = and i32 %28, 2
%30 = icmp ne i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %26
%32 = load i16, i16* %7
%33 = sext i16 %32 to i32
%34 = load i16, i16* %5
%35 = sext i16 %34 to i32
%36 = add nsw i32 %35, %33
%37 = trunc i32 %36 to i16
store i16 %37, i16* %5
br label %56
38: ; preds = %26, %21
%39 = load i16, i16* %6
%40 = sext i16 %39 to i32
%41 = and i32 %40, 1
%42 = icmp ne i32 %41, 0
br i1 %42, label %55, label %43
43: ; preds = %38
%44 = load i16, i16* %6
%45 = sext i16 %44 to i32
%46 = and i32 %45, 2
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load i16, i16* %7
%50 = sext i16 %49 to i32
%51 = load i16, i16* %5
%52 = sext i16 %51 to i32
%53 = sub nsw i32 %52, %50
%54 = trunc i32 %53 to i16
store i16 %54, i16* %5
br label %55
55: ; preds = %48, %43, %38
br label %56
56: ; preds = %55, %31
%57 = load i16, i16* %6
%58 = call i32 @print_binary(i16 signext %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%60 = load i16, i16* %7
%61 = call i32 @print_binary(i16 signext %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%63 = load i16, i16* %5
%64 = call i32 @print_binary(i16 signext %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%66 = load i16, i16* %6
%67 = sext i16 %66 to i32
%68 = sdiv i32 %67, 2
%69 = trunc i32 %68 to i16
store i16 %69, i16* %6
%70 = load i16, i16* %7
%71 = sext i16 %70 to i32
%72 = mul nsw i32 %71, 2
%73 = trunc i32 %72 to i16
store i16 %73, i16* %7
br label %17
74: ; preds = %17
%75 = load i16, i16* %5
ret i16 %75
}

LLVM IR (-O2):
@.str = [37 x i8] c"Multiplier:%sMultiplicand:%sResult:\0A\00"
@.str.1 = [3 x i8] c"\09\09\00"
define signext i16 @booth(i8 signext %0, i8 signext %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i8 %0, 0
br i1 %4, label %30, label %5
5: ; preds = %2
%6 = sext i8 %0 to i16
%7 = shl nsw i16 %6, 1
%8 = sext i8 %1 to i16
br label %9
9: ; preds = %5, %9
%10 = phi i16 [ %27, %9 ], [ %8, %5 ]
%11 = phi i16 [ %26, %9 ], [ %7, %5 ]
%12 = phi i16 [ %19, %9 ], [ 0, %5 ]
%13 = and i16 %11, 3
%14 = icmp eq i16 %13, 1
%15 = icmp eq i16 %13, 2
%16 = select i1 %15, i16 %10, i16 0
%17 = sub i16 0, %16
%18 = select i1 %14, i16 %10, i16 %17
%19 = add i16 %18, %12
%20 = tail call i32 @print_binary(i16 signext %11)
%21 = tail call i32 @putchar(i32 9)
%22 = tail call i32 @print_binary(i16 signext %10)
%23 = tail call i32 @putchar(i32 9)
%24 = tail call i32 @print_binary(i16 signext %19)
%25 = tail call i32 @putchar(i32 10)
%26 = sdiv i16 %11, 2
%27 = shl i16 %10, 1
%28 = add nsw i16 %11, 1
%29 = icmp ult i16 %28, 3
br i1 %29, label %30, label %9
30: ; preds = %9, %2
%31 = phi i16 [ 0, %2 ], [ %19, %9 ]
ret i16 %31
}