#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 power(int, int);
int count_twos(int num) {
  int result = 0;
  int pos = 0;
  int temp = num;
  while (temp > 0) {
    int digit = temp % 10;
    int left = num / power(10, pos + 1);
    int right = num % power(10, pos);
    if (digit < 2) {
      result += left * power(10, pos);
    } else if (digit > 2) {
      result += (left + 1) * power(10, pos);
    } else {
      result += left * power(10, pos) + right + 1;
    }
    temp /= 10;
    pos++;
  }
  return result;
}


x86 assembly (clang, -O0):
<count_twos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1231 <count_twos+0x121>
mov    -0x10(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%esi
add    $0x1,%esi
mov    $0xa,%edi
call   1030 <power@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%esi
mov    $0xa,%edi
call   1030 <power@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
cmpl   $0x2,-0x14(%rbp)
jge    11b6 <count_twos+0xa6>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%esi
mov    $0xa,%edi
call   1030 <power@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
imul   %ecx,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1215 <count_twos+0x105>
cmpl   $0x2,-0x14(%rbp)
jle    11e9 <count_twos+0xd9>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%esi
mov    $0xa,%edi
call   1030 <power@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
imul   %ecx,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1210 <count_twos+0x100>
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%esi
mov    $0xa,%edi
call   1030 <power@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
imul   %ecx,%eax
add    -0x1c(%rbp),%eax
add    $0x1,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1215 <count_twos+0x105>
mov    -0x10(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112f <count_twos+0x1f>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_twos>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %ebx,%ebx
test   %edi,%edi
jle    1207 <count_twos+0xf7>
mov    $0x1,%ebp
mov    %edi,0x14(%rsp)
mov    %edi,%r14d
jmp    1164 <count_twos+0x54>
cs nopw 0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %r15d,%esi
call   1030 <power@plt>
imul   0x10(%rsp),%eax
mov    0xc(%rsp),%ecx
add    %eax,%ebx
add    $0x1,%ebp
cmp    $0x9,%ecx
jbe    1207 <count_twos+0xf7>
mov    %r14d,%r13d
lea    -0x1(%rbp),%r15d
mov    %r14d,%r14d
mov    $0xcccccccd,%eax
imul   %rax,%r14
shr    $0x23,%r14
lea    (%r14,%r14,1),%eax
lea    (%rax,%rax,4),%eax
mov    %r13d,0xc(%rsp)
sub    %eax,%r13d
mov    $0xa,%edi
mov    %ebp,%esi
call   1030 <power@plt>
mov    %eax,%ecx
mov    0x14(%rsp),%r12d
mov    %r12d,%eax
cltd
idiv   %ecx
mov    %eax,0x10(%rsp)
mov    $0xa,%edi
mov    %r15d,%esi
call   1030 <power@plt>
mov    %eax,%ecx
mov    %r12d,%eax
cltd
idiv   %ecx
cmp    $0x1,%r13d
jbe    1140 <count_twos+0x30>
mov    %r15d,%esi
mov    0x10(%rsp),%r15d
cmp    $0x2,%r13d
jne    11f0 <count_twos+0xe0>
mov    %edx,%r12d
mov    $0xa,%edi
call   1030 <power@plt>
imul   %r15d,%eax
add    %r12d,%eax
add    $0x1,%eax
jmp    1152 <count_twos+0x42>
add    $0x1,%r15d
mov    $0xa,%edi
call   1030 <power@plt>
imul   %r15d,%eax
jmp    1152 <count_twos+0x42>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @count_twos(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%9 = load i32, i32* %2
store i32 %9, i32* %5
br label %10
10: ; preds = %56, %1
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %61
13: ; preds = %10
%14 = load i32, i32* %5
%15 = srem i32 %14, 10
store i32 %15, i32* %6
%16 = load i32, i32* %2
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 1
%19 = call i32 @power(i32 10, i32 %18)
%20 = sdiv i32 %16, %19
store i32 %20, i32* %7
%21 = load i32, i32* %2
%22 = load i32, i32* %4
%23 = call i32 @power(i32 10, i32 %22)
%24 = srem i32 %21, %23
store i32 %24, i32* %8
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 2
br i1 %26, label %27, label %34
27: ; preds = %13
%28 = load i32, i32* %7
%29 = load i32, i32* %4
%30 = call i32 @power(i32 10, i32 %29)
%31 = mul nsw i32 %28, %30
%32 = load i32, i32* %3
%33 = add nsw i32 %32, %31
store i32 %33, i32* %3
br label %56
34: ; preds = %13
%35 = load i32, i32* %6
%36 = icmp sgt i32 %35, 2
br i1 %36, label %37, label %45
37: ; preds = %34
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
%40 = load i32, i32* %4
%41 = call i32 @power(i32 10, i32 %40)
%42 = mul nsw i32 %39, %41
%43 = load i32, i32* %3
%44 = add nsw i32 %43, %42
store i32 %44, i32* %3
br label %55
45: ; preds = %34
%46 = load i32, i32* %7
%47 = load i32, i32* %4
%48 = call i32 @power(i32 10, i32 %47)
%49 = mul nsw i32 %46, %48
%50 = load i32, i32* %8
%51 = add nsw i32 %49, %50
%52 = add nsw i32 %51, 1
%53 = load i32, i32* %3
%54 = add nsw i32 %53, %52
store i32 %54, i32* %3
br label %55
55: ; preds = %45, %37
br label %56
56: ; preds = %55, %27
%57 = load i32, i32* %5
%58 = sdiv i32 %57, 10
store i32 %58, i32* %5
%59 = load i32, i32* %4
%60 = add nsw i32 %59, 1
store i32 %60, i32* %4
br label %10
61: ; preds = %10
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
define i32 @count_twos(i32 %0) {
%2 = icmp sgt i32 %0, 0
br i1 %2, label %3, label %33
3: ; preds = %1, %29
%4 = phi i32 [ %31, %29 ], [ 0, %1 ]
%5 = phi i32 [ %9, %29 ], [ 0, %1 ]
%6 = phi i32 [ %8, %29 ], [ %0, %1 ]
%7 = urem i32 %6, 10
%8 = udiv i32 %6, 10
%9 = add nuw nsw i32 %5, 1
%10 = tail call i32 @power(i32 10, i32 %9)
%11 = sdiv i32 %0, %10
%12 = tail call i32 @power(i32 10, i32 %5)
%13 = srem i32 %0, %12
%14 = icmp ult i32 %7, 2
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = tail call i32 @power(i32 10, i32 %5)
%17 = mul nsw i32 %16, %11
br label %29
18: ; preds = %3
%19 = icmp eq i32 %7, 2
br i1 %19, label %24, label %20
20: ; preds = %18
%21 = add nsw i32 %11, 1
%22 = tail call i32 @power(i32 10, i32 %5)
%23 = mul nsw i32 %22, %21
br label %29
24: ; preds = %18
%25 = tail call i32 @power(i32 10, i32 %5)
%26 = mul nsw i32 %25, %11
%27 = add i32 %13, 1
%28 = add i32 %27, %26
br label %29
29: ; preds = %20, %24, %15
%30 = phi i32 [ %17, %15 ], [ %23, %20 ], [ %28, %24 ]
%31 = add nsw i32 %30, %4
%32 = icmp ult i32 %6, 10
br i1 %32, label %33, label %3
33: ; preds = %29, %1
%34 = phi i32 [ 0, %1 ], [ %31, %29 ]
ret i32 %34
}