#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*/ insertion_sort(int *, int *, int);
int *malloc(int);
scalar_t__ pow(int, int);
int /*<<< orphan*/ printf(char *, ...);
void RADIX_SORT(int *A, int len, int d) {
  int *tmp;
  int i, j, k;
  tmp = malloc(sizeof(int) * len);
  for (i = 1; i <= d; i++) {
    for (k = 0; k < len; k++)
      printf("%d\t", A[k]);
    printf("\n");
    for (j = 0; j < len; j++) {
      tmp[j] = (int)A[j] % (int)pow(10, i) / (int)pow(10, i - 1);
    }
    printf("\n");
    insertion_sort(A, tmp, len);
  }
}


x86 assembly (clang, -O0):
<RADIX_SORT>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     125a <RADIX_SORT+0x11a>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b2 <RADIX_SORT+0x72>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    117f <RADIX_SORT+0x3f>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    122e <RADIX_SORT+0xee>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%esi
mov    $0xa,%edi
call   1050 <pow@plt>
mov    %rax,%rcx
mov    -0x2c(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    $0xa,%edi
call   1050 <pow@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c7 <RADIX_SORT+0x87>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1060 <insertion_sort@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116c <RADIX_SORT+0x2c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RADIX_SORT>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %esi,%r15d
mov    %rdi,%r14
lea    0x0(,%r15,4),%edi
call   1050 <malloc@plt>
mov    %ebx,(%rsp)
test   %ebx,%ebx
jle    1279 <RADIX_SORT+0x129>
mov    %rax,%rbp
mov    %r15d,%r12d
mov    $0x1,%ebx
lea    0xe70(%rip),%r13
mov    %r15,0x10(%rsp)
jmp    11dc <RADIX_SORT+0x8c>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    0x10(%rsp),%r15
mov    %r15d,%edx
call   1070 <insertion_sort@plt>
lea    0x1(%rbx),%eax
cmp    (%rsp),%ebx
mov    %eax,%ebx
lea    0xe2a(%rip),%r13
je     1279 <RADIX_SORT+0x129>
test   %r15d,%r15d
jle    11a0 <RADIX_SORT+0x50>
mov    %rbx,0x8(%rsp)
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,4),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11f0 <RADIX_SORT+0xa0>
mov    $0xa,%edi
call   1030 <putchar@plt>
test   %r15d,%r15d
mov    0x8(%rsp),%rbx
jle    11aa <RADIX_SORT+0x5a>
lea    -0x1(%rbx),%eax
mov    %eax,0x4(%rsp)
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14,%r13,4),%r15d
mov    $0xa,%edi
mov    %ebx,%esi
call   1060 <pow@plt>
mov    %rax,%rcx
mov    %r15d,%eax
cltd
idiv   %ecx
mov    %edx,%ebx
mov    $0xa,%edi
mov    0x4(%rsp),%esi
call   1060 <pow@plt>
mov    %rax,%rcx
mov    %ebx,%eax
mov    0x8(%rsp),%rbx
cltd
idiv   %ecx
mov    %eax,0x0(%rbp,%r13,4)
add    $0x1,%r13
cmp    %r13,%r12
jne    1230 <RADIX_SORT+0xe0>
jmp    11aa <RADIX_SORT+0x5a>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\09\00"
@.str.1 = [2 x i8] c"\0A\00"
define void @RADIX_SORT(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* %5
%12 = sext i32 %11 to i64
%13 = mul i64 4, %12
%14 = trunc i64 %13 to i32
%15 = call i32* @malloc(i32 %14)
store i32* %15, i32** %7
store i32 1, i32* %8
br label %16
16: ; preds = %69, %3
%17 = load i32, i32* %8
%18 = load i32, i32* %6
%19 = icmp sle i32 %17, %18
br i1 %19, label %20, label %72
20: ; preds = %16
store i32 0, i32* %10
br label %21
21: ; preds = %32, %20
%22 = load i32, i32* %10
%23 = load i32, i32* %5
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %35
25: ; preds = %21
%26 = load i32*, i32** %4
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %25
%33 = load i32, i32* %10
%34 = add nsw i32 %33, 1
store i32 %34, i32* %10
br label %21
35: ; preds = %21
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %9
br label %37
37: ; preds = %60, %35
%38 = load i32, i32* %9
%39 = load i32, i32* %5
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %63
41: ; preds = %37
%42 = load i32*, i32** %4
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i32, i32* %42, i64 %44
%46 = load i32, i32* %45
%47 = load i32, i32* %8
%48 = call i64 @pow(i32 10, i32 %47)
%49 = trunc i64 %48 to i32
%50 = srem i32 %46, %49
%51 = load i32, i32* %8
%52 = sub nsw i32 %51, 1
%53 = call i64 @pow(i32 10, i32 %52)
%54 = trunc i64 %53 to i32
%55 = sdiv i32 %50, %54
%56 = load i32*, i32** %7
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %56, i64 %58
store i32 %55, i32* %59
br label %60
60: ; preds = %41
%61 = load i32, i32* %9
%62 = add nsw i32 %61, 1
store i32 %62, i32* %9
br label %37
63: ; preds = %37
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%65 = load i32*, i32** %4
%66 = load i32*, i32** %7
%67 = load i32, i32* %5
%68 = call i32 @insertion_sort(i32* %65, i32* %66, i32 %67)
br label %69
69: ; preds = %63
%70 = load i32, i32* %8
%71 = add nsw i32 %70, 1
store i32 %71, i32* %8
br label %16
72: ; preds = %16
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\09\00"
define void @RADIX_SORT(i32* %0, i32 %1, i32 %2) {
%4 = shl i32 %1, 2
%5 = tail call i32* @malloc(i32 %4)
%6 = icmp slt i32 %2, 1
br i1 %6, label %44, label %7
7: ; preds = %3
%8 = icmp sgt i32 %1, 0
%9 = zext i32 %1 to i64
%10 = zext i32 %1 to i64
br label %11
11: ; preds = %7, %39
%12 = phi i32 [ 1, %7 ], [ %42, %39 ]
br i1 %8, label %15, label %13
13: ; preds = %11
%14 = tail call i32 @putchar(i32 10)
br label %39
15: ; preds = %11, %15
%16 = phi i64 [ %20, %15 ], [ 0, %11 ]
%17 = getelementptr inbounds i32, i32* %0, i64 %16
%18 = load i32, i32* %17
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = add nuw nsw i64 %16, 1
%21 = icmp eq i64 %20, %9
br i1 %21, label %22, label %15
22: ; preds = %15
%23 = tail call i32 @putchar(i32 10)
br i1 %8, label %24, label %39
24: ; preds = %22
%25 = add nsw i32 %12, -1
br label %26
26: ; preds = %24, %26
%27 = phi i64 [ 0, %24 ], [ %37, %26 ]
%28 = getelementptr inbounds i32, i32* %0, i64 %27
%29 = load i32, i32* %28
%30 = tail call i64 @pow(i32 10, i32 %12)
%31 = trunc i64 %30 to i32
%32 = srem i32 %29, %31
%33 = tail call i64 @pow(i32 10, i32 %25)
%34 = trunc i64 %33 to i32
%35 = sdiv i32 %32, %34
%36 = getelementptr inbounds i32, i32* %5, i64 %27
store i32 %35, i32* %36
%37 = add nuw nsw i64 %27, 1
%38 = icmp eq i64 %37, %10
br i1 %38, label %39, label %26
39: ; preds = %26, %13, %22
%40 = tail call i32 @putchar(i32 10)
%41 = tail call i32 @insertion_sort(i32* %0, i32* %5, i32 %1)
%42 = add nuw i32 %12, 1
%43 = icmp eq i32 %12, %2
br i1 %43, label %44, label %11
44: ; preds = %39, %3
ret void
}