void __putn(unsigned n, int width, int padch, struct bufstate *b) {
  char s[10 + 1];
  char *p = s + sizeof s;
  int dcnt = 0;
  *--p = '\0';
  do {
    *--p = '0' + n % 10;
    n /= 10;
    dcnt++;
  } while (n);
  while (dcnt < width)
    __putc(padch, b), dcnt++;
  __puts(p, b);
}


x86 assembly (clang, -O0):
<__putn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
lea    -0x23(%rbp),%rax
add    $0xb,%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x0,-0x1(%rax)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
add    $0x30,%edx
mov    %dl,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x4(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    115b <__putn+0x3b>
jmp    11a5 <__putn+0x85>
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11cd <__putn+0xad>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1030 <__putc@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a5 <__putn+0x85>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1040 <__puts@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__putn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebp
lea    0x17(%rsp),%rbx
movb   $0x0,0x17(%rsp)
xor    %eax,%eax
mov    $0xcccccccd,%r8d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %edi,%edx
imul   %r8,%rdx
shr    $0x23,%rdx
lea    (%rdx,%rdx,1),%esi
lea    (%rsi,%rsi,4),%esi
mov    %edi,%ecx
sub    %esi,%ecx
or     $0x30,%cl
mov    %cl,-0x1(%rbx)
add    $0xffffffffffffffff,%rbx
add    $0x1,%eax
cmp    $0x9,%edi
mov    %edx,%edi
ja     1150 <__putn+0x30>
cmp    %ebp,%eax
jge    1192 <__putn+0x72>
sub    %eax,%ebp
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <__putc@plt>
add    $0xffffffff,%ebp
jne    1180 <__putn+0x60>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <__puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bufstate = type opaque
define void @__putn(i32 %0, i32 %1, i32 %2, %struct.bufstate* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.bufstate*
%9 = alloca [11 x i8]
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store %struct.bufstate* %3, %struct.bufstate** %8
%12 = getelementptr inbounds [11 x i8], [11 x i8]* %9, i64 0, i64 0
%13 = getelementptr inbounds i8, i8* %12, i64 11
store i8* %13, i8** %10
store i32 0, i32* %11
%14 = load i8*, i8** %10
%15 = getelementptr inbounds i8, i8* %14, i32 -1
store i8* %15, i8** %10
store i8 0, i8* %15
br label %16
16: ; preds = %27, %4
%17 = load i32, i32* %5
%18 = urem i32 %17, 10
%19 = add i32 48, %18
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %10
%22 = getelementptr inbounds i8, i8* %21, i32 -1
store i8* %22, i8** %10
store i8 %20, i8* %22
%23 = load i32, i32* %5
%24 = udiv i32 %23, 10
store i32 %24, i32* %5
%25 = load i32, i32* %11
%26 = add nsw i32 %25, 1
store i32 %26, i32* %11
br label %27
27: ; preds = %16
%28 = load i32, i32* %5
%29 = icmp ne i32 %28, 0
br i1 %29, label %16, label %30
30: ; preds = %27
br label %31
31: ; preds = %35, %30
%32 = load i32, i32* %11
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i32, i32* %7
%37 = load %struct.bufstate*, %struct.bufstate** %8
%38 = call i32 (i32, %struct.bufstate*, ...) bitcast (i32 (...)* @__putc to i32 (i32, %struct.bufstate*, ...)*)(i32 %36, %struct.bufstate* %37)
%39 = load i32, i32* %11
%40 = add nsw i32 %39, 1
store i32 %40, i32* %11
br label %31
41: ; preds = %31
%42 = load i8*, i8** %10
%43 = load %struct.bufstate*, %struct.bufstate** %8
%44 = call i32 (i8*, %struct.bufstate*, ...) bitcast (i32 (...)* @__puts to i32 (i8*, %struct.bufstate*, ...)*)(i8* %42, %struct.bufstate* %43)
ret void
}

LLVM IR (-O2):
%struct.bufstate = type opaque
define void @__putn(i32 %0, i32 %1, i32 %2, %struct.bufstate* %3) {
%5 = alloca [11 x i8]
%6 = getelementptr inbounds [11 x i8], [11 x i8]* %5, i64 0, i64 0
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %5, i64 0, i64 10
store i8 0, i8* %7
br label %8
8: ; preds = %8, %4
%9 = phi i32 [ %0, %4 ], [ %16, %8 ]
%10 = phi i8* [ %7, %4 ], [ %15, %8 ]
%11 = phi i32 [ 0, %4 ], [ %17, %8 ]
%12 = urem i32 %9, 10
%13 = trunc i32 %12 to i8
%14 = or i8 %13, 48
%15 = getelementptr inbounds i8, i8* %10, i64 -1
store i8 %14, i8* %15
%16 = udiv i32 %9, 10
%17 = add nuw nsw i32 %11, 1
%18 = icmp ult i32 %9, 10
br i1 %18, label %19, label %8
19: ; preds = %8
%20 = icmp slt i32 %17, %1
br i1 %20, label %21, label %26
21: ; preds = %19, %21
%22 = phi i32 [ %24, %21 ], [ %17, %19 ]
%23 = tail call i32 (i32, %struct.bufstate*, ...) bitcast (i32 (...)* @__putc to i32 (i32, %struct.bufstate*, ...)*)(i32 %2, %struct.bufstate* %3)
%24 = add i32 %22, 1
%25 = icmp eq i32 %24, %1
br i1 %25, label %26, label %21
26: ; preds = %21, %19
%27 = call i32 (i8*, %struct.bufstate*, ...) bitcast (i32 (...)* @__puts to i32 (i8*, %struct.bufstate*, ...)*)(i8* %15, %struct.bufstate* %3)
ret void
}