#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*/ sprintf(char *, char *, ...);
char *percentage(unsigned long partial, unsigned long total) {
  static char buffer[10][64];
  static short k = -1;
  float percent;
  k = (k + 1) % 10;
  if (partial && total) {
    percent = (float)partial * 100 / (float)total;
    if (partial == total)
      sprintf(buffer[k], "(%3d%%) ", (int)percent);
    else
      sprintf(buffer[k], "(%4.*f%%)", 2, percent);
  } else
    sprintf(buffer[k], " ");
  return buffer[k];
}


x86 assembly (clang, -O0):
<percentage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movswl 0x2f01(%rip),%eax
add    $0x1,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %dx,%ax
mov    %ax,0x2eec(%rip)
cmpq   $0x0,-0x8(%rbp)
je     1268 <percentage+0x158>
cmpq   $0x0,-0x10(%rbp)
je     1268 <percentage+0x158>
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x1c(%rbp)
test   %rax,%rax
movss  %xmm0,-0x18(%rbp)
js     118f <percentage+0x7f>
movss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0x18(%rbp)
movss  -0x18(%rbp),%xmm0
movss  0xe64(%rip),%xmm1
mulss  %xmm1,%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x24(%rbp)
test   %rax,%rax
movss  %xmm0,-0x20(%rbp)
js     11e2 <percentage+0xd2>
movss  -0x24(%rbp),%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x28(%rbp),%xmm0
movss  -0x20(%rbp),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1231 <percentage+0x121>
movswq 0x2e1d(%rip),%rax
lea    0x2e2e(%rip),%rdi
shl    $0x6,%rax
add    %rax,%rdi
cvttss2si -0x14(%rbp),%edx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
jmp    1263 <percentage+0x153>
movswq 0x2def(%rip),%rax
lea    0x2e00(%rip),%rdi
shl    $0x6,%rax
add    %rax,%rdi
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdb6(%rip),%rsi
mov    $0x2,%edx
mov    $0x1,%al
call   1030 <sprintf@plt>
jmp    128c <percentage+0x17c>
movswq 0x2db8(%rip),%rax
lea    0x2dc9(%rip),%rdi
shl    $0x6,%rax
add    %rax,%rdi
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
movswq 0x2d94(%rip),%rcx
lea    0x2da5(%rip),%rax
shl    $0x6,%rcx
add    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<percentage>:
push   %rax
movswl 0x2f10(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   $0x66666667,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %ax,0x2ee1(%rip)
test   %rdi,%rdi
je     115d <percentage+0x4d>
test   %rsi,%rsi
je     115d <percentage+0x4d>
test   %rdi,%rdi
js     1176 <percentage+0x66>
cvtsi2ss %rdi,%xmm0
jmp    118d <percentage+0x7d>
movzwl %ax,%eax
shl    $0x6,%rax
lea    0x2ed5(%rip),%rcx
movw   $0x20,(%rax,%rcx,1)
jmp    1203 <percentage+0xf3>
mov    %rdi,%rcx
shr    %rcx
mov    %edi,%edx
and    $0x1,%edx
or     %rcx,%rdx
cvtsi2ss %rdx,%xmm0
addss  %xmm0,%xmm0
mulss  0xe6b(%rip),%xmm0
test   %rsi,%rsi
js     11a1 <percentage+0x91>
cvtsi2ss %rsi,%xmm1
jmp    11b8 <percentage+0xa8>
mov    %rsi,%rcx
shr    %rcx
mov    %esi,%edx
and    $0x1,%edx
or     %rcx,%rdx
cvtsi2ss %rdx,%xmm1
addss  %xmm1,%xmm1
divss  %xmm1,%xmm0
movzwl %ax,%ecx
shl    $0x6,%rcx
lea    0x2e76(%rip),%rax
add    %rcx,%rax
cmp    %rsi,%rdi
jne    11e9 <percentage+0xd9>
cvttss2si %xmm0,%edx
lea    0xe27(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1030 <sprintf@plt>
jmp    1203 <percentage+0xf3>
cvtss2sd %xmm0,%xmm0
lea    0xe19(%rip),%rsi
mov    %rax,%rdi
mov    $0x2,%edx
mov    $0x1,%al
call   1030 <sprintf@plt>
movswq 0x2e1d(%rip),%rcx
shl    $0x6,%rcx
lea    0x2e2a(%rip),%rax
add    %rcx,%rax
pop    %rcx
ret

LLVM IR (-O0):
@percentage.buffer = internal global [10 x [64 x i8]] zeroinitializer
@percentage.k = internal global i16 -1
@.str = [9 x i8] c"(%3d%%) \00"
@.str.1 = [10 x i8] c"(%4.*f%%)\00"
@.str.2 = [2 x i8] c" \00"
define i8* @percentage(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca float
store i64 %0, i64* %3
store i64 %1, i64* %4
%6 = load i16, i16* @percentage.k
%7 = sext i16 %6 to i32
%8 = add nsw i32 %7, 1
%9 = srem i32 %8, 10
%10 = trunc i32 %9 to i16
store i16 %10, i16* @percentage.k
%11 = load i64, i64* %3
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %43
13: ; preds = %2
%14 = load i64, i64* %4
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %43
16: ; preds = %13
%17 = load i64, i64* %3
%18 = uitofp i64 %17 to float
%19 = fmul float %18, 1.000000e+02
%20 = load i64, i64* %4
%21 = uitofp i64 %20 to float
%22 = fdiv float %19, %21
store float %22, float* %5
%23 = load i64, i64* %3
%24 = load i64, i64* %4
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %34
26: ; preds = %16
%27 = load i16, i16* @percentage.k
%28 = sext i16 %27 to i64
%29 = getelementptr inbounds [10 x [64 x i8]], [10 x [64 x i8]]* @percentage.buffer, i64 0, i64 %28
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %29, i64 0, i64 0
%31 = load float, float* %5
%32 = fptosi float %31 to i32
%33 = call i32 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %32)
br label %42
34: ; preds = %16
%35 = load i16, i16* @percentage.k
%36 = sext i16 %35 to i64
%37 = getelementptr inbounds [10 x [64 x i8]], [10 x [64 x i8]]* @percentage.buffer, i64 0, i64 %36
%38 = getelementptr inbounds [64 x i8], [64 x i8]* %37, i64 0, i64 0
%39 = load float, float* %5
%40 = fpext float %39 to double
%41 = call i32 (i8*, i8*, ...) @sprintf(i8* %38, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 2, double %40)
br label %42
42: ; preds = %34, %26
br label %49
43: ; preds = %13, %2
%44 = load i16, i16* @percentage.k
%45 = sext i16 %44 to i64
%46 = getelementptr inbounds [10 x [64 x i8]], [10 x [64 x i8]]* @percentage.buffer, i64 0, i64 %45
%47 = getelementptr inbounds [64 x i8], [64 x i8]* %46, i64 0, i64 0
%48 = call i32 (i8*, i8*, ...) @sprintf(i8* %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %43, %42
%50 = load i16, i16* @percentage.k
%51 = sext i16 %50 to i64
%52 = getelementptr inbounds [10 x [64 x i8]], [10 x [64 x i8]]* @percentage.buffer, i64 0, i64 %51
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %52, i64 0, i64 0
ret i8* %53
}

LLVM IR (-O2):
@percentage.buffer = internal global [10 x [64 x i8]] zeroinitializer
@percentage.k = internal unnamed_addr global i16 -1
@.str = [9 x i8] c"(%3d%%) \00"
@.str.1 = [10 x i8] c"(%4.*f%%)\00"
define i8* @percentage(i64 %0, i64 %1) {
%3 = load i16, i16* @percentage.k
%4 = sext i16 %3 to i32
%5 = add nsw i32 %4, 1
%6 = srem i32 %5, 10
%7 = trunc i32 %6 to i16
store i16 %7, i16* @percentage.k
%8 = icmp ne i64 %0, 0
%9 = icmp ne i64 %1, 0
%10 = and i1 %8, %9
br i1 %10, label %11, label %26
11: ; preds = %2
%12 = uitofp i64 %0 to float
%13 = fmul float %12, 1.000000e+02
%14 = uitofp i64 %1 to float
%15 = fdiv float %13, %14
%16 = icmp eq i64 %0, %1
%17 = and i32 %6, 65535
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [10 x [64 x i8]], [10 x [64 x i8]]* @percentage.buffer, i64 0, i64 %18, i64 0
br i1 %16, label %20, label %23
20: ; preds = %11
%21 = fptosi float %15 to i32
%22 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %31
23: ; preds = %11
%24 = fpext float %15 to double
%25 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 2, double %24)
br label %31
26: ; preds = %2
%27 = and i32 %6, 65535
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds [10 x [64 x i8]], [10 x [64 x i8]]* @percentage.buffer, i64 0, i64 %28, i64 0
%30 = bitcast i8* %29 to i16*
store i16 32, i16* %30
br label %31
31: ; preds = %20, %23, %26
%32 = load i16, i16* @percentage.k
%33 = sext i16 %32 to i64
%34 = getelementptr inbounds [10 x [64 x i8]], [10 x [64 x i8]]* @percentage.buffer, i64 0, i64 %33, i64 0
ret i8* %34
}