#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 */
typedef int ushort;

/* Variables and functions */
ushort myItoA(ushort num, char *buff) {
  ushort lngth;
  if (10 > num) {
    buff[0] = num + 0x30;
    buff[1] = 0;
    lngth = 1;
  } else if (100 > num) {
    buff[0] = num / 10 + 0x30;
    buff[1] = num % 10 + 0x30;
    buff[2] = 0;
    lngth = 2;
  } else {
    buff[0] = num / 100 + 0x30;
    buff[1] = num / 10 % 10 + 0x30;
    buff[2] = num % 10 + 0x30;
    buff[3] = 0;
    lngth = 3;
  }
  return lngth;
}


x86 assembly (clang, -O0):
<myItoA>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xa,%eax
cmp    -0x4(%rbp),%eax
jle    113b <myItoA+0x3b>
mov    -0x4(%rbp),%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
movl   $0x1,-0x14(%rbp)
jmp    11ea <myItoA+0xea>
mov    $0x64,%eax
cmp    -0x4(%rbp),%eax
jle    118a <myItoA+0x8a>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x2(%rax)
movl   $0x2,-0x14(%rbp)
jmp    11e5 <myItoA+0xe5>
mov    -0x4(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
add    $0x30,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x3(%rax)
movl   $0x3,-0x14(%rbp)
jmp    11ea <myItoA+0xea>
mov    -0x14(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<myItoA>:
cmp    $0x9,%edi
jg     111b <myItoA+0x1b>
add    $0x30,%dil
mov    %dil,(%rsi)
mov    $0x1,%eax
mov    $0x1,%ecx
movb   $0x0,(%rsi,%rcx,1)
ret
cmp    $0x63,%edi
ja     114d <myItoA+0x4d>
movzbl %dil,%eax
imul   $0xcd,%eax,%ecx
shr    $0xb,%ecx
lea    0x30(%rcx),%edx
mov    %dl,(%rsi)
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %cl,%al
or     $0x30,%al
mov    %al,0x1(%rsi)
mov    $0x2,%eax
mov    $0x2,%ecx
movb   $0x0,(%rsi,%rcx,1)
ret
mov    %edi,%eax
imul   $0x51eb851f,%rax,%rcx
shr    $0x25,%rcx
add    $0x30,%cl
mov    %cl,(%rsi)
mov    $0xcccccccd,%ecx
imul   %rcx,%rax
shr    $0x23,%rax
imul   %rax,%rcx
shr    $0x23,%rcx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %eax,%edx
sub    %ecx,%edx
or     $0x30,%dl
mov    %dl,0x1(%rsi)
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%edi
or     $0x30,%dil
mov    %dil,0x2(%rsi)
mov    $0x3,%eax
mov    $0x3,%ecx
movb   $0x0,(%rsi,%rcx,1)
ret

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

LLVM IR (-O2):
define i32 @myItoA(i32 %0, i8* nocapture writeonly %1) {
%3 = icmp slt i32 %0, 10
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = trunc i32 %0 to i8
%6 = add i8 %5, 48
store i8 %6, i8* %1
br label %29
7: ; preds = %2
%8 = icmp ult i32 %0, 100
br i1 %8, label %9, label %16
9: ; preds = %7
%10 = trunc i32 %0 to i8
%11 = udiv i8 %10, 10
%12 = add nuw nsw i8 %11, 48
store i8 %12, i8* %1
%13 = urem i8 %10, 10
%14 = or i8 %13, 48
%15 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %14, i8* %15
br label %29
16: ; preds = %7
%17 = udiv i32 %0, 100
%18 = trunc i32 %17 to i8
%19 = add i8 %18, 48
store i8 %19, i8* %1
%20 = udiv i32 %0, 10
%21 = urem i32 %20, 10
%22 = trunc i32 %21 to i8
%23 = or i8 %22, 48
%24 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %23, i8* %24
%25 = urem i32 %0, 10
%26 = trunc i32 %25 to i8
%27 = or i8 %26, 48
%28 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %27, i8* %28
br label %29
29: ; preds = %9, %16, %4
%30 = phi i64 [ 2, %9 ], [ 3, %16 ], [ 1, %4 ]
%31 = phi i32 [ 2, %9 ], [ 3, %16 ], [ 1, %4 ]
%32 = getelementptr inbounds i8, i8* %1, i64 %30
store i8 0, i8* %32
ret i32 %31
}