#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 */
typedef int u_long;
typedef int u_int;

/* Variables and functions */
int asprintf(char **, char *, int, int);
char *bmalloc(int);
int /*<<< orphan*/ err(int, int /*<<< orphan*/ *);
char *get_digit(u_long num, int digits, u_int base) {
  char *p;
  if (base <= 16) {
    p = bmalloc(2);
    p[0] = num >= 10 ? num + 'A' - 10 : num + '0';
    p[1] = '\0';
  } else {
    if (asprintf(&p, "%0*lu", digits, num) == -1)
      err(1, NULL);
  }
  return (p);
}


x86 assembly (clang, -O0):
<get_digit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
cmpl   $0x10,-0xc(%rbp)
jg     1195 <get_digit+0x65>
mov    $0x2,%edi
call   1030 <bmalloc@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0xa,-0x4(%rbp)
jl     1174 <get_digit+0x44>
mov    -0x4(%rbp),%eax
add    $0x41,%eax
sub    $0xa,%eax
mov    %eax,-0x1c(%rbp)
jmp    117d <get_digit+0x4d>
mov    -0x4(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
jmp    11c7 <get_digit+0x97>
mov    -0x8(%rbp),%edx
mov    -0x4(%rbp),%ecx
lea    -0x18(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11c2 <get_digit+0x92>
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <err@plt>
jmp    11c7 <get_digit+0x97>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_digit>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
cmp    $0x10,%edx
jg     1164 <get_digit+0x34>
mov    $0x2,%edi
call   1030 <bmalloc@plt>
lea    0x30(%rbx),%ecx
lea    0x37(%rbx),%edx
cmp    $0xa,%ebx
cmovl  %ecx,%edx
mov    %rax,0x8(%rsp)
mov    %dl,(%rax)
mov    0x8(%rsp),%rax
movb   $0x0,0x1(%rax)
jmp    118c <get_digit+0x5c>
mov    %esi,%eax
lea    0xe93(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %eax,%edx
mov    %ebx,%ecx
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    118c <get_digit+0x5c>
mov    $0x1,%edi
xor    %esi,%esi
call   1040 <err@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%0*lu\00"
define i8* @get_digit(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i32, i32* %6
%9 = icmp sle i32 %8, 16
br i1 %9, label %10, label %28
10: ; preds = %3
%11 = call i8* @bmalloc(i32 2)
store i8* %11, i8** %7
%12 = load i32, i32* %4
%13 = icmp sge i32 %12, 10
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* %4
%16 = add nsw i32 %15, 65
%17 = sub nsw i32 %16, 10
br label %21
18: ; preds = %10
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 48
br label %21
21: ; preds = %18, %14
%22 = phi i32 [ %17, %14 ], [ %20, %18 ]
%23 = trunc i32 %22 to i8
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i64 0
store i8 %23, i8* %25
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i64 1
store i8 0, i8* %27
br label %36
28: ; preds = %3
%29 = load i32, i32* %5
%30 = load i32, i32* %4
%31 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @err(i32 1, i32* null)
br label %35
35: ; preds = %33, %28
br label %36
36: ; preds = %35, %21
%37 = load i8*, i8** %7
ret i8* %37
}

LLVM IR (-O2):
@.str = [6 x i8] c"%0*lu\00"
define i8* @get_digit(i32 %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = icmp slt i32 %2, 17
br i1 %6, label %7, label %15
7: ; preds = %3
%8 = tail call i8* @bmalloc(i32 2)
store i8* %8, i8** %4
%9 = icmp sgt i32 %0, 9
%10 = select i1 %9, i32 55, i32 48
%11 = add nsw i32 %10, %0
%12 = trunc i32 %11 to i8
store i8 %12, i8* %8
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 1
store i8 0, i8* %14
br label %20
15: ; preds = %3
%16 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @err(i32 1, i32* null)
br label %20
20: ; preds = %15, %18, %7
%21 = load i8*, i8** %4
ret i8* %21
}