#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 *, long);
char *strcat(char *, char *);
size_t strlen(char *);
char *ltostr(char *buf, long number, size_t digits) {
  long i, nz;
  char bufn[256];
  i = 0;
  sprintf(bufn, "%ld", number);
  nz = digits - strlen(bufn);
  for (i = 0; i < nz; i++)
    buf[i] = 48;
  buf[i] = '\0';
  return strcat(buf, bufn);
}


x86 assembly (clang, -O0):
<ltostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe9f(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x130(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x138(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    11c1 <ltostr+0x91>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x30,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1196 <ltostr+0x66>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   1040 <strcat@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltostr>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdx,%rbx
mov    %rsi,%rdx
mov    %rdi,%r14
lea    0xea4(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
call   1060 <sprintf@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
sub    %rax,%rbx
test   %rbx,%rbx
jle    1189 <ltostr+0x49>
mov    %r14,%rdi
mov    $0x30,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
jmp    118b <ltostr+0x4b>
xor    %ebx,%ebx
movb   $0x0,(%r14,%rbx,1)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1050 <strcat@plt>
mov    %r14,%rax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"%ld\00"
define i8* @ltostr(i8* returned %0, i64 %1, i64 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %1)
%7 = call i64 @strlen(i8* %5)
%8 = sub i64 %2, %7
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %3
br label %11
11: ; preds = %10, %3
%12 = phi i64 [ 0, %3 ], [ %8, %10 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 0, i8* %13
%14 = call i8* @strcat(i8* dereferenceable(1) %0, i8* %5)
ret i8* %0
}