#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 */
char *xasprintf(char *, unsigned long, ...);
char *cpufreq_duration_to_string(unsigned long duration) {
  unsigned long tmp;
  if (duration > 1000000) {
    tmp = duration % 10000;
    if (tmp >= 5000)
      duration += 10000;
    return xasprintf("%u.%02ums", ((unsigned int)duration / 1000000),
                     ((unsigned int)(duration % 1000000) / 10000));
  } else if (duration > 100000) {
    tmp = duration % 1000;
    if (tmp >= 500)
      duration += 1000;
    return xasprintf("%uus", ((unsigned int)duration / 1000));
  } else if (duration > 1000) {
    tmp = duration % 100;
    if (tmp >= 50)
      duration += 100;
    return xasprintf("%u.%01uus", ((unsigned int)duration / 1000),
                     ((unsigned int)(duration % 1000) / 100));
  } else
    return xasprintf("%luns", duration);
}


x86 assembly (clang, -O0):
<cpufreq_duration_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0xf4240,-0x10(%rbp)
jbe    119b <cpufreq_duration_to_string+0x8b>
mov    -0x10(%rbp),%rax
mov    $0x2710,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x18(%rbp)
cmpq   $0x1388,-0x18(%rbp)
jb     1158 <cpufreq_duration_to_string+0x48>
mov    -0x10(%rbp),%rax
add    $0x2710,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%eax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    $0x2710,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%edx
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <xasprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    128f <cpufreq_duration_to_string+0x17f>
cmpq   $0x186a0,-0x10(%rbp)
jbe    11ff <cpufreq_duration_to_string+0xef>
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x18(%rbp)
cmpq   $0x1f4,-0x18(%rbp)
jb     11d7 <cpufreq_duration_to_string+0xc7>
mov    -0x10(%rbp),%rax
add    $0x3e8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%eax
mov    %eax,%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <xasprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    128f <cpufreq_duration_to_string+0x17f>
cmpq   $0x3e8,-0x10(%rbp)
jbe    1279 <cpufreq_duration_to_string+0x169>
mov    -0x10(%rbp),%rax
mov    $0x64,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x18(%rbp)
cmpq   $0x32,-0x18(%rbp)
jb     1236 <cpufreq_duration_to_string+0x126>
mov    -0x10(%rbp),%rax
add    $0x64,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%eax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    $0x64,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%edx
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1030 <xasprintf@plt>
mov    %rax,-0x8(%rbp)
jmp    128f <cpufreq_duration_to_string+0x17f>
mov    -0x10(%rbp),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1030 <xasprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpufreq_duration_to_string>:
mov    %rdi,%rsi
cmp    $0xf4241,%rdi
jb     1195 <cpufreq_duration_to_string+0x85>
movabs $0x346dc5d63886594b,%rcx
c5 6d 34
mov    %rsi,%rax
mul    %rcx
shr    $0xb,%rdx
imul   $0x2710,%rdx,%rax
mov    %rsi,%rdx
sub    %rax,%rdx
lea    0x2710(%rsi),%rcx
cmp    $0x1388,%rdx
cmovb  %rsi,%rcx
mov    %ecx,%eax
imul   $0x431bde83,%rax,%rsi
shr    $0x32,%rsi
movabs $0x431bde82d7b634db,%rdx
de 1b 43
mov    %rcx,%rax
mul    %rdx
shr    $0x12,%rdx
imul   $0xf4240,%rdx,%rax
sub    %rax,%rcx
mov    $0xd1b71759,%edx
imul   %rcx,%rdx
shr    $0x2d,%rdx
lea    0xe72(%rip),%rdi
xor    %eax,%eax
jmp    1030 <xasprintf@plt>
cmp    $0x186a1,%rsi
jb     11e0 <cpufreq_duration_to_string+0xd0>
mov    %esi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%eax
mov    %esi,%ecx
sub    %eax,%ecx
lea    0x3e8(%rsi),%eax
cmp    $0x1f4,%ecx
cmovb  %rsi,%rax
mov    %eax,%eax
imul   $0x10624dd3,%rax,%rsi
shr    $0x26,%rsi
lea    0xe31(%rip),%rdi
xor    %eax,%eax
jmp    1030 <xasprintf@plt>
cmp    $0x3e9,%rsi
jb     123a <cpufreq_duration_to_string+0x12a>
mov    %esi,%eax
imul   $0x51eb851f,%rax,%rax
shr    $0x25,%rax
imul   $0x64,%eax,%eax
mov    %esi,%ecx
sub    %eax,%ecx
lea    0x64(%rsi),%rax
cmp    $0x32,%ecx
cmovb  %rsi,%rax
mov    %eax,%ecx
imul   $0x10624dd3,%rcx,%rsi
shr    $0x26,%rsi
imul   $0x3e8,%esi,%ecx
sub    %ecx,%eax
movzwl %ax,%eax
shr    $0x2,%eax
imul   $0x147b,%eax,%edx
shr    $0x11,%edx
lea    0xddc(%rip),%rdi
xor    %eax,%eax
jmp    1030 <xasprintf@plt>
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
jmp    1030 <xasprintf@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"%u.%02ums\00"
@.str.1 = [5 x i8] c"%uus\00"
@.str.2 = [10 x i8] c"%u.%01uus\00"
@.str.3 = [6 x i8] c"%luns\00"
define i8* @cpufreq_duration_to_string(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = icmp ugt i64 %5, 1000000
br i1 %6, label %7, label %25
7: ; preds = %1
%8 = load i64, i64* %3
%9 = urem i64 %8, 10000
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = icmp uge i64 %10, 5000
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i64, i64* %3
%14 = add i64 %13, 10000
store i64 %14, i64* %3
br label %15
15: ; preds = %12, %7
%16 = load i64, i64* %3
%17 = trunc i64 %16 to i32
%18 = udiv i32 %17, 1000000
%19 = zext i32 %18 to i64
%20 = load i64, i64* %3
%21 = urem i64 %20, 1000000
%22 = trunc i64 %21 to i32
%23 = udiv i32 %22, 10000
%24 = call i8* (i8*, i64, ...) @xasprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 %19, i32 %23)
store i8* %24, i8** %2
br label %66
25: ; preds = %1
%26 = load i64, i64* %3
%27 = icmp ugt i64 %26, 100000
br i1 %27, label %28, label %42
28: ; preds = %25
%29 = load i64, i64* %3
%30 = urem i64 %29, 1000
store i64 %30, i64* %4
%31 = load i64, i64* %4
%32 = icmp uge i64 %31, 500
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i64, i64* %3
%35 = add i64 %34, 1000
store i64 %35, i64* %3
br label %36
36: ; preds = %33, %28
%37 = load i64, i64* %3
%38 = trunc i64 %37 to i32
%39 = udiv i32 %38, 1000
%40 = zext i32 %39 to i64
%41 = call i8* (i8*, i64, ...) @xasprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %40)
store i8* %41, i8** %2
br label %66
42: ; preds = %25
%43 = load i64, i64* %3
%44 = icmp ugt i64 %43, 1000
br i1 %44, label %45, label %63
45: ; preds = %42
%46 = load i64, i64* %3
%47 = urem i64 %46, 100
store i64 %47, i64* %4
%48 = load i64, i64* %4
%49 = icmp uge i64 %48, 50
br i1 %49, label %50, label %53
50: ; preds = %45
%51 = load i64, i64* %3
%52 = add i64 %51, 100
store i64 %52, i64* %3
br label %53
53: ; preds = %50, %45
%54 = load i64, i64* %3
%55 = trunc i64 %54 to i32
%56 = udiv i32 %55, 1000
%57 = zext i32 %56 to i64
%58 = load i64, i64* %3
%59 = urem i64 %58, 1000
%60 = trunc i64 %59 to i32
%61 = udiv i32 %60, 100
%62 = call i8* (i8*, i64, ...) @xasprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %57, i32 %61)
store i8* %62, i8** %2
br label %66
63: ; preds = %42
%64 = load i64, i64* %3
%65 = call i8* (i8*, i64, ...) @xasprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %64)
store i8* %65, i8** %2
br label %66
66: ; preds = %63, %53, %36, %15
%67 = load i8*, i8** %2
ret i8* %67
}

