char *convertTOchar(int val) {
  int ones, tens, hundreds, thousands;
  if (val > 999) {
    static char str[5];
    ones = val % 10;
    tens = (val % 100) / 10;
    hundreds = (val % 1000) / 100;
    thousands = val / 1000;
    str[0] = thousands + 48;
    str[1] = hundreds + 48;
    str[2] = tens + 48;
    str[3] = ones + 48;
    str[4] = '\0';
    return str;
  } else if (val > 99 && val <= 999) {
    static char str[4];
    ones = val % 10;
    tens = (val % 100) / 10;
    hundreds = val / 100;
    str[0] = hundreds + 48;
    str[1] = tens + 48;
    str[2] = ones + 48;
    str[3] = '\0';
    return str;
  } else {
    static char str[3];
    ones = val % 10;
    tens = val / 10;
    str[0] = tens + 48;
    str[1] = ones + 48;
    str[2] = '\0';
    return str;
  }
}


x86 assembly (clang, -O0):
<convertTOchar>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0xc(%rbp)
cmpl   $0x3e7,-0xc(%rbp)
jle    11a7 <convertTOchar+0xa7>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2eb5(%rip)
mov    -0x18(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2eaa(%rip)
mov    -0x14(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2e9f(%rip)
mov    -0x10(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2e94(%rip)
movb   $0x0,0x2e8e(%rip)
lea    0x2e83(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1273 <convertTOchar+0x173>
cmpl   $0x63,-0xc(%rbp)
jle    122d <convertTOchar+0x12d>
cmpl   $0x3e7,-0xc(%rbp)
jg     122d <convertTOchar+0x12d>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2e28(%rip)
mov    -0x14(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2e1d(%rip)
mov    -0x10(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2e12(%rip)
movb   $0x0,0x2e0c(%rip)
lea    0x2e02(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1273 <convertTOchar+0x173>
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2dd5(%rip)
mov    -0x10(%rbp),%eax
add    $0x30,%eax
mov    %al,0x2dca(%rip)
movb   $0x0,0x2dc4(%rip)
lea    0x2dbb(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<convertTOchar>:
cmp    $0x3e8,%edi
jl     119b <convertTOchar+0x9b>
mov    %edi,%eax
mov    $0xcccccccd,%ecx
imul   %rax,%rcx
shr    $0x23,%rcx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%edx
mov    %edi,%ecx
sub    %edx,%ecx
imul   $0x51eb851f,%rax,%rdx
shr    $0x25,%rdx
imul   $0x64,%edx,%edx
mov    %edi,%esi
sub    %edx,%esi
imul   $0xcd,%esi,%edx
movzwl %dx,%edx
shr    $0xb,%edx
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%esi
sub    %esi,%edi
movzwl %di,%esi
shr    $0x2,%esi
imul   $0x147b,%esi,%esi
shr    $0x11,%esi
add    $0x30,%al
mov    %al,0x2eb5(%rip)
lea    0x2eae(%rip),%rax
or     $0x30,%sil
mov    %sil,0x2ea4(%rip)
or     $0x30,%dl
mov    %dl,0x2e9c(%rip)
or     $0x30,%cl
mov    %cl,0x2e94(%rip)
lea    0x2e8e(%rip),%rcx
movb   $0x0,(%rcx)
ret
cmp    $0x64,%edi
jl     1200 <convertTOchar+0x100>
movzwl %di,%eax
imul   $0xcccd,%eax,%ecx
shr    $0x12,%ecx
and    $0xfffffffe,%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %edi,%edx
sub    %ecx,%edx
shr    $0x2,%eax
imul   $0x147b,%eax,%eax
shr    $0x11,%eax
imul   $0x64,%eax,%ecx
sub    %ecx,%edi
movzbl %dil,%ecx
imul   $0xcd,%ecx,%ecx
shr    $0xb,%ecx
add    $0x30,%al
mov    %al,0x2e4a(%rip)
lea    0x2e43(%rip),%rax
or     $0x30,%cl
mov    %cl,0x2e3b(%rip)
or     $0x30,%dl
mov    %dl,0x2e33(%rip)
lea    0x2e2d(%rip),%rcx
movb   $0x0,(%rcx)
ret
movslq %edi,%rax
imul   $0x66666667,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
lea    (%rax,%rax,1),%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%edi
add    $0x30,%al
mov    %al,0x2e03(%rip)
lea    0x2dfc(%rip),%rax
add    $0x30,%dil
mov    %dil,0x2df2(%rip)
lea    0x2dec(%rip),%rcx
movb   $0x0,(%rcx)
ret

LLVM IR (-O0):
@convertTOchar.str = internal global [5 x i8] zeroinitializer
@convertTOchar.str.1 = internal global [4 x i8] zeroinitializer
@convertTOchar.str.2 = internal global [3 x i8] zeroinitializer
define i8* @convertTOchar(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 999
br i1 %9, label %10, label %33
10: ; preds = %1
%11 = load i32, i32* %3
%12 = srem i32 %11, 10
store i32 %12, i32* %4
%13 = load i32, i32* %3
%14 = srem i32 %13, 100
%15 = sdiv i32 %14, 10
store i32 %15, i32* %5
%16 = load i32, i32* %3
%17 = srem i32 %16, 1000
%18 = sdiv i32 %17, 100
store i32 %18, i32* %6
%19 = load i32, i32* %3
%20 = sdiv i32 %19, 1000
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 48
%23 = trunc i32 %22 to i8
store i8 %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 0)
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 48
%26 = trunc i32 %25 to i8
store i8 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 1)
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 48
%29 = trunc i32 %28 to i8
store i8 %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 2)
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 48
%32 = trunc i32 %31 to i8
store i8 %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 3)
store i8 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 4)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 0), i8** %2
br label %67
33: ; preds = %1
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 99
br i1 %35, label %36, label %56
36: ; preds = %33
%37 = load i32, i32* %3
%38 = icmp sle i32 %37, 999
br i1 %38, label %39, label %56
39: ; preds = %36
%40 = load i32, i32* %3
%41 = srem i32 %40, 10
store i32 %41, i32* %4
%42 = load i32, i32* %3
%43 = srem i32 %42, 100
%44 = sdiv i32 %43, 10
store i32 %44, i32* %5
%45 = load i32, i32* %3
%46 = sdiv i32 %45, 100
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 48
%49 = trunc i32 %48 to i8
store i8 %49, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 0)
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 48
%52 = trunc i32 %51 to i8
store i8 %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 1)
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 48
%55 = trunc i32 %54 to i8
store i8 %55, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 2)
store i8 0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 3)
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 0), i8** %2
br label %67
56: ; preds = %36, %33
%57 = load i32, i32* %3
%58 = srem i32 %57, 10
store i32 %58, i32* %4
%59 = load i32, i32* %3
%60 = sdiv i32 %59, 10
store i32 %60, i32* %5
%61 = load i32, i32* %5
%62 = add nsw i32 %61, 48
%63 = trunc i32 %62 to i8
store i8 %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @convertTOchar.str.2, i64 0, i64 0)
%64 = load i32, i32* %4
%65 = add nsw i32 %64, 48
%66 = trunc i32 %65 to i8
store i8 %66, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @convertTOchar.str.2, i64 0, i64 1)
store i8 0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @convertTOchar.str.2, i64 0, i64 2)
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @convertTOchar.str.2, i64 0, i64 0), i8** %2
br label %67
67: ; preds = %56, %39, %10
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
@convertTOchar.str = internal global [5 x i8] zeroinitializer
@convertTOchar.str.1 = internal global [4 x i8] zeroinitializer
@convertTOchar.str.2 = internal global [3 x i8] zeroinitializer
define i8* @convertTOchar(i32 %0) {
%2 = icmp sgt i32 %0, 999
br i1 %2, label %3, label %19
3: ; preds = %1
%4 = urem i32 %0, 10
%5 = urem i32 %0, 100
%6 = trunc i32 %5 to i8
%7 = udiv i8 %6, 10
%8 = urem i32 %0, 1000
%9 = trunc i32 %8 to i16
%10 = udiv i16 %9, 100
%11 = udiv i32 %0, 1000
%12 = trunc i32 %11 to i8
%13 = add i8 %12, 48
store i8 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 0)
%14 = trunc i16 %10 to i8
%15 = or i8 %14, 48
store i8 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 1)
%16 = or i8 %7, 48
store i8 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 2)
%17 = trunc i32 %4 to i8
%18 = or i8 %17, 48
store i8 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 3)
br label %40
19: ; preds = %1
%20 = icmp sgt i32 %0, 99
br i1 %20, label %21, label %33
21: ; preds = %19
%22 = trunc i32 %0 to i16
%23 = urem i16 %22, 10
%24 = urem i16 %22, 100
%25 = trunc i16 %24 to i8
%26 = udiv i8 %25, 10
%27 = udiv i16 %22, 100
%28 = trunc i16 %27 to i8
%29 = add nuw nsw i8 %28, 48
store i8 %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 0)
%30 = or i8 %26, 48
store i8 %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 1)
%31 = trunc i16 %23 to i8
%32 = or i8 %31, 48
store i8 %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 2)
br label %40
33: ; preds = %19
%34 = srem i32 %0, 10
%35 = sdiv i32 %0, 10
%36 = trunc i32 %35 to i8
%37 = add i8 %36, 48
store i8 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @convertTOchar.str.2, i64 0, i64 0)
%38 = trunc i32 %34 to i8
%39 = add nsw i8 %38, 48
store i8 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @convertTOchar.str.2, i64 0, i64 1)
br label %40
40: ; preds = %33, %21, %3
%41 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @convertTOchar.str.2, i64 0, i64 2), %33 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 3), %21 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 4), %3 ]
%42 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @convertTOchar.str.2, i64 0, i64 0), %33 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @convertTOchar.str.1, i64 0, i64 0), %21 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @convertTOchar.str, i64 0, i64 0), %3 ]
store i8 0, i8* %41
ret i8* %42
}