#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*/ die(char *);
int num_digits(int n) {
  int ndigits = 0;
  if (n < 0)
    die("oops, num_digits called with negative value");
  if (n == 0)
    return 1;
  while (n) {
    n /= 10;
    ndigits++;
  }
  return ndigits;
}


x86 assembly (clang, -O0):
<num_digits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1138 <num_digits+0x28>
lea    0xecd(%rip),%rdi
call   1030 <die@plt>
cmpl   $0x0,-0x8(%rbp)
jne    114e <num_digits+0x3e>
movl   $0x1,-0x4(%rbp)
jmp    117f <num_digits+0x6f>
jmp    1153 <num_digits+0x43>
cmpl   $0x0,-0x8(%rbp)
je     1179 <num_digits+0x69>
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1153 <num_digits+0x43>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_digits>:
push   %rbx
mov    %edi,%ebx
test   %edi,%edi
js     1120 <num_digits+0x10>
jne    112c <num_digits+0x1c>
mov    $0x1,%eax
jmp    1152 <num_digits+0x42>
lea    0xed9(%rip),%rdi
call   1030 <die@plt>
xor    %eax,%eax
xchg   %ax,%ax
movslq %ebx,%rcx
imul   $0x66666667,%rcx,%rbx
mov    %rbx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rbx
add    %edx,%ebx
add    $0x1,%eax
add    $0x9,%ecx
cmp    $0x12,%ecx
ja     1130 <num_digits+0x20>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [44 x i8] c"oops, num_digits called with negative value\00"
define i32 @num_digits(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 1, i32* %2
br label %24
13: ; preds = %9
br label %14
14: ; preds = %17, %13
%15 = load i32, i32* %3
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i32, i32* %3
%19 = sdiv i32 %18, 10
store i32 %19, i32* %3
%20 = load i32, i32* %4
%21 = add nsw i32 %20, 1
store i32 %21, i32* %4
br label %14
22: ; preds = %14
%23 = load i32, i32* %4
store i32 %23, i32* %2
br label %24
24: ; preds = %22, %12
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@.str = [44 x i8] c"oops, num_digits called with negative value\00"
define i32 @num_digits(i32 %0) {
%2 = icmp slt i32 %0, 0
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %7
5: ; preds = %1
%6 = icmp eq i32 %0, 0
br i1 %6, label %15, label %7
7: ; preds = %3, %5
br label %8
8: ; preds = %7, %8
%9 = phi i32 [ %12, %8 ], [ 0, %7 ]
%10 = phi i32 [ %11, %8 ], [ %0, %7 ]
%11 = sdiv i32 %10, 10
%12 = add nuw nsw i32 %9, 1
%13 = add i32 %10, 9
%14 = icmp ult i32 %13, 19
br i1 %14, label %15, label %8
15: ; preds = %8, %5
%16 = phi i32 [ 1, %5 ], [ %12, %8 ]
ret i32 %16
}