char *four_digits(pb, i)
register char *pb;
{
  i %= 10000;
  *pb++ = (i / 1000) + '0';
  i %= 1000;
  *pb++ = (i / 100) + '0';
  i %= 100;
  *pb++ = (i / 10) + '0';
  *pb++ = (i % 10) + '0';
  return ++pb;
}


x86 assembly (clang, -O0):
<four_digits>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2710,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
add    $0x30,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
add    $0x30,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
pop    %rbp
ret

x86 assembly (clang, -O2):
<four_digits>:
movslq %esi,%rax
imul   $0x68db8bad,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x2c,%rcx
add    %edx,%ecx
imul   $0x2710,%ecx,%ecx
sub    %ecx,%eax
movswl %ax,%ecx
imul   $0xffff8313,%ecx,%ecx
shr    $0x10,%ecx
add    %eax,%ecx
movzwl %cx,%ecx
movswl %cx,%edx
shr    $0xf,%ecx
sar    $0x9,%edx
add    %ecx,%edx
lea    0x30(%rdx),%ecx
mov    %cl,(%rdi)
imul   $0x3e8,%edx,%ecx
sub    %ecx,%eax
movswl %ax,%ecx
imul   $0x147b,%ecx,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x13,%ecx
add    %edx,%ecx
lea    0x30(%rcx),%edx
mov    %dl,0x1(%rdi)
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
movsbl %al,%ecx
imul   $0x67,%ecx,%ecx
movzwl %cx,%ecx
movswl %cx,%edx
shr    $0xf,%ecx
shr    $0xa,%edx
add    %cl,%dl
lea    0x30(%rdx),%ecx
mov    %cl,0x2(%rdi)
movzbl %dl,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %cl,%al
add    $0x30,%al
mov    %al,0x3(%rdi)
lea    0x5(%rdi),%rax
ret

LLVM IR (-O0):
define i8* @four_digits(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%5 = load i32, i32* %4
%6 = srem i32 %5, 10000
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = sdiv i32 %7, 1000
%9 = add nsw i32 %8, 48
%10 = trunc i32 %9 to i8
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %3
store i8 %10, i8* %11
%13 = load i32, i32* %4
%14 = srem i32 %13, 1000
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = sdiv i32 %15, 100
%17 = add nsw i32 %16, 48
%18 = trunc i32 %17 to i8
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %3
store i8 %18, i8* %19
%21 = load i32, i32* %4
%22 = srem i32 %21, 100
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = sdiv i32 %23, 10
%25 = add nsw i32 %24, 48
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %3
store i8 %26, i8* %27
%29 = load i32, i32* %4
%30 = srem i32 %29, 10
%31 = add nsw i32 %30, 48
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %3
store i8 %32, i8* %33
%35 = load i8*, i8** %3
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %3
ret i8* %36
}

LLVM IR (-O2):
define i8* @four_digits(i8* writeonly %0, i32 %1) {
%3 = srem i32 %1, 10000
%4 = trunc i32 %3 to i16
%5 = sdiv i16 %4, 1000
%6 = trunc i16 %5 to i8
%7 = add nsw i8 %6, 48
%8 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %7, i8* %0
%9 = srem i16 %4, 1000
%10 = sdiv i16 %9, 100
%11 = trunc i16 %10 to i8
%12 = add nsw i8 %11, 48
%13 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %12, i8* %8
%14 = srem i16 %9, 100
%15 = trunc i16 %14 to i8
%16 = sdiv i8 %15, 10
%17 = add nsw i8 %16, 48
%18 = getelementptr inbounds i8, i8* %0, i64 3
store i8 %17, i8* %13
%19 = srem i8 %15, 10
%20 = add nsw i8 %19, 48
store i8 %20, i8* %18
%21 = getelementptr inbounds i8, i8* %0, i64 5
ret i8* %21
}