#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 */
unsigned long long get_num_digits(unsigned long long);
int /*<<< orphan*/ printf(char *, unsigned long long, unsigned long long, ...);
unsigned long long num_chars(unsigned long long start_range,
                             unsigned long long end_range) {
  unsigned long long range = end_range;
  unsigned long long num_digits;
  unsigned long long ret_val = 0;
  unsigned long long i = 0;
  unsigned char done = 0;
  unsigned long long next_decrease;
  printf("====== start:%llu, end:%llu =====\n", start_range, end_range);
  while (!done) {
    num_digits = get_num_digits(range);
    printf("num_digits(%llu), range(%llu)\n", num_digits, range);
    next_decrease = 1;
    for (i = 0; i < num_digits - 2 && num_digits > 1; ++i) {
      next_decrease *= 10;
      next_decrease += 1;
    }
    if (num_digits > 1) {
      next_decrease *= 9;
    } else {
      next_decrease = 0;
    }
    if (next_decrease < start_range) {
      next_decrease = start_range;
    }
    printf(
        "next_decrease(%llu), increase(%llu), range(%llu), num_digits(%llu)\n",
        next_decrease, ((range - next_decrease) * num_digits), range,
        num_digits);
    ret_val += ((range - next_decrease) * (num_digits));
    if (next_decrease == start_range) {
      ret_val--;
      break;
    }
    range = next_decrease;
  }
  printf("range(%llu, %llu):%llu\n", start_range, end_range, ret_val);
  return ret_val;
}


x86 assembly (clang, -O0):
<num_chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movb   $0x0,-0x31(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpb   $0x0,-0x31(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1178 <num_chars+0x58>
jmp    12a5 <num_chars+0x185>
mov    -0x18(%rbp),%rdi
call   1040 <get_num_digits@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x1,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rdx
sub    $0x2,%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x41(%rbp)
jae    11d0 <num_chars+0xb0>
cmpq   $0x1,-0x20(%rbp)
seta   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11e0 <num_chars+0xc0>
jmp    1206 <num_chars+0xe6>
imul   $0xa,-0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11ab <num_chars+0x8b>
cmpq   $0x1,-0x20(%rbp)
jbe    121f <num_chars+0xff>
imul   $0x9,-0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    1227 <num_chars+0x107>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    123d <num_chars+0x11d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x40(%rbp),%rdx
imul   -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
sub    -0x40(%rbp),%rax
imul   -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1298 <num_chars+0x178>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    12a5 <num_chars+0x185>
mov    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1162 <num_chars+0x42>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_chars>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%rsi
lea    0xec5(%rip),%rdi
mov    %rsi,0x8(%rsp)
mov    %r12,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %ebx,%ebx
mov    %r12,0x10(%rsp)
mov    0x8(%rsp),%r13
jmp    11a7 <num_chars+0x87>
nopl   0x0(%rax,%rax,1)
cmp    $0x2,%r14
lea    (%rax,%rax,8),%r15
mov    $0x0,%eax
cmovb  %rax,%r15
cmp    %r13,%r15
mov    %r15,%rbp
cmovb  %r13,%rbp
mov    %r12,%rcx
sub    %rbp,%r12
imul   %r14,%r12
lea    0xeb6(%rip),%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    %r14,%r8
xor    %eax,%eax
call   1030 <printf@plt>
add    %r12,%rbx
mov    %rbp,%r12
cmp    %r13,%r15
jbe    1206 <num_chars+0xe6>
mov    %r12,%rdi
call   1040 <get_num_digits@plt>
mov    %rax,%r14
lea    0xe6a(%rip),%rdi
mov    %rax,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%eax
cmp    $0x3,%r14
jb     1160 <num_chars+0x40>
lea    -0x2(%r14),%rcx
mov    $0x1,%edx
mov    $0x1,%eax
nop
add    %rax,%rax
lea    (%rax,%rax,4),%rax
or     $0x1,%rax
cmp    $0x2,%r14
jb     1160 <num_chars+0x40>
lea    0x1(%rdx),%rsi
cmp    %rcx,%rdx
mov    %rsi,%rdx
jb     11e0 <num_chars+0xc0>
jmp    1160 <num_chars+0x40>
add    $0xffffffffffffffff,%rbx
lea    0xe75(%rip),%rdi
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"====== start:%llu, end:%llu =====\0A\00"
@.str.1 = [31 x i8] c"num_digits(%llu), range(%llu)\0A\00"
@.str.2 = [68 x i8] c"next_decrease(%llu), increase(%llu), range(%llu), num_digits(%llu)\0A\00"
@.str.3 = [24 x i8] c"range(%llu, %llu):%llu\0A\00"
define i64 @num_chars(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8
%10 = alloca i64
store i64 %0, i64* %3
store i64 %1, i64* %4
%11 = load i64, i64* %4
store i64 %11, i64* %5
store i64 0, i64* %7
store i64 0, i64* %8
store i8 0, i8* %9
%12 = load i64, i64* %3
%13 = load i64, i64* %4
%14 = call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %12, i64 %13)
br label %15
15: ; preds = %79, %2
%16 = load i8, i8* %9
%17 = icmp ne i8 %16, 0
%18 = xor i1 %17, true
br i1 %18, label %19, label %81
19: ; preds = %15
%20 = load i64, i64* %5
%21 = call i64 @get_num_digits(i64 %20)
store i64 %21, i64* %6
%22 = load i64, i64* %6
%23 = load i64, i64* %5
%24 = call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %22, i64 %23)
store i64 1, i64* %10
store i64 0, i64* %8
br label %25
25: ; preds = %40, %19
%26 = load i64, i64* %8
%27 = load i64, i64* %6
%28 = sub i64 %27, 2
%29 = icmp ult i64 %26, %28
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i64, i64* %6
%32 = icmp ugt i64 %31, 1
br label %33
33: ; preds = %30, %25
%34 = phi i1 [ false, %25 ], [ %32, %30 ]
br i1 %34, label %35, label %43
35: ; preds = %33
%36 = load i64, i64* %10
%37 = mul i64 %36, 10
store i64 %37, i64* %10
%38 = load i64, i64* %10
%39 = add i64 %38, 1
store i64 %39, i64* %10
br label %40
40: ; preds = %35
%41 = load i64, i64* %8
%42 = add i64 %41, 1
store i64 %42, i64* %8
br label %25
43: ; preds = %33
%44 = load i64, i64* %6
%45 = icmp ugt i64 %44, 1
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i64, i64* %10
%48 = mul i64 %47, 9
store i64 %48, i64* %10
br label %50
49: ; preds = %43
store i64 0, i64* %10
br label %50
50: ; preds = %49, %46
%51 = load i64, i64* %10
%52 = load i64, i64* %3
%53 = icmp ult i64 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = load i64, i64* %3
store i64 %55, i64* %10
br label %56
56: ; preds = %54, %50
%57 = load i64, i64* %10
%58 = load i64, i64* %5
%59 = load i64, i64* %10
%60 = sub i64 %58, %59
%61 = load i64, i64* %6
%62 = mul i64 %60, %61
%63 = load i64, i64* %5
%64 = load i64, i64* %6
%65 = call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i64 %57, i64 %62, i64 %63, i64 %64)
%66 = load i64, i64* %5
%67 = load i64, i64* %10
%68 = sub i64 %66, %67
%69 = load i64, i64* %6
%70 = mul i64 %68, %69
%71 = load i64, i64* %7
%72 = add i64 %71, %70
store i64 %72, i64* %7
%73 = load i64, i64* %10
%74 = load i64, i64* %3
%75 = icmp eq i64 %73, %74
br i1 %75, label %76, label %79
76: ; preds = %56
%77 = load i64, i64* %7
%78 = add i64 %77, -1
store i64 %78, i64* %7
br label %81
79: ; preds = %56
%80 = load i64, i64* %10
store i64 %80, i64* %5
br label %15
81: ; preds = %76, %15
%82 = load i64, i64* %3
%83 = load i64, i64* %4
%84 = load i64, i64* %7
%85 = call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i64 %82, i64 %83, i64 %84)
%86 = load i64, i64* %7
ret i64 %86
}

