#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 count_ones(int);
int start_div(int);
int max_ones(int n) {
  int div = start_div(n);
  int most = count_ones(n / div);
  div /= 10;
  while (div) {
    int next = count_ones(n / div);
    if (next > most)
      most = next;
    div /= 10;
  }
  return most;
}


x86 assembly (clang, -O0):
<max_ones>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <start_div@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %eax,%edi
call   1030 <count_ones@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1195 <max_ones+0x75>
mov    -0x4(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %eax,%edi
call   1030 <count_ones@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    1182 <max_ones+0x62>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    1155 <max_ones+0x35>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<max_ones>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
call   1040 <start_div@plt>
mov    %eax,%r15d
mov    %r14d,%eax
cltd
idiv   %r15d
mov    %eax,%edi
call   1030 <count_ones@plt>
mov    %eax,%r12d
lea    0x9(%r15),%eax
cmp    $0x13,%eax
jb     1189 <max_ones+0x69>
nopl   (%rax)
movslq %r15d,%rax
imul   $0x66666667,%rax,%rbx
mov    %rbx,%rbp
shr    $0x3f,%rbp
sar    $0x22,%rbx
lea    (%rbx,%rbp,1),%r15d
mov    %r14d,%eax
cltd
idiv   %r15d
mov    %eax,%edi
call   1030 <count_ones@plt>
cmp    %r12d,%eax
cmovg  %eax,%r12d
lea    (%rbx,%rbp,1),%eax
add    $0x9,%eax
cmp    $0x12,%eax
ja     1150 <max_ones+0x30>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @max_ones(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @start_div(i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = load i32, i32* %3
%10 = sdiv i32 %8, %9
%11 = call i32 @count_ones(i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %3
%13 = sdiv i32 %12, 10
store i32 %13, i32* %3
br label %14
14: ; preds = %27, %1
%15 = load i32, i32* %3
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %14
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = sdiv i32 %18, %19
%21 = call i32 @count_ones(i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = load i32, i32* %4
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = load i32, i32* %5
store i32 %26, i32* %4
br label %27
27: ; preds = %25, %17
%28 = load i32, i32* %3
%29 = sdiv i32 %28, 10
store i32 %29, i32* %3
br label %14
30: ; preds = %14
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
define i32 @max_ones(i32 %0) {
%2 = tail call i32 @start_div(i32 %0)
%3 = sdiv i32 %0, %2
%4 = tail call i32 @count_ones(i32 %3)
%5 = add i32 %2, 9
%6 = icmp ult i32 %5, 19
br i1 %6, label %17, label %7
7: ; preds = %1, %7
%8 = phi i32 [ %10, %7 ], [ %2, %1 ]
%9 = phi i32 [ %14, %7 ], [ %4, %1 ]
%10 = sdiv i32 %8, 10
%11 = sdiv i32 %0, %10
%12 = tail call i32 @count_ones(i32 %11)
%13 = icmp sgt i32 %12, %9
%14 = select i1 %13, i32 %12, i32 %9
%15 = add nsw i32 %10, 9
%16 = icmp ult i32 %15, 19
br i1 %16, label %17, label %7
17: ; preds = %7, %1
%18 = phi i32 [ %4, %1 ], [ %14, %7 ]
ret i32 %18
}