#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 */
int /*<<< orphan*/ sprintf(char *, char *, int);
char *xmalloc(int);
char *itods(int i) {
  char *ds;
  int p = (i < 0) ? -i : i;
  int magnitude = 0;
  do {
    p /= 10;
    magnitude += 1;
  } while (p > 0);
  ds = xmalloc((magnitude + 1) * sizeof(char));
  sprintf(ds, "%d", i);
  return ds;
}


x86 assembly (clang, -O0):
<itods>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1142 <itods+0x22>
xor    %eax,%eax
sub    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1148 <itods+0x28>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1155 <itods+0x35>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe65(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<itods>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    %edi,%eax
neg    %eax
cmovs  %edi,%eax
mov    $0x1,%edi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %eax,%rcx
imul   $0x66666667,%rcx,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rax
add    %edx,%eax
add    $0x1,%edi
cmp    $0x9,%ecx
jg     1140 <itods+0x20>
call   1030 <xmalloc@plt>
mov    %rax,%rbx
lea    0xe92(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1040 <sprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
define i8* @itods(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* %2
%10 = sub nsw i32 0, %9
br label %13
11: ; preds = %1
%12 = load i32, i32* %2
br label %13
13: ; preds = %11, %8
%14 = phi i32 [ %10, %8 ], [ %12, %11 ]
store i32 %14, i32* %4
store i32 0, i32* %5
br label %15
15: ; preds = %20, %13
%16 = load i32, i32* %4
%17 = sdiv i32 %16, 10
store i32 %17, i32* %4
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
store i32 %19, i32* %5
br label %20
20: ; preds = %15
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 0
br i1 %22, label %15, label %23
23: ; preds = %20
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 1
%28 = trunc i64 %27 to i32
%29 = call i8* @xmalloc(i32 %28)
store i8* %29, i8** %3
%30 = load i8*, i8** %3
%31 = load i32, i32* %2
%32 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define i8* @itods(i32 %0) {
%2 = tail call i32 @llvm.abs.i32(i32 %0, i1 true)
br label %3
3: ; preds = %3, %1
%4 = phi i32 [ %2, %1 ], [ %6, %3 ]
%5 = phi i32 [ 0, %1 ], [ %7, %3 ]
%6 = sdiv i32 %4, 10
%7 = add nuw nsw i32 %5, 1
%8 = icmp sgt i32 %4, 9
br i1 %8, label %3, label %9
9: ; preds = %3
%10 = add nuw nsw i32 %5, 2
%11 = tail call i8* @xmalloc(i32 %10)
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
ret i8* %11
}