#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*/ getchar();
int power(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main(int argc, char *argv[]) {
  int num;
  int frac = 1;
  int digit = 1;
  printf("Enter a number: ");
  scanf("%d", &num);
  while (1) {
    frac = num / power(10, digit);
    if (frac == 0)
      break;
    digit += 1;
  }
  printf("the digit is: %d\n\n", digit);
  int j;
  int n1;
  for (j = 0, n1 = 0; j < digit; j++) {
    int base = power(10, j + 1);
    printf("The base is: %d\n", base);
    int fra = num / base;
    printf("The fra is: %d\n", fra);
    int mod = num - base * fra;
    printf("The mod is: %d\n", mod);
    int quan = power(10, j);
    printf("The quan is: %d\n", quan);
    n1 += fra * quan;
    if (mod > 2 * quan - 1) {
      n1 += quan;
      printf("A\n");
    } else if (mod > quan - 1) {
      n1 += mod - quan + 1;
      printf("B\n");
    }
    printf("The n1 is: %d\n\n", n1);
  }
  printf("The number of ones is: %d\n", n1);
  getchar();
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe98(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%esi
mov    $0xa,%edi
call   1030 <power@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11af <main+0x6f>
jmp    11bd <main+0x7d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1182 <main+0x42>
mov    -0x1c(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12f7 <main+0x1b7>
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    $0xa,%edi
call   1030 <power@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
cltd
idivl  -0x28(%rbp)
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
mov    -0x28(%rbp),%ecx
imul   -0x2c(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%esi
mov    $0xa,%edi
call   1030 <power@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
imul   -0x34(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%eax
mov    -0x34(%rbp),%ecx
shl    $0x1,%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    12a5 <main+0x165>
mov    -0x34(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d8 <main+0x198>
mov    -0x30(%rbp),%eax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    12d3 <main+0x193>
mov    -0x30(%rbp),%eax
sub    -0x34(%rbp),%eax
add    $0x1,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d8 <main+0x198>
mov    -0x24(%rbp),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11dc <main+0x9c>
mov    -0x24(%rbp),%esi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe9f(%rip),%rdi
mov    %rsp,%rsi
call   1070 <scanf@plt>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    (%rsp),%ebp
mov    $0xa,%edi
mov    %ebx,%esi
call   1030 <power@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
test   %eax,%eax
jne    1180 <main+0x30>
lea    0xe70(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    11e9 <main+0x99>
cs nopw 0x0(%rax,%rax,1)
nop
add    %ebp,%r15d
lea    0xecc(%rip),%rdi
call   1040 <puts@plt>
lea    0xe93(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    %r14d,%ebx
je     129b <main+0x14b>
mov    %r15d,0x4(%rsp)
mov    %r14d,%r12d
lea    0x1(%r12),%r14d
mov    $0xa,%edi
mov    %r14d,%esi
call   1030 <power@plt>
mov    %eax,%ebp
lea    0xe1b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%eax
cltd
idiv   %ebp
mov    %eax,%r15d
lea    0xe13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%r13d
imul   %r15d,%ebp
sub    %ebp,%r13d
lea    0xe08(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
mov    %r12d,%esi
call   1030 <power@plt>
mov    %eax,%ebp
lea    0xdf8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
imul   %ebp,%r15d
add    0x4(%rsp),%r15d
lea    0x0(%rbp,%rbp,1),%eax
cmp    %eax,%r13d
jge    11c0 <main+0x70>
sub    %ebp,%r13d
jl     11cf <main+0x7f>
add    %r13d,%r15d
add    $0x1,%r15d
lea    0xdfe(%rip),%rdi
jmp    11ca <main+0x7a>
lea    0xdd7(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Enter a number: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [19 x i8] c"the digit is: %d\0A\0A\00"
@.str.3 = [17 x i8] c"The base is: %d\0A\00"
@.str.4 = [16 x i8] c"The fra is: %d\0A\00"
@.str.5 = [16 x i8] c"The mod is: %d\0A\00"
@.str.6 = [17 x i8] c"The quan is: %d\0A\00"
@.str.7 = [3 x i8] c"A\0A\00"
@.str.8 = [3 x i8] c"B\0A\00"
@.str.9 = [16 x i8] c"The n1 is: %d\0A\0A\00"
@.str.10 = [27 x i8] c"The number of ones is: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
store i32 1, i32* %8
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
br label %17
17: ; preds = %2, %25
%18 = load i32, i32* %6
%19 = load i32, i32* %8
%20 = call i32 @power(i32 10, i32 %19)
%21 = sdiv i32 %18, %20
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
br label %28
25: ; preds = %17
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
store i32 %27, i32* %8
br label %17
28: ; preds = %24
%29 = load i32, i32* %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %29)
store i32 0, i32* %9
store i32 0, i32* %10
br label %31
31: ; preds = %89, %28
%32 = load i32, i32* %9
%33 = load i32, i32* %8
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %92
35: ; preds = %31
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
%38 = call i32 @power(i32 10, i32 %37)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %6
%42 = load i32, i32* %11
%43 = sdiv i32 %41, %42
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %6
%47 = load i32, i32* %11
%48 = load i32, i32* %12
%49 = mul nsw i32 %47, %48
%50 = sub nsw i32 %46, %49
store i32 %50, i32* %13
%51 = load i32, i32* %13
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %9
%54 = call i32 @power(i32 10, i32 %53)
store i32 %54, i32* %14
%55 = load i32, i32* %14
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %12
%58 = load i32, i32* %14
%59 = mul nsw i32 %57, %58
%60 = load i32, i32* %10
%61 = add nsw i32 %60, %59
store i32 %61, i32* %10
%62 = load i32, i32* %13
%63 = load i32, i32* %14
%64 = mul nsw i32 2, %63
%65 = sub nsw i32 %64, 1
%66 = icmp sgt i32 %62, %65
br i1 %66, label %67, label %72
67: ; preds = %35
%68 = load i32, i32* %14
%69 = load i32, i32* %10
%70 = add nsw i32 %69, %68
store i32 %70, i32* %10
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
br label %86
72: ; preds = %35
%73 = load i32, i32* %13
%74 = load i32, i32* %14
%75 = sub nsw i32 %74, 1
%76 = icmp sgt i32 %73, %75
br i1 %76, label %77, label %85
77: ; preds = %72
%78 = load i32, i32* %13
%79 = load i32, i32* %14
%80 = sub nsw i32 %78, %79
%81 = add nsw i32 %80, 1
%82 = load i32, i32* %10
%83 = add nsw i32 %82, %81
store i32 %83, i32* %10
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
br label %85
85: ; preds = %77, %72
br label %86
86: ; preds = %85, %67
%87 = load i32, i32* %10
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i32 %87)
br label %89
89: ; preds = %86
%90 = load i32, i32* %9
%91 = add nsw i32 %90, 1
store i32 %91, i32* %9
br label %31
92: ; preds = %31
%93 = load i32, i32* %10
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0), i32 %93)
%95 = call i32 (...) @getchar()
%96 = call i32 (...) @getchar()
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"Enter a number: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [19 x i8] c"the digit is: %d\0A\0A\00"
@.str.3 = [17 x i8] c"The base is: %d\0A\00"
@.str.4 = [16 x i8] c"The fra is: %d\0A\00"
@.str.5 = [16 x i8] c"The mod is: %d\0A\00"
@.str.6 = [17 x i8] c"The quan is: %d\0A\00"
@.str.9 = [16 x i8] c"The n1 is: %d\0A\0A\00"
@.str.10 = [27 x i8] c"The number of ones is: %d\0A\00"
@str = [2 x i8] c"B\00"
@str.11 = [2 x i8] c"A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
br label %7
7: ; preds = %7, %2
%8 = phi i32 [ 1, %2 ], [ %13, %7 ]
%9 = load i32, i32* %3
%10 = call i32 @power(i32 10, i32 %8)
%11 = sdiv i32 %9, %10
%12 = icmp eq i32 %11, 0
%13 = add nuw nsw i32 %8, 1
br i1 %12, label %14, label %7
14: ; preds = %7
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %8)
br label %16
16: ; preds = %14, %47
%17 = phi i32 [ 0, %14 ], [ %19, %47 ]
%18 = phi i32 [ 0, %14 ], [ %48, %47 ]
%19 = add nuw nsw i32 %17, 1
%20 = call i32 @power(i32 10, i32 %19)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %3
%23 = sdiv i32 %22, %20
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %3
%26 = mul nsw i32 %23, %20
%27 = sub nsw i32 %25, %26
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = call i32 @power(i32 10, i32 %17)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %29)
%31 = mul nsw i32 %29, %23
%32 = add nsw i32 %31, %18
%33 = shl nsw i32 %29, 1
%34 = icmp slt i32 %27, %33
br i1 %34, label %37, label %35
35: ; preds = %16
%36 = add nsw i32 %32, %29
br label %43
37: ; preds = %16
%38 = icmp slt i32 %27, %29
br i1 %38, label %47, label %39
39: ; preds = %37
%40 = add nsw i32 %27, 1
%41 = sub i32 %40, %29
%42 = add i32 %41, %32
br label %43
43: ; preds = %35, %39
%44 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0), %39 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @str.11, i64 0, i64 0), %35 ]
%45 = phi i32 [ %42, %39 ], [ %36, %35 ]
%46 = call i32 @puts(i8* dereferenceable(1) %44)
br label %47
47: ; preds = %43, %37
%48 = phi i32 [ %32, %37 ], [ %45, %43 ]
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i32 %48)
%50 = icmp eq i32 %19, %8
br i1 %50, label %51, label %16
51: ; preds = %47
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0), i32 %48)
%53 = call i32 (...) @getchar()
%54 = call i32 (...) @getchar()
ret i32 0
}