#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*/ assert(int);
int /*<<< orphan*/ counting_sort(int *, int *, int, int);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  printf("counting sort\n");
  int expected[8] = {0, 0, 2, 2, 3, 3, 3, 5};
  int arr[8] = {2, 5, 3, 0, 2, 3, 0, 3};
  int *result;
  result = (int *)malloc(8);
  memset(result, 0, 8);
  counting_sort(arr, result, 8, 5);
  for (int i = 0; i < 8; i++) {
    printf("expected: %d actual: %d\n", expected[i], result[i]);
    assert(expected[i] == result[i]);
  }
  free(result);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0xeac(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xea9(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xea6(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xea3(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xea0(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe9d(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe97(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    $0x8,%edi
call   1080 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    $0x8,%edx
mov    $0x5,%ecx
call   1050 <counting_sort@plt>
movl   $0x0,-0x5c(%rbp)
cmpl   $0x8,-0x5c(%rbp)
jge    126a <main+0x10a>
movslq -0x5c(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%esi
mov    -0x58(%rbp),%rax
movslq -0x5c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x5c(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%eax
mov    -0x58(%rbp),%rcx
movslq -0x5c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1211 <main+0xb1>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xebb(%rip),%rdi
call   1040 <puts@plt>
movaps 0xe86(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe6a(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    $0x8,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
mov    $0x5,%ecx
call   1060 <counting_sort@plt>
mov    (%rbx),%edx
lea    0xe4c(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
cmpl   $0x0,(%rbx)
sete   %dil
call   1080 <assert@plt>
mov    0x4(%rbx),%edx
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
cmpl   $0x0,0x4(%rbx)
sete   %dil
call   1080 <assert@plt>
mov    0x8(%rbx),%edx
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
cmpl   $0x2,0x8(%rbx)
sete   %dil
call   1080 <assert@plt>
mov    0xc(%rbx),%edx
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
cmpl   $0x2,0xc(%rbx)
sete   %dil
call   1080 <assert@plt>
mov    0x10(%rbx),%edx
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
cmpl   $0x3,0x10(%rbx)
sete   %dil
call   1080 <assert@plt>
mov    0x14(%rbx),%edx
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
cmpl   $0x3,0x14(%rbx)
sete   %dil
call   1080 <assert@plt>
mov    0x18(%rbx),%edx
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
cmpl   $0x3,0x18(%rbx)
sete   %dil
call   1080 <assert@plt>
mov    0x1c(%rbx),%edx
mov    %r14,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
cmpl   $0x5,0x1c(%rbx)
sete   %dil
call   1080 <assert@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"counting sort\0A\00"
@__const.main.expected = [8 x i32] [i32 0, i32 0, i32 2, i32 2, i32 3, i32 3, i32 3, i32 5]
@__const.main.arr = [8 x i32] [i32 2, i32 5, i32 3, i32 0, i32 2, i32 3, i32 0, i32 3]
@.str.1 = [25 x i8] c"expected: %d actual: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [8 x i32]
%3 = alloca [8 x i32]
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%7 = bitcast [8 x i32]* %2 to i8*
%8 = bitcast [8 x i32]* %3 to i8*
%9 = call i64 @malloc(i32 8)
%10 = inttoptr i64 %9 to i32*
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = call i32 @memset(i32* %11, i32 0, i32 8)
%13 = getelementptr inbounds [8 x i32], [8 x i32]* %3, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i32 @counting_sort(i32* %13, i32* %14, i32 8, i32 5)
store i32 0, i32* %5
br label %16
16: ; preds = %42, %0
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 8
br i1 %18, label %19, label %45
19: ; preds = %16
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 %21
%23 = load i32, i32* %22
%24 = load i32*, i32** %4
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
%28 = load i32, i32* %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %28)
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 %31
%33 = load i32, i32* %32
%34 = load i32*, i32** %4
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = load i32, i32* %37
%39 = icmp eq i32 %33, %38
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
br label %42
42: ; preds = %19
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %16
45: ; preds = %16
%46 = load i32*, i32** %4
%47 = call i32 @free(i32* %46)
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@__const.main.arr = [8 x i32] [i32 2, i32 5, i32 3, i32 0, i32 2, i32 3, i32 0, i32 3]
@.str.1 = [25 x i8] c"expected: %d actual: %d\0A\00"
@str = [14 x i8] c"counting sort\00"
define i32 @main() {
%1 = alloca [8 x i32]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%3 = bitcast [8 x i32]* %1 to i8*
%4 = tail call i64 @malloc(i32 8)
%5 = inttoptr i64 %4 to i32*
%6 = tail call i32 @memset(i32* %5, i32 0, i32 8)
%7 = getelementptr inbounds [8 x i32], [8 x i32]* %1, i64 0, i64 0
%8 = call i32 @counting_sort(i32* %7, i32* %5, i32 8, i32 5)
%9 = load i32, i32* %5
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %9)
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = getelementptr inbounds i32, i32* %5, i64 1
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 0, i32 %16)
%18 = load i32, i32* %15
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = getelementptr inbounds i32, i32* %5, i64 2
%23 = load i32, i32* %22
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 %23)
%25 = load i32, i32* %22
%26 = icmp eq i32 %25, 2
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = getelementptr inbounds i32, i32* %5, i64 3
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 %30)
%32 = load i32, i32* %29
%33 = icmp eq i32 %32, 2
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = getelementptr inbounds i32, i32* %5, i64 4
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 3, i32 %37)
%39 = load i32, i32* %36
%40 = icmp eq i32 %39, 3
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
%43 = getelementptr inbounds i32, i32* %5, i64 5
%44 = load i32, i32* %43
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 3, i32 %44)
%46 = load i32, i32* %43
%47 = icmp eq i32 %46, 3
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = getelementptr inbounds i32, i32* %5, i64 6
%51 = load i32, i32* %50
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 3, i32 %51)
%53 = load i32, i32* %50
%54 = icmp eq i32 %53, 3
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = getelementptr inbounds i32, i32* %5, i64 7
%58 = load i32, i32* %57
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 5, i32 %58)
%60 = load i32, i32* %57
%61 = icmp eq i32 %60, 5
%62 = zext i1 %61 to i32
%63 = call i32 @assert(i32 %62)
%64 = call i32 @free(i32* %5)
ret i32 0
}