#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 *malloc(scalar_t__);
scalar_t__ snprintf(int /*<<< orphan*/ *, int /*<<< orphan*/, char *, char *,
                    int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
char *midi_ntos(int note_num) {
  if (note_num < 0 || note_num > 127)
    return NULL;
  static char *note_data[] = {"C",  "C#", "D",  "D#", "E",  "F",
                              "F#", "G",  "G#", "A",  "A#", "B"};
  int oct = (note_num / 12) - 2;
  int note = note_num % 12;
  char *ret = malloc(snprintf(NULL, 0, "%s%i", note_data[note], oct) + 1);
  sprintf(ret, "%s%i", note_data[note], oct);
  return ret;
}


x86 assembly (clang, -O0):
<midi_ntos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     114f <midi_ntos+0x1f>
cmpl   $0x7f,-0xc(%rbp)
jle    115c <midi_ntos+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11da <midi_ntos+0xaa>
mov    -0xc(%rbp),%eax
mov    $0xc,%ecx
cltd
idiv   %ecx
sub    $0x2,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xc,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
movslq -0x14(%rbp),%rcx
lea    0x2eba(%rip),%rax
mov    (%rax,%rcx,8),%rcx
mov    -0x10(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xe82(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rcx
lea    0x2e81(%rip),%rax
mov    (%rax,%rcx,8),%rdx
mov    -0x10(%rbp),%ecx
lea    0xe50(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<midi_ntos>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x7f,%edi
jbe    1140 <midi_ntos+0x10>
xor    %ebx,%ebx
jmp    119d <midi_ntos+0x6d>
movzbl %dil,%eax
imul   $0xab,%eax,%ecx
shr    $0xb,%ecx
lea    -0x2(%rcx),%r14d
shl    $0x2,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %cl,%al
movzbl %al,%eax
lea    0x2c5d(%rip),%rcx
mov    (%rcx,%rax,8),%rbp
lea    0xeaf(%rip),%r15
xor    %edi,%edi
xor    %esi,%esi
mov    %r15,%rdx
mov    %rbp,%rcx
mov    %r14d,%r8d
call   1030 <snprintf@plt>
lea    0x1(%rax),%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %r14d,%ecx
call   1050 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@midi_ntos.note_data = internal global [12 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i32 0, i32 0)]
@.str = [2 x i8] c"C\00"
@.str.1 = [3 x i8] c"C#\00"
@.str.2 = [2 x i8] c"D\00"
@.str.3 = [3 x i8] c"D#\00"
@.str.4 = [2 x i8] c"E\00"
@.str.5 = [2 x i8] c"F\00"
@.str.6 = [3 x i8] c"F#\00"
@.str.7 = [2 x i8] c"G\00"
@.str.8 = [3 x i8] c"G#\00"
@.str.9 = [2 x i8] c"A\00"
@.str.10 = [3 x i8] c"A#\00"
@.str.11 = [2 x i8] c"B\00"
@.str.12 = [5 x i8] c"%s%i\00"
define i8* @midi_ntos(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 127
br i1 %11, label %12, label %13
12: ; preds = %9, %1
store i8* null, i8** %2
br label %35
13: ; preds = %9
%14 = load i32, i32* %3
%15 = sdiv i32 %14, 12
%16 = sub nsw i32 %15, 2
store i32 %16, i32* %4
%17 = load i32, i32* %3
%18 = srem i32 %17, 12
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [12 x i8*], [12 x i8*]* @midi_ntos.note_data, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = load i32, i32* %4
%24 = call i64 @snprintf(i32* null, i32 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i8* %22, i32 %23)
%25 = add nsw i64 %24, 1
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [12 x i8*], [12 x i8*]* @midi_ntos.note_data, i64 0, i64 %29
%31 = load i8*, i8** %30
%32 = load i32, i32* %4
%33 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i8* %31, i32 %32)
%34 = load i8*, i8** %6
store i8* %34, i8** %2
br label %35
35: ; preds = %13, %12
%36 = load i8*, i8** %2
ret i8* %36
}

LLVM IR (-O2):
@midi_ntos.note_data = internal unnamed_addr constant [12 x i8*] [i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i32 0, i32 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i32 0, i32 0)]
@.str = [2 x i8] c"C\00"
@.str.1 = [3 x i8] c"C#\00"
@.str.2 = [2 x i8] c"D\00"
@.str.3 = [3 x i8] c"D#\00"
@.str.4 = [2 x i8] c"E\00"
@.str.5 = [2 x i8] c"F\00"
@.str.6 = [3 x i8] c"F#\00"
@.str.7 = [2 x i8] c"G\00"
@.str.8 = [3 x i8] c"G#\00"
@.str.9 = [2 x i8] c"A\00"
@.str.10 = [3 x i8] c"A#\00"
@.str.11 = [2 x i8] c"B\00"
@.str.12 = [5 x i8] c"%s%i\00"
define noalias i8* @midi_ntos(i32 %0) {
%2 = icmp ugt i32 %0, 127
br i1 %2, label %16, label %3
3: ; preds = %1
%4 = trunc i32 %0 to i8
%5 = udiv i8 %4, 12
%6 = zext i8 %5 to i32
%7 = add nsw i32 %6, -2
%8 = urem i8 %4, 12
%9 = zext i8 %8 to i64
%10 = getelementptr inbounds [12 x i8*], [12 x i8*]* @midi_ntos.note_data, i64 0, i64 %9
%11 = load i8*, i8** %10
%12 = tail call i64 @snprintf(i32* null, i32 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i8* %11, i32 %7)
%13 = add nsw i64 %12, 1
%14 = tail call i8* @malloc(i64 %13)
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i8* %11, i32 %7)
br label %16
16: ; preds = %1, %3
%17 = phi i8* [ %14, %3 ], [ null, %1 ]
ret i8* %17
}