#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 */
typedef scalar_t__ uint64_t;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, double);
int /*<<< orphan*/ strcpy(char *, char *);
char *percent(uint64_t val, uint64_t total) {
  static char buffer[32];
  if (total == 0) {
    strcpy(buffer, "?%");
    return buffer;
  }
  if (val >= total) {
    strcpy(buffer, "100%");
    return buffer;
  }
  double p = (double)val;
  p /= (double)total;
  p *= 100;
  if (p < 10.0)
    sprintf(buffer, "%.02f%%", p);
  else
    sprintf(buffer, "%.01f%%", p);
  return buffer;
}


x86 assembly (clang, -O0):
<percent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115e <percent+0x3e>
lea    0x2efe(%rip),%rdi
lea    0xec7(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2eeb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1213 <percent+0xf3>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jl     118f <percent+0x6f>
lea    0x2ecd(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2eba(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1213 <percent+0xf3>
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
cvtsi2sdq -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe52(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe38(%rip),%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    11f0 <percent+0xd0>
movsd  -0x20(%rbp),%xmm0
lea    0x2e61(%rip),%rdi
lea    0xe32(%rip),%rsi
call   1040 <sprintf@plt>
jmp    1208 <percent+0xe8>
movsd  -0x20(%rbp),%xmm0
lea    0x2e44(%rip),%rdi
lea    0xe1d(%rip),%rsi
call   1040 <sprintf@plt>
lea    0x2e31(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<percent>:
push   %rax
test   %rsi,%rsi
je     115f <percent+0x3f>
cmp    %rsi,%rdi
jge    1174 <percent+0x54>
cvtsi2sd %rdi,%xmm0
cvtsi2sd %rsi,%xmm1
divsd  %xmm1,%xmm0
mulsd  0xebf(%rip),%xmm0
movsd  0xebf(%rip),%xmm1
lea    0x2ef0(%rip),%rdi
ucomisd %xmm0,%xmm1
jbe    1189 <percent+0x69>
lea    0xebb(%rip),%rsi
jmp    1190 <percent+0x70>
lea    0x2eda(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1195 <percent+0x75>
lea    0x2ec5(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1195 <percent+0x75>
lea    0xe90(%rip),%rsi
call   1040 <sprintf@plt>
lea    0x2ea4(%rip),%rax
pop    %rcx
ret

LLVM IR (-O0):
@percent.buffer = internal global [32 x i8] zeroinitializer
@.str = [3 x i8] c"?%\00"
@.str.1 = [5 x i8] c"100%\00"
@.str.2 = [8 x i8] c"%.02f%%\00"
@.str.3 = [8 x i8] c"%.01f%%\00"
define i8* @percent(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca double
store i64 %0, i64* %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @strcpy(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8** %3
br label %35
11: ; preds = %2
%12 = load i64, i64* %4
%13 = load i64, i64* %5
%14 = icmp sge i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @strcpy(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8** %3
br label %35
17: ; preds = %11
%18 = load i64, i64* %4
%19 = sitofp i64 %18 to double
store double %19, double* %6
%20 = load i64, i64* %5
%21 = sitofp i64 %20 to double
%22 = load double, double* %6
%23 = fdiv double %22, %21
store double %23, double* %6
%24 = load double, double* %6
%25 = fmul double %24, 1.000000e+02
store double %25, double* %6
%26 = load double, double* %6
%27 = fcmp olt double %26, 1.000000e+01
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = load double, double* %6
%30 = call i32 @sprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), double %29)
br label %34
31: ; preds = %17
%32 = load double, double* %6
%33 = call i32 @sprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double %32)
br label %34
34: ; preds = %31, %28
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8** %3
br label %35
35: ; preds = %34, %15, %9
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@percent.buffer = internal global [32 x i8] zeroinitializer
@.str = [3 x i8] c"?%\00"
@.str.1 = [5 x i8] c"100%\00"
@.str.2 = [8 x i8] c"%.02f%%\00"
@.str.3 = [8 x i8] c"%.01f%%\00"
define i8* @percent(i64 %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @strcpy(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %20
6: ; preds = %2
%7 = icmp slt i64 %0, %1
br i1 %7, label %10, label %8
8: ; preds = %6
%9 = tail call i32 @strcpy(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %20
10: ; preds = %6
%11 = sitofp i64 %0 to double
%12 = sitofp i64 %1 to double
%13 = fdiv double %11, %12
%14 = fmul double %13, 1.000000e+02
%15 = fcmp olt double %14, 1.000000e+01
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), double %14)
br label %20
18: ; preds = %10
%19 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double %14)
br label %20
20: ; preds = %16, %18, %8, %4
ret i8* getelementptr inbounds ([32 x i8], [32 x i8]* @percent.buffer, i64 0, i64 0)
}