LLVM IR (-O2):
@.str = [10 x i8] c"%u.%02ums\00"
@.str.1 = [5 x i8] c"%uus\00"
@.str.2 = [10 x i8] c"%u.%01uus\00"
@.str.3 = [6 x i8] c"%luns\00"
define i8* @cpufreq_duration_to_string(i64 %0) {
%2 = icmp ugt i64 %0, 1000000
br i1 %2, label %3, label %15
3: ; preds = %1
%4 = urem i64 %0, 10000
%5 = icmp ugt i64 %4, 4999
%6 = add i64 %0, 10000
%7 = select i1 %5, i64 %6, i64 %0
%8 = trunc i64 %7 to i32
%9 = udiv i32 %8, 1000000
%10 = zext i32 %9 to i64
%11 = urem i64 %7, 1000000
%12 = trunc i64 %11 to i32
%13 = udiv i32 %12, 10000
%14 = tail call i8* (i8*, i64, ...) @xasprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 %10, i32 %13)
br label %45
15: ; preds = %1
%16 = icmp ugt i64 %0, 100000
br i1 %16, label %17, label %27
17: ; preds = %15
%18 = trunc i64 %0 to i32
%19 = urem i32 %18, 1000
%20 = icmp ugt i32 %19, 499
%21 = add nuw nsw i64 %0, 1000
%22 = select i1 %20, i64 %21, i64 %0
%23 = trunc i64 %22 to i32
%24 = udiv i32 %23, 1000
%25 = zext i32 %24 to i64
%26 = tail call i8* (i8*, i64, ...) @xasprintf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %25)
br label %45
27: ; preds = %15
%28 = icmp ugt i64 %0, 1000
br i1 %28, label %29, label %43
29: ; preds = %27
%30 = trunc i64 %0 to i32
%31 = urem i32 %30, 100
%32 = icmp ugt i32 %31, 49
%33 = add nuw nsw i64 %0, 100
%34 = select i1 %32, i64 %33, i64 %0
%35 = trunc i64 %34 to i32
%36 = udiv i32 %35, 1000
%37 = zext i32 %36 to i64
%38 = urem i32 %35, 1000
%39 = trunc i32 %38 to i16
%40 = udiv i16 %39, 100
%41 = zext i16 %40 to i32
%42 = tail call i8* (i8*, i64, ...) @xasprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %37, i32 %41)
br label %45
43: ; preds = %27
%44 = tail call i8* (i8*, i64, ...) @xasprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %0)
br label %45
45: ; preds = %43, %29, %17, %3
%46 = phi i8* [ %14, %3 ], [ %26, %17 ], [ %42, %29 ], [ %44, %43 ]
ret i8* %46
}