#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 linear_select(int *, int /*<<< orphan*/, int, int);
int median_of_medians(int nums[], int begin, int end) {
  int len = end - begin;
  int parts_count = len / 5;
  int last_part_len = len % 5;
  int part[5] = {0};
  if (last_part_len > 0) {
    parts_count++;
  } else {
    last_part_len = 5;
  }
  int medians[parts_count];
  for (int i = 0; i < parts_count; i++) {
    int part_len = 5;
    if (i == parts_count - 1) {
      part_len = last_part_len;
    }
    for (int j = 0; j < part_len; j++) {
      part[j] = nums[begin + i * 5 + j];
    }
    insertion_sort(part, part_len);
    medians[i] = part[(part_len - 1) / 2];
  }
  int mm = linear_select(medians, 0, parts_count, (parts_count - 1) / 2);
  return mm;
}


x86 assembly (clang, -O0):
<median_of_medians>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    118f <median_of_medians+0x5f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1196 <median_of_medians+0x66>
movl   $0x5,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1269 <median_of_medians+0x139>
movl   $0x5,-0x48(%rbp)
mov    -0x44(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11ee <median_of_medians+0xbe>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    122d <median_of_medians+0xfd>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
imul   $0x5,-0x44(%rbp),%edx
add    %edx,%ecx
add    -0x4c(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
movslq -0x4c(%rbp),%rax
mov    %ecx,-0x30(%rbp,%rax,4)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11f5 <median_of_medians+0xc5>
lea    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%esi
call   1040 <insertion_sort@plt>
mov    -0x48(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
mov    -0x30(%rbp,%rcx,4),%edx
movslq -0x44(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11c4 <median_of_medians+0x94>
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x5c(%rbp),%edx
mov    %eax,%ecx
xor    %esi,%esi
call   1050 <linear_select@plt>
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<median_of_medians>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebx
mov    %rsp,-0x48(%rbp)
sub    %esi,%edx
movslq %edx,%rsi
imul   $0x66666667,%rsi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rax
add    %ecx,%eax
lea    (%rax,%rax,4),%ecx
sub    %ecx,%esi
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movl   $0x0,-0x30(%rbp)
xor    %ecx,%ecx
test   %esi,%esi
setg   %cl
mov    %eax,%r15d
add    %ecx,%r15d
lea    0xf(,%r15,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%r13
sub    %rdx,%r13
mov    %r13,%rsp
add    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
test   %r15d,%r15d
jle    1222 <median_of_medians+0xf2>
mov    -0x2c(%rbp),%eax
mov    %rax,-0x50(%rbp)
xor    %r12d,%r12d
mov    %rsi,-0x60(%rbp)
mov    %rdi,-0x58(%rbp)
nopl   0x0(%rax,%rax,1)
test   %esi,%esi
mov    $0x5,%r14d
cmovg  %esi,%r14d
cmp    %r12,-0x50(%rbp)
mov    $0x5,%eax
cmovne %eax,%r14d
movslq %ebx,%rbx
lea    (%rdi,%rbx,4),%rsi
lea    0x0(,%r14,4),%rdx
lea    -0x40(%rbp),%rdi
call   1030 <memcpy@plt>
lea    -0x40(%rbp),%rdi
mov    %r14d,%esi
call   1040 <insertion_sort@plt>
mov    -0x60(%rbp),%rsi
mov    -0x58(%rbp),%rdi
add    $0xffffffff,%r14d
shr    %r14d
mov    -0x40(%rbp,%r14,4),%eax
mov    %eax,0x0(%r13,%r12,4)
add    $0x1,%r12
add    $0x5,%ebx
cmp    %r12,%r15
jne    11c0 <median_of_medians+0x90>
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %r13,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <linear_select@plt>
mov    -0x48(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @median_of_medians(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 [5 x i32]
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%17 = load i32, i32* %6
%18 = load i32, i32* %5
%19 = sub nsw i32 %17, %18
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = sdiv i32 %20, 5
store i32 %21, i32* %8
%22 = load i32, i32* %7
%23 = srem i32 %22, 5
store i32 %23, i32* %9
%24 = bitcast [5 x i32]* %10 to i8*
%25 = load i32, i32* %9
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %3
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %31
30: ; preds = %3
store i32 5, i32* %9
br label %31
31: ; preds = %30, %27
%32 = load i32, i32* %8
%33 = zext i32 %32 to i64
%34 = call i8* @llvm.stacksave()
store i8* %34, i8** %11
%35 = alloca i32, i64 %33
store i64 %33, i64* %12
store i32 0, i32* %13
br label %36
36: ; preds = %82, %31
%37 = load i32, i32* %13
%38 = load i32, i32* %8
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %85
40: ; preds = %36
store i32 5, i32* %14
%41 = load i32, i32* %13
%42 = load i32, i32* %8
%43 = sub nsw i32 %42, 1
%44 = icmp eq i32 %41, %43
br i1 %44, label %45, label %47
45: ; preds = %40
%46 = load i32, i32* %9
store i32 %46, i32* %14
br label %47
47: ; preds = %45, %40
store i32 0, i32* %15
br label %48
48: ; preds = %66, %47
%49 = load i32, i32* %15
%50 = load i32, i32* %14
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %69
52: ; preds = %48
%53 = load i32*, i32** %4
%54 = load i32, i32* %5
%55 = load i32, i32* %13
%56 = mul nsw i32 %55, 5
%57 = add nsw i32 %54, %56
%58 = load i32, i32* %15
%59 = add nsw i32 %57, %58
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %53, i64 %60
%62 = load i32, i32* %61
%63 = load i32, i32* %15
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [5 x i32], [5 x i32]* %10, i64 0, i64 %64
store i32 %62, i32* %65
br label %66
66: ; preds = %52
%67 = load i32, i32* %15
%68 = add nsw i32 %67, 1
store i32 %68, i32* %15
br label %48
69: ; preds = %48
%70 = getelementptr inbounds [5 x i32], [5 x i32]* %10, i64 0, i64 0
%71 = load i32, i32* %14
%72 = call i32 @insertion_sort(i32* %70, i32 %71)
%73 = load i32, i32* %14
%74 = sub nsw i32 %73, 1
%75 = sdiv i32 %74, 2
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [5 x i32], [5 x i32]* %10, i64 0, i64 %76
%78 = load i32, i32* %77
%79 = load i32, i32* %13
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32, i32* %35, i64 %80
store i32 %78, i32* %81
br label %82
82: ; preds = %69
%83 = load i32, i32* %13
%84 = add nsw i32 %83, 1
store i32 %84, i32* %13
br label %36
85: ; preds = %36
%86 = load i32, i32* %8
%87 = load i32, i32* %8
%88 = sub nsw i32 %87, 1
%89 = sdiv i32 %88, 2
%90 = call i32 @linear_select(i32* %35, i32 0, i32 %86, i32 %89)
store i32 %90, i32* %16
%91 = load i32, i32* %16
%92 = load i8*, i8** %11
ret i32 %91
}

LLVM IR (-O2):
define i32 @median_of_medians(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca [5 x i32]
%5 = bitcast [5 x i32]* %4 to i8*
%6 = sub nsw i32 %2, %1
%7 = sdiv i32 %6, 5
%8 = srem i32 %6, 5
%9 = icmp sgt i32 %8, 0
%10 = zext i1 %9 to i32
%11 = add nsw i32 %7, %10
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i32, i64 %12
%15 = icmp sgt i32 %11, 0
%16 = add nsw i32 %11, -1
br i1 %15, label %17, label %20
17: ; preds = %3
%18 = getelementptr inbounds [5 x i32], [5 x i32]* %4, i64 0, i64 0
%19 = zext i32 %16 to i64
br label %23
20: ; preds = %23, %3
%21 = sdiv i32 %16, 2
%22 = call i32 @linear_select(i32* %14, i32 0, i32 %11, i32 %21)
ret i32 %22
23: ; preds = %17, %23
%24 = phi i64 [ 0, %17 ], [ %43, %23 ]
%25 = icmp eq i64 %24, %19
%26 = select i1 %25, i1 %9, i1 false
%27 = select i1 %26, i32 %8, i32 5
%28 = trunc i64 %24 to i32
%29 = mul i32 %28, 5
%30 = add i32 %29, %1
%31 = sext i32 %30 to i64
%32 = getelementptr i32, i32* %0, i64 %31
%33 = bitcast i32* %32 to i8*
%34 = zext i32 %27 to i64
%35 = shl nuw nsw i64 %34, 2
%36 = call i32 @insertion_sort(i32* %18, i32 %27)
%37 = add nsw i32 %27, -1
%38 = lshr i32 %37, 1
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds [5 x i32], [5 x i32]* %4, i64 0, i64 %39
%41 = load i32, i32* %40
%42 = getelementptr inbounds i32, i32* %14, i64 %24
store i32 %41, i32* %42
%43 = add nuw nsw i64 %24, 1
%44 = icmp eq i64 %43, %12
br i1 %44, label %20, label %23
}