#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 /*<<< orphan*/ revString;
typedef int /*<<< orphan*/ numString;

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int strlen(char *);
char *shortToAscii(unsigned short num) {
  char *numString;
  char revString[12];
  int remainder;
  int i = 0;
  numString = malloc(sizeof(char) * 12);
  memset(numString, 0, sizeof(numString));
  memset(revString, 0, sizeof(revString));
  while (num) {
    remainder = num % 10;
    revString[i++] = (char)remainder + 48;
    num = num / 10;
  }
  for (i = 0; i < strlen(revString); i++) {
    numString[i] = revString[strlen(revString) - (i + 1)];
  }
  return (numString);
}


x86 assembly (clang, -O0):
<shortToAscii>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x1c(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
cmpw   $0x0,-0x2(%rbp)
je     11bf <shortToAscii+0x8f>
movzwl -0x2(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0x20(%rbp),%eax
movsbl %al,%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x24(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
cltq
mov    %cl,-0x1c(%rbp,%rax,1)
movzwl -0x2(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %ax,-0x2(%rbp)
jmp    1174 <shortToAscii+0x44>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    1212 <shortToAscii+0xe2>
lea    -0x1c(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
sub    %ecx,%eax
cltq
mov    -0x1c(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c6 <shortToAscii+0x96>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shortToAscii>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    $0xc,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
test   %ebp,%ebp
je     1197 <shortToAscii+0x67>
movzwl %bp,%eax
imul   $0xcccd,%eax,%ecx
shr    $0x13,%ecx
lea    (%rcx,%rcx,1),%edx
lea    (%rdx,%rdx,4),%edx
sub    %edx,%ebp
or     $0x30,%bpl
mov    %bpl,(%rbx)
add    $0x1,%rbx
mov    %ecx,%ebp
cmp    $0xa,%ax
jae    1170 <shortToAscii+0x40>
lea    0xc(%rsp),%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11ea <shortToAscii+0xba>
lea    0xb(%rsp),%rbp
xor    %ebx,%ebx
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
movzbl 0x0(%rbp,%rax,1),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
add    $0xffffffffffffffff,%rbp
cmp    %rax,%rbx
jl     11c0 <shortToAscii+0x90>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @shortToAscii(i16 zeroext %0) {
%2 = alloca [12 x i8]
%3 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%4 = tail call dereferenceable_or_null(12) i8* @malloc(i32 12)
%5 = tail call i32 @memset(i8* %4, i32 0, i32 8)
%6 = call i32 @memset(i8* %3, i32 0, i32 12)
%7 = icmp eq i16 %0, 0
br i1 %7, label %8, label %11
8: ; preds = %11, %1
%9 = call i32 @strlen(i8* %3)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %21, label %34
11: ; preds = %1, %11
%12 = phi i64 [ %17, %11 ], [ 0, %1 ]
%13 = phi i16 [ %19, %11 ], [ %0, %1 ]
%14 = urem i16 %13, 10
%15 = trunc i16 %14 to i8
%16 = or i8 %15, 48
%17 = add nuw i64 %12, 1
%18 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 %12
store i8 %16, i8* %18
%19 = udiv i16 %13, 10
%20 = icmp ult i16 %13, 10
br i1 %20, label %8, label %11
21: ; preds = %8, %21
%22 = phi i64 [ %24, %21 ], [ 0, %8 ]
%23 = call i32 @strlen(i8* %3)
%24 = add nuw nsw i64 %22, 1
%25 = trunc i64 %24 to i32
%26 = sub nsw i32 %23, %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = getelementptr inbounds i8, i8* %4, i64 %22
store i8 %29, i8* %30
%31 = call i32 @strlen(i8* %3)
%32 = sext i32 %31 to i64
%33 = icmp slt i64 %24, %32
br i1 %33, label %21, label %34
34: ; preds = %21, %8
ret i8* %4
}