void set_melody_shift_txt(short k, unsigned char *txt) {
  if (k == 0) {
    txt[0] = 1;
    txt[1] = '0';
  } else if (k < 0) {
    txt[0] = 2;
    txt[1] = '-';
    txt[2] = '0' - k;
  } else {
    txt[0] = 2;
    txt[1] = '+';
    txt[2] = '0' + k;
  }
}


x86 assembly (clang, -O0):
<set_melody_shift_txt>:
push   %rbp
mov    %rsp,%rbp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    %rsi,-0x10(%rbp)
movswl -0x2(%rbp),%eax
cmp    $0x0,%eax
jne    1130 <set_melody_shift_txt+0x30>
mov    -0x10(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x30,0x1(%rax)
jmp    1189 <set_melody_shift_txt+0x89>
movswl -0x2(%rbp),%eax
cmp    $0x0,%eax
jge    1165 <set_melody_shift_txt+0x65>
mov    -0x10(%rbp),%rax
movb   $0x2,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x2d,0x1(%rax)
movswl -0x2(%rbp),%ecx
mov    $0x30,%eax
sub    %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
jmp    1184 <set_melody_shift_txt+0x84>
mov    -0x10(%rbp),%rax
movb   $0x2,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x2b,0x1(%rax)
movswl -0x2(%rbp),%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
jmp    1189 <set_melody_shift_txt+0x89>
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_melody_shift_txt>:
test   %di,%di
je     111c <set_melody_shift_txt+0x1c>
movb   $0x2,(%rsi)
js     112a <set_melody_shift_txt+0x2a>
movb   $0x2b,0x1(%rsi)
add    $0x30,%dil
mov    $0x2,%ecx
mov    %dil,(%rsi,%rcx,1)
ret
movb   $0x1,(%rsi)
mov    $0x30,%al
mov    $0x1,%ecx
mov    %al,(%rsi,%rcx,1)
ret
movb   $0x2d,0x1(%rsi)
mov    $0x30,%al
sub    %dil,%al
mov    $0x2,%ecx
mov    %al,(%rsi,%rcx,1)
ret

LLVM IR (-O0):
define void @set_melody_shift_txt(i16 signext %0, i8* %1) {
%3 = alloca i16
%4 = alloca i8*
store i16 %0, i16* %3
store i8* %1, i8** %4
%5 = load i16, i16* %3
%6 = sext i16 %5 to i32
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i8*, i8** %4
%10 = getelementptr inbounds i8, i8* %9, i64 0
store i8 1, i8* %10
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 1
store i8 48, i8* %12
br label %40
13: ; preds = %2
%14 = load i16, i16* %3
%15 = sext i16 %14 to i32
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 2, i8* %19
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8 45, i8* %21
%22 = load i16, i16* %3
%23 = sext i16 %22 to i32
%24 = sub nsw i32 48, %23
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 2
store i8 %25, i8* %27
br label %39
28: ; preds = %13
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 2, i8* %30
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 43, i8* %32
%33 = load i16, i16* %3
%34 = sext i16 %33 to i32
%35 = add nsw i32 48, %34
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 2
store i8 %36, i8* %38
br label %39
39: ; preds = %28, %17
br label %40
40: ; preds = %39, %8
ret void
}

LLVM IR (-O2):
define void @set_melody_shift_txt(i16 signext %0, i8* nocapture writeonly %1) {
%3 = trunc i16 %0 to i8
%4 = icmp eq i16 %0, 0
br i1 %4, label %5, label %6
5: ; preds = %2
store i8 1, i8* %1
br label %13
6: ; preds = %2
%7 = icmp slt i16 %0, 0
store i8 2, i8* %1
%8 = getelementptr inbounds i8, i8* %1, i64 1
br i1 %7, label %9, label %11
9: ; preds = %6
store i8 45, i8* %8
%10 = sub i8 48, %3
br label %13
11: ; preds = %6
store i8 43, i8* %8
%12 = add i8 %3, 48
br label %13
13: ; preds = %9, %11, %5
%14 = phi i64 [ 2, %9 ], [ 2, %11 ], [ 1, %5 ]
%15 = phi i8 [ %10, %9 ], [ %12, %11 ], [ 48, %5 ]
%16 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 %15, i8* %16
ret void
}