#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*/ snprintf(char *, size_t, char *, int, ...);
void audio_rate_format(char *buf, size_t buflen, int rate) {
  int k, r;
  k = rate / 1000;
  r = rate % 1000;
  if (!k) {
    snprintf(buf, buflen, "%d Hz", r);
  } else if (!r) {
    snprintf(buf, buflen, "%d kHz", k);
  } else {
    if (!(r % 10)) {
      r /= 10;
      if (!(r % 10))
        r /= 10;
    }
    snprintf(buf, buflen, "%d.%d kHz", k, r);
  }
}


x86 assembly (clang, -O0):
<audio_rate_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1167 <audio_rate_format+0x57>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
lea    0xea5(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11fa <audio_rate_format+0xea>
cmpl   $0x0,-0x1c(%rbp)
jne    118f <audio_rate_format+0x7f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%ecx
lea    0xe86(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11f5 <audio_rate_format+0xe5>
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11d8 <audio_rate_format+0xc8>
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11d3 <audio_rate_format+0xc3>
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
jmp    11d8 <audio_rate_format+0xc8>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    0xe18(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    11fa <audio_rate_format+0xea>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<audio_rate_format>:
movslq %edx,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0x3e8,%ecx,%edx
mov    %eax,%r8d
sub    %edx,%r8d
add    $0x3e7,%eax
cmp    $0x7ce,%eax
ja     1150 <audio_rate_format+0x40>
lea    0xeba(%rip),%rdx
mov    %r8d,%ecx
xor    %eax,%eax
jmp    1030 <snprintf@plt>
test   %r8d,%r8d
je     1194 <audio_rate_format+0x84>
imul   $0xffffcccd,%r8d,%eax
add    $0x1998,%eax
ror    %ax
movzwl %ax,%eax
cmp    $0x1998,%eax
ja     11b6 <audio_rate_format+0xa6>
movswl %r8w,%r8d
imul   $0x6667,%r8d,%edx
mov    %edx,%eax
shr    $0x1f,%eax
sar    $0x12,%edx
add    %eax,%edx
imul   $0xffffffcd,%edx,%eax
add    $0x18,%al
ror    %al
cmp    $0x18,%al
jbe    11a2 <audio_rate_format+0x92>
movswl %dx,%r8d
jmp    11b6 <audio_rate_format+0xa6>
lea    0xe6e(%rip),%rdx
xor    %eax,%eax
jmp    1030 <snprintf@plt>
imul   $0x147b,%r8d,%r8d
mov    %r8d,%eax
shr    $0x1f,%eax
sar    $0x13,%r8d
add    %eax,%r8d
lea    0xe49(%rip),%rdx
xor    %eax,%eax
jmp    1030 <snprintf@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%d Hz\00"
@.str.1 = [7 x i8] c"%d kHz\00"
@.str.2 = [10 x i8] c"%d.%d kHz\00"
define void @audio_rate_format(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = sdiv i32 %9, 1000
store i32 %10, i32* %7
%11 = load i32, i32* %6
%12 = srem i32 %11, 1000
store i32 %12, i32* %8
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = load i32, i32* %8
%19 = call i32 (i8*, i64, i8*, i32, ...) @snprintf(i8* %16, i64 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %49
20: ; preds = %3
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = load i32, i32* %7
%27 = call i32 (i8*, i64, i8*, i32, ...) @snprintf(i8* %24, i64 %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %48
28: ; preds = %20
%29 = load i32, i32* %8
%30 = srem i32 %29, 10
%31 = icmp ne i32 %30, 0
br i1 %31, label %42, label %32
32: ; preds = %28
%33 = load i32, i32* %8
%34 = sdiv i32 %33, 10
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = srem i32 %35, 10
%37 = icmp ne i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %32
%39 = load i32, i32* %8
%40 = sdiv i32 %39, 10
store i32 %40, i32* %8
br label %41
41: ; preds = %38, %32
br label %42
42: ; preds = %41, %28
%43 = load i8*, i8** %4
%44 = load i64, i64* %5
%45 = load i32, i32* %7
%46 = load i32, i32* %8
%47 = call i32 (i8*, i64, i8*, i32, ...) @snprintf(i8* %43, i64 %44, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %45, i32 %46)
br label %48
48: ; preds = %42, %23
br label %49
49: ; preds = %48, %15
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%d Hz\00"
@.str.1 = [7 x i8] c"%d kHz\00"
@.str.2 = [10 x i8] c"%d.%d kHz\00"
define void @audio_rate_format(i8* %0, i64 %1, i32 %2) {
%4 = sdiv i32 %2, 1000
%5 = srem i32 %2, 1000
%6 = add i32 %2, 999
%7 = icmp ult i32 %6, 1999
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (i8*, i64, i8*, i32, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %30
10: ; preds = %3
%11 = icmp eq i32 %5, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 (i8*, i64, i8*, i32, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %30
14: ; preds = %10
%15 = trunc i32 %5 to i16
%16 = srem i16 %15, 10
%17 = sdiv i16 %15, 10
%18 = icmp eq i16 %16, 0
br i1 %18, label %19, label %27
19: ; preds = %14
%20 = sext i16 %17 to i32
%21 = trunc i16 %17 to i8
%22 = srem i8 %21, 10
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = sdiv i16 %15, 100
%26 = sext i16 %25 to i32
br label %27
27: ; preds = %19, %24, %14
%28 = phi i32 [ %5, %14 ], [ %20, %19 ], [ %26, %24 ]
%29 = tail call i32 (i8*, i64, i8*, i32, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 %28)
br label %30
30: ; preds = %12, %27, %8
ret void
}