#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 */
unsigned char mkhex(unsigned long);
unsigned char *ksprintn(unsigned char *nbuf, unsigned long ul,
                        unsigned char base, int width, unsigned char *lenp) {
  unsigned char *p;
  p = nbuf;
  *p = 0;
  for (;;) {
    *++p = mkhex(ul % base);
    ul /= base;
    if (--width > 0)
      continue;
    if (!ul)
      break;
  }
  if (lenp)
    *lenp = p - nbuf;
  return (p);
}


x86 assembly (clang, -O0):
<ksprintn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movzbl -0x11(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,%rdi
call   1030 <mkhex@plt>
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,0x1(%rax)
movzbl -0x11(%rbp),%eax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    118e <ksprintn+0x7e>
jmp    113b <ksprintn+0x2b>
cmpq   $0x0,-0x10(%rbp)
jne    119e <ksprintn+0x8e>
jmp    11a3 <ksprintn+0x93>
jmp    113b <ksprintn+0x2b>
cmpq   $0x0,-0x20(%rbp)
je     11c1 <ksprintn+0xb1>
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ksprintn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x8(%rsp)
mov    %rcx,0x10(%rsp)
mov    %rsi,%r13
mov    %rdi,%r12
movb   $0x0,(%rdi)
mov    %edx,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rbx
mov    0x10(%rsp),%rax
lea    (%rax,%rbp,1),%r15d
mov    %r13,%rax
shr    $0x20,%rax
je     1170 <ksprintn+0x60>
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
mov    %rdx,%rdi
mov    %rax,%r13
jmp    117c <ksprintn+0x6c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
mov    %edx,%edi
mov    %eax,%r13d
call   1030 <mkhex@plt>
mov    %al,0x1(%r12)
add    $0x1,%r12
add    $0xffffffff,%ebp
cmp    $0x1,%r15d
jg     1140 <ksprintn+0x30>
cmp    %r14,%rbx
jae    1140 <ksprintn+0x30>
mov    0x8(%rsp),%rax
test   %rax,%rax
je     11a8 <ksprintn+0x98>
neg    %bpl
mov    %bpl,(%rax)
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @ksprintn(i8* %0, i64 %1, i8 zeroext %2, i32 %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8 %2, i8* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%12 = load i8*, i8** %6
store i8* %12, i8** %11
%13 = load i8*, i8** %11
store i8 0, i8* %13
br label %14
14: ; preds = %34, %29, %5
%15 = load i64, i64* %7
%16 = load i8, i8* %8
%17 = zext i8 %16 to i64
%18 = urem i64 %15, %17
%19 = call zeroext i8 @mkhex(i64 %18)
%20 = load i8*, i8** %11
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %11
store i8 %19, i8* %21
%22 = load i8, i8* %8
%23 = zext i8 %22 to i64
%24 = load i64, i64* %7
%25 = udiv i64 %24, %23
store i64 %25, i64* %7
%26 = load i32, i32* %9
%27 = add nsw i32 %26, -1
store i32 %27, i32* %9
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %14
br label %14
30: ; preds = %14
%31 = load i64, i64* %7
%32 = icmp ne i64 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %30
br label %35
34: ; preds = %30
br label %14
35: ; preds = %33
%36 = load i8*, i8** %10
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %46
38: ; preds = %35
%39 = load i8*, i8** %11
%40 = load i8*, i8** %6
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i8
%45 = load i8*, i8** %10
store i8 %44, i8* %45
br label %46
46: ; preds = %38, %35
%47 = load i8*, i8** %11
ret i8* %47
}

LLVM IR (-O2):
define i8* @ksprintn(i8* %0, i64 %1, i8 zeroext %2, i32 %3, i8* writeonly %4) {
store i8 0, i8* %0
%6 = zext i8 %2 to i64
br label %7
7: ; preds = %7, %5
%8 = phi i32 [ %3, %5 ], [ %15, %7 ]
%9 = phi i64 [ %1, %5 ], [ %14, %7 ]
%10 = phi i8* [ %0, %5 ], [ %13, %7 ]
%11 = urem i64 %9, %6
%12 = tail call zeroext i8 @mkhex(i64 %11)
%13 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %12, i8* %13
%14 = udiv i64 %9, %6
%15 = add nsw i32 %8, -1
%16 = icmp sgt i32 %8, 1
%17 = icmp uge i64 %9, %6
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %7, label %19
19: ; preds = %7
%20 = icmp eq i8* %4, null
br i1 %20, label %26, label %21
21: ; preds = %19
%22 = ptrtoint i8* %13 to i64
%23 = ptrtoint i8* %0 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i8
store i8 %25, i8* %4
br label %26
26: ; preds = %21, %19
ret i8* %13
}