#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*/ swapIfNeeded(int *, int, int);
int median(int *array, int start, int end) {
  if ((end - start) < 2) {
    return start;
  }
  int middle = start + (end - start) / 2;
  swapIfNeeded(array, middle, end);
  swapIfNeeded(array, start, end);
  swapIfNeeded(array, start, middle);
  return middle;
}


x86 assembly (clang, -O0):
<median>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
cmp    $0x2,%eax
jge    113c <median+0x2c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118d <median+0x7d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <swapIfNeeded@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <swapIfNeeded@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1030 <swapIfNeeded@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<median>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edx,%ebp
sub    %esi,%ebp
cmp    $0x2,%ebp
jl     114e <median+0x3e>
mov    %edx,%r15d
mov    %rdi,%r14
shr    %ebp
add    %ebx,%ebp
mov    %ebp,%esi
call   1030 <swapIfNeeded@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
call   1030 <swapIfNeeded@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1030 <swapIfNeeded@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @median(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = load i32, i32* %6
%11 = sub nsw i32 %9, %10
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32, i32* %6
store i32 %14, i32* %4
br label %35
15: ; preds = %3
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = load i32, i32* %6
%19 = sub nsw i32 %17, %18
%20 = sdiv i32 %19, 2
%21 = add nsw i32 %16, %20
store i32 %21, i32* %8
%22 = load i32*, i32** %5
%23 = load i32, i32* %8
%24 = load i32, i32* %7
%25 = call i32 @swapIfNeeded(i32* %22, i32 %23, i32 %24)
%26 = load i32*, i32** %5
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = call i32 @swapIfNeeded(i32* %26, i32 %27, i32 %28)
%30 = load i32*, i32** %5
%31 = load i32, i32* %6
%32 = load i32, i32* %8
%33 = call i32 @swapIfNeeded(i32* %30, i32 %31, i32 %32)
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %15, %13
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
define i32 @median(i32* %0, i32 %1, i32 %2) {
%4 = sub nsw i32 %2, %1
%5 = icmp slt i32 %4, 2
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = lshr i32 %4, 1
%8 = add nsw i32 %7, %1
%9 = tail call i32 @swapIfNeeded(i32* %0, i32 %8, i32 %2)
%10 = tail call i32 @swapIfNeeded(i32* %0, i32 %1, i32 %2)
%11 = tail call i32 @swapIfNeeded(i32* %0, i32 %1, i32 %8)
br label %12
12: ; preds = %3, %6
%13 = phi i32 [ %8, %6 ], [ %1, %3 ]
ret i32 %13
}