#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 *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void fcd_append_freq(char *buffer, double fq) {
  int j, freq;
  char *src, *dst, *tail = " Hz", stmp[30];
  unsigned u, spaceleft;
  freq = (int)fq;
  sprintf(stmp, "%d", freq);
  spaceleft = 30 - strlen(buffer) - 1;
  if ((strlen(stmp) + strlen(" Hz")) > spaceleft) {
    freq = freq / 1000;
    sprintf(stmp, "%d", freq);
    tail = " kHz";
  }
  for (u = strlen(buffer); u < (30 - strlen(tail) - 1); ++u)
    buffer[u] = ' ';
  strcpy(&buffer[30 - strlen(tail) - 1], tail);
  src = &stmp[strlen(stmp)];
  dst = &buffer[30 - strlen(tail) - 1];
  for (u = strlen(stmp), j = 0; u; --u) {
    *--dst = *--src;
    if ((++j % 3) == 0 && u > 1)
      *--dst = ',';
  }
}


x86 assembly (clang, -O0):
<fcd_append_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
lea    0xeb8(%rip),%rax
mov    %rax,-0x30(%rbp)
cvttsd2si -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xea2(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    $0x1e,%eax
sub    %ecx,%eax
sub    $0x1,%eax
mov    %eax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x5c(%rbp)
lea    0xe6e(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x5c(%rbp),%eax
add    %ecx,%eax
cmp    -0x58(%rbp),%eax
jbe    11d3 <fcd_append_freq+0xa3>
mov    -0x18(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xe41(%rip),%rsi
call   1050 <sprintf@plt>
lea    0xe38(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edx
mov    -0x60(%rbp),%eax
mov    $0x1e,%ecx
sub    %edx,%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    121e <fcd_append_freq+0xee>
mov    -0x8(%rbp),%rax
mov    -0x54(%rbp),%ecx
movb   $0x20,(%rax,%rcx,1)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11df <fcd_append_freq+0xaf>
mov    -0x8(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%ecx
mov    $0x1e,%eax
sub    %ecx,%eax
sub    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
lea    -0x50(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edx
mov    -0x68(%rbp),%rax
mov    $0x1e,%ecx
sub    %edx,%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x54(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     131a <fcd_append_freq+0x1ea>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
mov    -0x1(%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1307 <fcd_append_freq+0x1d7>
cmpl   $0x1,-0x54(%rbp)
jbe    1307 <fcd_append_freq+0x1d7>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x2c,-0x1(%rax)
jmp    130c <fcd_append_freq+0x1dc>
mov    -0x54(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x54(%rbp)
jmp    12a2 <fcd_append_freq+0x172>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcd_append_freq>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r13
cvttsd2si %xmm0,%r15d
lea    0xeb7(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %r15d,%edx
call   1050 <sprintf@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    $0x1d,%r12d
mov    $0x1d,%ebx
sub    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0xe7f(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
cmp    %ebx,%eax
jbe    11bc <fcd_append_freq+0x8c>
movslq %r15d,%rax
imul   $0x10624dd3,%rax,%rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x26,%rdx
add    %eax,%edx
lea    0xe57(%rip),%rsi
mov    %rsp,%rdi
call   1050 <sprintf@plt>
lea    0xe4b(%rip),%r14
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
sub    %eax,%r12d
cmp    %r12d,%ebp
jae    11fe <fcd_append_freq+0xce>
mov    %ebp,%ebx
nopl   0x0(%rax,%rax,1)
movb   $0x20,0x0(%r13,%rbx,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    $0x1d,%ecx
sub    %eax,%ecx
cmp    %rcx,%rbx
jb     11e0 <fcd_append_freq+0xb0>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    $0x1d,%ebp
mov    $0x1d,%ecx
sub    %eax,%ecx
movslq %ecx,%rdi
add    %r13,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rsp,%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     12c2 <fcd_append_freq+0x192>
sub    %r14d,%ebp
movslq %ebp,%rcx
add    %rcx,%r13
movslq %r12d,%rdx
neg    %eax
lea    0x1(%rax),%r9
lea    (%rdx,%r15,1),%r10
add    $0xffffffffffffffff,%r10
mov    $0xffffffff,%esi
mov    $0x1,%edi
xor    %ebp,%ebp
mov    $0xaaaaaaab,%r8d
jmp    1291 <fcd_append_freq+0x161>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%esi
add    $0x1,%edi
add    $0xffffffffffffffff,%rbp
mov    %rbx,%r13
cmp    %ebp,%eax
je     12c2 <fcd_append_freq+0x192>
mov    %edi,%ebx
imul   %r8,%rbx
shr    $0x21,%rbx
lea    (%rbx,%rbx,2),%ecx
movzbl (%r10,%rbp,1),%edx
lea    -0x1(%r13),%rbx
mov    %dl,-0x1(%r13)
add    %esi,%ecx
jne    1280 <fcd_append_freq+0x150>
cmp    %ebp,%r9d
je     1280 <fcd_append_freq+0x150>
movb   $0x2c,-0x2(%r13)
add    $0xfffffffffffffffe,%r13
mov    %r13,%rbx
jmp    1280 <fcd_append_freq+0x150>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c" Hz\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [5 x i8] c" kHz\00"
define void @fcd_append_freq(i8* %0, double %1) {
%3 = alloca i8*
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [30 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store double %1, double* %4
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %9
%13 = load double, double* %4
%14 = fptosi double %13 to i32
store i32 %14, i32* %6
%15 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%16 = load i32, i32* %6
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 30, %19
%21 = sub nsw i32 %20, 1
store i32 %21, i32* %12
%22 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%25 = add nsw i32 %23, %24
%26 = load i32, i32* %12
%27 = icmp ugt i32 %25, %26
br i1 %27, label %28, label %34
28: ; preds = %2
%29 = load i32, i32* %6
%30 = sdiv i32 %29, 1000
store i32 %30, i32* %6
%31 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%32 = load i32, i32* %6
%33 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %9
br label %34
34: ; preds = %28, %2
%35 = load i8*, i8** %3
%36 = call i32 @strlen(i8* %35)
store i32 %36, i32* %11
br label %37
37: ; preds = %49, %34
%38 = load i32, i32* %11
%39 = load i8*, i8** %9
%40 = call i32 @strlen(i8* %39)
%41 = sub nsw i32 30, %40
%42 = sub nsw i32 %41, 1
%43 = icmp ult i32 %38, %42
br i1 %43, label %44, label %52
44: ; preds = %37
%45 = load i8*, i8** %3
%46 = load i32, i32* %11
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 32, i8* %48
br label %49
49: ; preds = %44
%50 = load i32, i32* %11
%51 = add i32 %50, 1
store i32 %51, i32* %11
br label %37
52: ; preds = %37
%53 = load i8*, i8** %3
%54 = load i8*, i8** %9
%55 = call i32 @strlen(i8* %54)
%56 = sub nsw i32 30, %55
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %53, i64 %58
%60 = load i8*, i8** %9
%61 = call i32 @strcpy(i8* %59, i8* %60)
%62 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%63 = call i32 @strlen(i8* %62)
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 %64
store i8* %65, i8** %7
%66 = load i8*, i8** %3
%67 = load i8*, i8** %9
%68 = call i32 @strlen(i8* %67)
%69 = sub nsw i32 30, %68
%70 = sub nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %66, i64 %71
store i8* %72, i8** %8
%73 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%74 = call i32 @strlen(i8* %73)
store i32 %74, i32* %11
store i32 0, i32* %5
br label %75
75: ; preds = %95, %52
%76 = load i32, i32* %11
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %98
78: ; preds = %75
%79 = load i8*, i8** %7
%80 = getelementptr inbounds i8, i8* %79, i32 -1
store i8* %80, i8** %7
%81 = load i8, i8* %80
%82 = load i8*, i8** %8
%83 = getelementptr inbounds i8, i8* %82, i32 -1
store i8* %83, i8** %8
store i8 %81, i8* %83
%84 = load i32, i32* %5
%85 = add nsw i32 %84, 1
store i32 %85, i32* %5
%86 = srem i32 %85, 3
%87 = icmp eq i32 %86, 0
br i1 %87, label %88, label %94
88: ; preds = %78
%89 = load i32, i32* %11
%90 = icmp ugt i32 %89, 1
br i1 %90, label %91, label %94
91: ; preds = %88
%92 = load i8*, i8** %8
%93 = getelementptr inbounds i8, i8* %92, i32 -1
store i8* %93, i8** %8
store i8 44, i8* %93
br label %94
94: ; preds = %91, %88, %78
br label %95
95: ; preds = %94
%96 = load i32, i32* %11
%97 = add i32 %96, -1
store i32 %97, i32* %11
br label %75
98: ; preds = %75
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c" Hz\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [5 x i8] c" kHz\00"
define void @fcd_append_freq(i8* %0, double %1) {
%3 = alloca [30 x i8]
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%5 = fptosi double %1 to i32
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @strlen(i8* %0)
%8 = sub i32 29, %7
%9 = call i32 @strlen(i8* %4)
%10 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = add nsw i32 %10, %9
%12 = icmp ugt i32 %11, %8
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = sdiv i32 %5, 1000
%15 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %13, %2
%17 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %13 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %2 ]
%18 = call i32 @strlen(i8* %0)
%19 = call i32 @strlen(i8* %17)
%20 = sub i32 29, %19
%21 = icmp ult i32 %18, %20
br i1 %21, label %22, label %32
22: ; preds = %16
%23 = zext i32 %18 to i64
br label %24
24: ; preds = %22, %24
%25 = phi i64 [ %23, %22 ], [ %27, %24 ]
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 32, i8* %26
%27 = add nuw nsw i64 %25, 1
%28 = call i32 @strlen(i8* %17)
%29 = sub i32 29, %28
%30 = zext i32 %29 to i64
%31 = icmp ult i64 %27, %30
br i1 %31, label %24, label %32
32: ; preds = %24, %16
%33 = call i32 @strlen(i8* %17)
%34 = sub i32 29, %33
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
%37 = call i32 @strcpy(i8* %36, i8* %17)
%38 = call i32 @strlen(i8* %4)
%39 = call i32 @strlen(i8* %17)
%40 = call i32 @strlen(i8* %4)
%41 = icmp eq i32 %40, 0
br i1 %41, label %67, label %42
42: ; preds = %32
%43 = sub i32 29, %39
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %0, i64 %44
%46 = sext i32 %38 to i64
%47 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 %46
br label %48
48: ; preds = %42, %63
%49 = phi i32 [ %56, %63 ], [ 0, %42 ]
%50 = phi i32 [ %65, %63 ], [ %40, %42 ]
%51 = phi i8* [ %64, %63 ], [ %45, %42 ]
%52 = phi i8* [ %53, %63 ], [ %47, %42 ]
%53 = getelementptr inbounds i8, i8* %52, i64 -1
%54 = load i8, i8* %53
%55 = getelementptr inbounds i8, i8* %51, i64 -1
store i8 %54, i8* %55
%56 = add nuw nsw i32 %49, 1
%57 = urem i32 %56, 3
%58 = icmp eq i32 %57, 0
%59 = icmp ne i32 %50, 1
%60 = select i1 %58, i1 %59, i1 false
br i1 %60, label %61, label %63
61: ; preds = %48
%62 = getelementptr inbounds i8, i8* %51, i64 -2
store i8 44, i8* %62
br label %63
63: ; preds = %48, %61
%64 = phi i8* [ %62, %61 ], [ %55, %48 ]
%65 = add i32 %50, -1
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %48
67: ; preds = %63, %32
ret void
}