LLVM IR (-O2):
@.str = [35 x i8] c"====== start:%llu, end:%llu =====\0A\00"
@.str.1 = [31 x i8] c"num_digits(%llu), range(%llu)\0A\00"
@.str.2 = [68 x i8] c"next_decrease(%llu), increase(%llu), range(%llu), num_digits(%llu)\0A\00"
@.str.3 = [24 x i8] c"range(%llu, %llu):%llu\0A\00"
define i64 @num_chars(i64 %0, i64 %1) {
%3 = tail call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %0, i64 %1)
br label %4
4: ; preds = %20, %2
%5 = phi i64 [ 0, %2 ], [ %29, %20 ]
%6 = phi i64 [ %1, %2 ], [ %25, %20 ]
%7 = tail call i64 @get_num_digits(i64 %6)
%8 = tail call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i64 %7, i64 %6)
%9 = add i64 %7, -2
%10 = icmp ugt i64 %7, 1
%11 = icmp ugt i64 %7, 2
br i1 %11, label %12, label %20
12: ; preds = %4, %12
%13 = phi i64 [ %16, %12 ], [ 1, %4 ]
%14 = phi i64 [ %17, %12 ], [ 0, %4 ]
%15 = mul i64 %13, 10
%16 = or i64 %15, 1
%17 = add nuw i64 %14, 1
%18 = icmp ult i64 %17, %9
%19 = and i1 %10, %18
br i1 %19, label %12, label %20
20: ; preds = %12, %4
%21 = phi i64 [ 1, %4 ], [ %16, %12 ]
%22 = mul i64 %21, 9
%23 = select i1 %10, i64 %22, i64 0
%24 = icmp ult i64 %23, %0
%25 = select i1 %24, i64 %0, i64 %23
%26 = sub i64 %6, %25
%27 = mul i64 %26, %7
%28 = tail call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0), i64 %25, i64 %27, i64 %6, i64 %7)
%29 = add i64 %27, %5
%30 = icmp ugt i64 %23, %0
br i1 %30, label %4, label %31
31: ; preds = %20
%32 = add i64 %29, -1
%33 = tail call i32 (i8*, i64, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i64 %0, i64 %1, i64 %32)
ret i64 %32
}