#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*/ hist(int *, int *, int);
int /*<<< orphan*/ init(int *);
int /*<<< orphan*/ last_step_scan(int *, int *);
int /*<<< orphan*/ local_scan(int *);
int /*<<< orphan*/ sum_scan(int *, int *);
int /*<<< orphan*/ update(int *, int *, int *, int);
void ss_sort(int a[2048], int b[2048], int bucket[512 * 4],
             int sum[512 * 4 / 16]) {
  int exp = 0;
  int valid_buffer = 0;
sort_1:
  for (exp = 0; exp < 32; exp += 2) {
    init(bucket);
    if (valid_buffer == 0) {
      hist(bucket, a, exp);
    } else {
      hist(bucket, b, exp);
    }
    local_scan(bucket);
    sum_scan(sum, bucket);
    last_step_scan(bucket, sum);
    if (valid_buffer == 0) {
      update(b, bucket, a, exp);
      valid_buffer = 1;
    } else {
      update(a, bucket, b, exp);
      valid_buffer = 0;
    }
  }
}


x86 assembly (clang, -O0):
<ss_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x20,-0x24(%rbp)
jge    124a <ss_sort+0xea>
mov    -0x18(%rbp),%rdi
call   1060 <init@plt>
cmpl   $0x0,-0x28(%rbp)
jne    11bf <ss_sort+0x5f>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <hist@plt>
jmp    11cf <ss_sort+0x6f>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <hist@plt>
mov    -0x18(%rbp),%rdi
call   1080 <local_scan@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <sum_scan@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <last_step_scan@plt>
cmpl   $0x0,-0x28(%rbp)
jne    121c <ss_sort+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1030 <update@plt>
movl   $0x1,-0x28(%rbp)
jmp    1237 <ss_sort+0xd7>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1030 <update@plt>
movl   $0x0,-0x28(%rbp)
jmp    123c <ss_sort+0xdc>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
jmp    118d <ss_sort+0x2d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    119c <ss_sort+0x3c>
nopl   (%rax)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
mov    %ebx,%ecx
call   1030 <update@plt>
xor    %ebp,%ebp
lea    0x2(%rbx),%eax
cmp    $0x1e,%ebx
mov    %eax,%ebx
jae    11f0 <ss_sort+0x90>
mov    %r13,%rdi
call   1060 <init@plt>
test   %ebp,%ebp
mov    %r15,%rsi
cmove  %r12,%rsi
mov    %r13,%rdi
mov    %ebx,%edx
call   1070 <hist@plt>
mov    %r13,%rdi
call   1080 <local_scan@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   1050 <sum_scan@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   1040 <last_step_scan@plt>
test   %ebp,%ebp
jne    1180 <ss_sort+0x20>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %ebx,%ecx
call   1030 <update@plt>
mov    $0x1,%ebp
jmp    1192 <ss_sort+0x32>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @ss_sort(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %9
store i32 0, i32* %10
br label %11
11: ; preds = %4
store i32 0, i32* %9
br label %12
12: ; preds = %54, %11
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 32
br i1 %14, label %15, label %57
15: ; preds = %12
%16 = load i32*, i32** %7
%17 = call i32 @init(i32* %16)
%18 = load i32, i32* %10
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32*, i32** %7
%22 = load i32*, i32** %5
%23 = load i32, i32* %9
%24 = call i32 @hist(i32* %21, i32* %22, i32 %23)
br label %30
25: ; preds = %15
%26 = load i32*, i32** %7
%27 = load i32*, i32** %6
%28 = load i32, i32* %9
%29 = call i32 @hist(i32* %26, i32* %27, i32 %28)
br label %30
30: ; preds = %25, %20
%31 = load i32*, i32** %7
%32 = call i32 @local_scan(i32* %31)
%33 = load i32*, i32** %8
%34 = load i32*, i32** %7
%35 = call i32 @sum_scan(i32* %33, i32* %34)
%36 = load i32*, i32** %7
%37 = load i32*, i32** %8
%38 = call i32 @last_step_scan(i32* %36, i32* %37)
%39 = load i32, i32* %10
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %30
%42 = load i32*, i32** %6
%43 = load i32*, i32** %7
%44 = load i32*, i32** %5
%45 = load i32, i32* %9
%46 = call i32 @update(i32* %42, i32* %43, i32* %44, i32 %45)
store i32 1, i32* %10
br label %53
47: ; preds = %30
%48 = load i32*, i32** %5
%49 = load i32*, i32** %7
%50 = load i32*, i32** %6
%51 = load i32, i32* %9
%52 = call i32 @update(i32* %48, i32* %49, i32* %50, i32 %51)
store i32 0, i32* %10
br label %53
53: ; preds = %47, %41
br label %54
54: ; preds = %53
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 2
store i32 %56, i32* %9
br label %12
57: ; preds = %12
ret void
}

LLVM IR (-O2):
define void @ss_sort(i32* %0, i32* %1, i32* %2, i32* %3) {
br label %5
5: ; preds = %4, %19
%6 = phi i32 [ 0, %4 ], [ %20, %19 ]
%7 = phi i32 [ 0, %4 ], [ %21, %19 ]
%8 = tail call i32 @init(i32* %2)
%9 = icmp eq i32 %6, 0
%10 = select i1 %9, i32* %0, i32* %1
%11 = tail call i32 @hist(i32* %2, i32* %10, i32 %7)
%12 = tail call i32 @local_scan(i32* %2)
%13 = tail call i32 @sum_scan(i32* %3, i32* %2)
%14 = tail call i32 @last_step_scan(i32* %2, i32* %3)
br i1 %9, label %15, label %17
15: ; preds = %5
%16 = tail call i32 @update(i32* %1, i32* %2, i32* %0, i32 %7)
br label %19
17: ; preds = %5
%18 = tail call i32 @update(i32* %0, i32* %2, i32* %1, i32 %7)
br label %19
19: ; preds = %15, %17
%20 = phi i32 [ 1, %15 ], [ 0, %17 ]
%21 = add nuw nsw i32 %7, 2
%22 = icmp ult i32 %7, 30
br i1 %22, label %5, label %23
23: ; preds = %19
ret void
}