int Cartridge_CalcCartSizeInBlocks(int NoOfBlocks) {
  int i;
  int Temp = NoOfBlocks;
  int BitCount = 32;
  if (Temp == 0)
    return 0;
  for (i = 32; i > 0; i++) {
    if ((Temp & 0x080000000) != 0) {
      break;
    }
    BitCount--;
    Temp = Temp << 1;
  }
  NoOfBlocks = 0x0ffffffff & (~(0x0ffffffff << BitCount));
  return NoOfBlocks + 1;
}


x86 assembly (clang, -O0):
<Cartridge_CalcCartSizeInBlocks>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x20,-0x14(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    112a <Cartridge_CalcCartSizeInBlocks+0x2a>
movl   $0x0,-0x4(%rbp)
jmp    118f <Cartridge_CalcCartSizeInBlocks+0x8f>
movl   $0x20,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1171 <Cartridge_CalcCartSizeInBlocks+0x71>
mov    -0x10(%rbp),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     1151 <Cartridge_CalcCartSizeInBlocks+0x51>
jmp    1171 <Cartridge_CalcCartSizeInBlocks+0x71>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1131 <Cartridge_CalcCartSizeInBlocks+0x31>
mov    -0x14(%rbp),%ecx
mov    $0xffffffff,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
and    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<Cartridge_CalcCartSizeInBlocks>:
test   %edi,%edi
je     111f <Cartridge_CalcCartSizeInBlocks+0x1f>
mov    $0x20,%ecx
js     1117 <Cartridge_CalcCartSizeInBlocks+0x17>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ecx
add    %edi,%edi
jns    1110 <Cartridge_CalcCartSizeInBlocks+0x10>
mov    $0x1,%eax
shl    %cl,%eax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @Cartridge_CalcCartSizeInBlocks(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
store i32 %7, i32* %5
store i32 32, i32* %6
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %35
11: ; preds = %1
store i32 32, i32* %4
br label %12
12: ; preds = %25, %11
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %12
%16 = load i32, i32* %5
%17 = and i32 %16, -2147483648
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
br label %28
20: ; preds = %15
%21 = load i32, i32* %6
%22 = add nsw i32 %21, -1
store i32 %22, i32* %6
%23 = load i32, i32* %5
%24 = shl i32 %23, 1
store i32 %24, i32* %5
br label %25
25: ; preds = %20
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %12
28: ; preds = %19, %12
%29 = load i32, i32* %6
%30 = shl i32 -1, %29
%31 = xor i32 %30, -1
%32 = and i32 -1, %31
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %2
br label %35
35: ; preds = %28, %10
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
define i32 @Cartridge_CalcCartSizeInBlocks(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %14, label %3
3: ; preds = %1
%4 = icmp sgt i32 %0, -1
br i1 %4, label %5, label %11
5: ; preds = %3, %5
%6 = phi i32 [ %8, %5 ], [ 32, %3 ]
%7 = phi i32 [ %9, %5 ], [ %0, %3 ]
%8 = add nsw i32 %6, -1
%9 = shl nuw i32 %7, 1
%10 = icmp sgt i32 %9, -1
br i1 %10, label %5, label %11
11: ; preds = %5, %3
%12 = phi i32 [ 32, %3 ], [ %8, %5 ]
%13 = shl nuw i32 1, %12
br label %14
14: ; preds = %1, %11
%15 = phi i32 [ %13, %11 ], [ 0, %1 ]
ret i32 %15
}