#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 read_nibble(unsigned long, int);
int /*<<< orphan*/ write_nibble(unsigned long *, int, int);
unsigned long nibble_sort_word_ref(unsigned long arg) {
  for (int i = 0; i < 16; ++i) {
    int min = i;
    for (int j = i + 1; j < 16; ++j) {
      if (read_nibble(arg, j) < read_nibble(arg, min))
        min = j;
    }
    if (min != i) {
      int tmp = read_nibble(arg, i);
      write_nibble(&arg, i, read_nibble(arg, min));
      write_nibble(&arg, min, tmp);
    }
  }
  return arg;
}


x86 assembly (clang, -O0):
<nibble_sort_word_ref>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x10,-0xc(%rbp)
jge    11f4 <nibble_sort_word_ref+0xd4>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x10,-0x14(%rbp)
jge    1197 <nibble_sort_word_ref+0x77>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <read_nibble@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <read_nibble@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
jge    1184 <nibble_sort_word_ref+0x64>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1189 <nibble_sort_word_ref+0x69>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114c <nibble_sort_word_ref+0x2c>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11e1 <nibble_sort_word_ref+0xc1>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <read_nibble@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <read_nibble@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    -0x8(%rbp),%rdi
call   1030 <write_nibble@plt>
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    -0x8(%rbp),%rdi
call   1030 <write_nibble@plt>
jmp    11e6 <nibble_sort_word_ref+0xc6>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1133 <nibble_sort_word_ref+0x13>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nibble_sort_word_ref>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,(%rsp)
xor    %r14d,%r14d
jmp    114a <nibble_sort_word_ref+0x2a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x10,%r14d
je     11e1 <nibble_sort_word_ref+0xc1>
mov    %r14d,%r15d
lea    0x1(%r15),%r14d
mov    %r14d,%ebp
mov    %r15d,%ebx
cmp    $0xe,%r15d
jbe    116b <nibble_sort_word_ref+0x4b>
jmp    1140 <nibble_sort_word_ref+0x20>
nop
add    $0x1,%ebp
mov    %r12d,%ebx
cmp    $0x10,%ebp
je     11a0 <nibble_sort_word_ref+0x80>
mov    (%rsp),%rdi
mov    %ebp,%esi
call   1040 <read_nibble@plt>
mov    %eax,%r13d
mov    (%rsp),%rdi
mov    %ebx,%esi
call   1040 <read_nibble@plt>
mov    %ebp,%r12d
cmp    %eax,%r13d
jl     1160 <nibble_sort_word_ref+0x40>
mov    %ebx,%r12d
jmp    1160 <nibble_sort_word_ref+0x40>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %r15d,%r12d
je     1140 <nibble_sort_word_ref+0x20>
mov    (%rsp),%rdi
mov    %r15d,%esi
call   1040 <read_nibble@plt>
mov    %eax,%ebx
mov    (%rsp),%rdi
mov    %r12d,%esi
call   1040 <read_nibble@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %eax,%edx
call   1030 <write_nibble@plt>
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %ebx,%edx
call   1030 <write_nibble@plt>
jmp    1140 <nibble_sort_word_ref+0x20>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @nibble_sort_word_ref(i64 %0) {
%2 = alloca i64
store i64 %0, i64* %2
br label %5
3: ; preds = %29
%4 = load i64, i64* %2
ret i64 %4
5: ; preds = %1, %29
%6 = phi i32 [ 0, %1 ], [ %7, %29 ]
%7 = add nuw nsw i32 %6, 1
%8 = icmp ult i32 %6, 15
br i1 %8, label %11, label %29
9: ; preds = %11
%10 = icmp eq i32 %19, %6
br i1 %10, label %29, label %22
11: ; preds = %5, %11
%12 = phi i32 [ %20, %11 ], [ %7, %5 ]
%13 = phi i32 [ %19, %11 ], [ %6, %5 ]
%14 = load i64, i64* %2
%15 = call i32 @read_nibble(i64 %14, i32 %12)
%16 = load i64, i64* %2
%17 = call i32 @read_nibble(i64 %16, i32 %13)
%18 = icmp slt i32 %15, %17
%19 = select i1 %18, i32 %12, i32 %13
%20 = add nuw nsw i32 %12, 1
%21 = icmp eq i32 %20, 16
br i1 %21, label %9, label %11
22: ; preds = %9
%23 = load i64, i64* %2
%24 = call i32 @read_nibble(i64 %23, i32 %6)
%25 = load i64, i64* %2
%26 = call i32 @read_nibble(i64 %25, i32 %19)
%27 = call i32 @write_nibble(i64* %2, i32 %6, i32 %26)
%28 = call i32 @write_nibble(i64* %2, i32 %19, i32 %24)
br label %29
29: ; preds = %5, %22, %9
%30 = icmp eq i32 %7, 16
br i1 %30, label %3, label %5
}