#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 isPowOfTwo(long);
int mult(long, long);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, ...);
void main() {
  int t, k = 0;
  long int a, b;
  scanf("%d", &t);
  while (t-- != 0) {
    scanf("%ld", &a);
    scanf("%ld", &b);
    if (isPowOfTwo(a) == 1)
      k = mult(a, b);
    else {
      while (isPowOfTwo(a) == 0) {
        if (a % 2 == 1)
          a = (a - 1) / 2;
        else
          a = a / 2;
        k++;
      }
      k = k + mult(a, b);
    }
    printf("%d\n", k);
    k = 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
lea    0xeaa(%rip),%rdi
lea    -0x4(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
je     1262 <main+0x122>
lea    0xe87(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
lea    0xe75(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <isPowOfTwo@plt>
cmp    $0x1,%eax
jne    11c0 <main+0x80>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <mult@plt>
mov    %eax,-0x8(%rbp)
jmp    1247 <main+0x107>
jmp    11c5 <main+0x85>
mov    -0x10(%rbp),%rdi
call   1060 <isPowOfTwo@plt>
cmp    $0x0,%eax
jne    122a <main+0xea>
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
cmp    $0x1,%rdx
jne    120a <main+0xca>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
jmp    121c <main+0xdc>
mov    -0x10(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11c5 <main+0x85>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <mult@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xdb6(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
jmp    1161 <main+0x21>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xead(%rip),%rdi
lea    0x14(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
subl   $0x1,0x14(%rsp)
jae    1173 <main+0x33>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe89(%rip),%rbx
lea    0x8(%rsp),%r14
lea    0x18(%rsp),%r15
lea    0xe7c(%rip),%r12
jmp    11ab <main+0x6b>
nopl   (%rax)
mov    0x18(%rsp),%rsi
call   1040 <mult@plt>
mov    %eax,%esi
mov    %r12,%rdi
call   1030 <printf@plt>
subl   $0x1,0x14(%rsp)
jb     1166 <main+0x26>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x8(%rsp),%rdi
call   1060 <isPowOfTwo@plt>
mov    0x8(%rsp),%rdi
cmp    $0x1,%eax
je     1190 <main+0x50>
call   1060 <isPowOfTwo@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    1233 <main+0xf3>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
add    %rax,%rcx
and    $0xfffffffffffffffe,%rcx
mov    %rax,%rdx
sub    %rcx,%rdx
xor    %ecx,%ecx
cmp    $0x1,%rdx
sete   %cl
sub    %rcx,%rax
mov    %rax,%rdi
shr    $0x3f,%rdi
add    %rax,%rdi
sar    %rdi
mov    %rdi,0x8(%rsp)
add    $0x1,%ebp
call   1060 <isPowOfTwo@plt>
test   %eax,%eax
je     11f0 <main+0xb0>
mov    0x8(%rsp),%rdi
mov    0x18(%rsp),%rsi
call   1040 <mult@plt>
mov    %eax,%esi
add    %ebp,%esi
jmp    119c <main+0x5c>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
store i32 0, i32* %2
%5 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
br label %6
6: ; preds = %45, %0
%7 = load i32, i32* %1
%8 = add nsw i32 %7, -1
store i32 %8, i32* %1
%9 = icmp ne i32 %7, 0
br i1 %9, label %10, label %48
10: ; preds = %6
%11 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %3)
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %4)
%13 = load i64, i64* %3
%14 = call i32 @isPowOfTwo(i64 %13)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i64, i64* %3
%18 = load i64, i64* %4
%19 = call i32 @mult(i64 %17, i64 %18)
store i32 %19, i32* %2
br label %45
20: ; preds = %10
br label %21
21: ; preds = %36, %20
%22 = load i64, i64* %3
%23 = call i32 @isPowOfTwo(i64 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %21
%26 = load i64, i64* %3
%27 = srem i64 %26, 2
%28 = icmp eq i64 %27, 1
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i64, i64* %3
%31 = sub nsw i64 %30, 1
%32 = sdiv i64 %31, 2
store i64 %32, i64* %3
br label %36
33: ; preds = %25
%34 = load i64, i64* %3
%35 = sdiv i64 %34, 2
store i64 %35, i64* %3
br label %36
36: ; preds = %33, %29
%37 = load i32, i32* %2
%38 = add nsw i32 %37, 1
store i32 %38, i32* %2
br label %21
39: ; preds = %21
%40 = load i32, i32* %2
%41 = load i64, i64* %3
%42 = load i64, i64* %4
%43 = call i32 @mult(i64 %41, i64 %42)
%44 = add nsw i32 %40, %43
store i32 %44, i32* %2
br label %45
45: ; preds = %39, %16
%46 = load i32, i32* %2
%47 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %46)
store i32 0, i32* %2
br label %6
48: ; preds = %6
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i32* %1 to i8*
%5 = bitcast i64* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = add nsw i32 %8, -1
store i32 %9, i32* %1
%10 = icmp eq i32 %8, 0
br i1 %10, label %47, label %11
11: ; preds = %0, %41
%12 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %2)
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %3)
%14 = load i64, i64* %2
%15 = call i32 @isPowOfTwo(i64 %14)
%16 = icmp eq i32 %15, 1
%17 = load i64, i64* %2
br i1 %16, label %21, label %18
18: ; preds = %11
%19 = call i32 @isPowOfTwo(i64 %17)
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %35
21: ; preds = %11
%22 = load i64, i64* %3
%23 = call i32 @mult(i64 %17, i64 %22)
br label %41
24: ; preds = %18, %24
%25 = phi i32 [ %32, %24 ], [ 0, %18 ]
%26 = load i64, i64* %2
%27 = srem i64 %26, 2
%28 = icmp eq i64 %27, 1
%29 = sext i1 %28 to i64
%30 = add nsw i64 %26, %29
%31 = sdiv i64 %30, 2
store i64 %31, i64* %2
%32 = add nuw nsw i32 %25, 1
%33 = call i32 @isPowOfTwo(i64 %31)
%34 = icmp eq i32 %33, 0
br i1 %34, label %24, label %35
35: ; preds = %24, %18
%36 = phi i32 [ 0, %18 ], [ %32, %24 ]
%37 = load i64, i64* %2
%38 = load i64, i64* %3
%39 = call i32 @mult(i64 %37, i64 %38)
%40 = add nsw i32 %39, %36
br label %41
41: ; preds = %35, %21
%42 = phi i32 [ %23, %21 ], [ %40, %35 ]
%43 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %1
%45 = add nsw i32 %44, -1
store i32 %45, i32* %1
%46 = icmp eq i32 %44, 0
br i1 %46, label %47, label %11
47: ; preds = %41, %0
ret void
}