#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*/ swap(int *, int *);
int median_of_three_ints(int *array, int len) {
  len--;
  int indices[3] = {0, len / 2, len};
  int small = array[0], large = array[len / 2], final = array[len];
  if (small > large) {
    swap(&small, &large);
    swap(&indices[0], &indices[1]);
  }
  if (final < small) {
    return indices[0];
  }
  if (final > large) {
    return indices[1];
  }
  return indices[2];
}


x86 assembly (clang, -O0):
<median_of_three_ints>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    11a6 <median_of_three_ints+0x96>
lea    -0x24(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <swap@plt>
lea    -0x20(%rbp),%rdi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
call   1030 <swap@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11bd <median_of_three_ints+0xad>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <median_of_three_ints+0xca>
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    11d4 <median_of_three_ints+0xc4>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11da <median_of_three_ints+0xca>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<median_of_three_ints>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    -0x1(%rsi),%eax
movl   $0x0,0xc(%rsp)
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %esi,%ecx
add    $0xffffffff,%ecx
sar    %ecx
mov    %ecx,0x10(%rsp)
mov    %eax,0x14(%rsp)
mov    (%rdi),%eax
mov    %eax,0x8(%rsp)
movslq %ecx,%rcx
mov    (%rdi,%rcx,4),%ecx
mov    %ecx,0x4(%rsp)
movslq %esi,%rdx
mov    -0x4(%rdi,%rdx,4),%ebp
cmp    %ecx,%eax
jle    1175 <median_of_three_ints+0x65>
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
call   1030 <swap@plt>
lea    0xc(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <swap@plt>
mov    0x8(%rsp),%eax
cmp    %eax,%ebp
jge    117f <median_of_three_ints+0x6f>
mov    0xc(%rsp),%eax
jmp    118f <median_of_three_ints+0x7f>
cmp    0x4(%rsp),%ebp
jle    118b <median_of_three_ints+0x7b>
mov    0x10(%rsp),%eax
jmp    118f <median_of_three_ints+0x7f>
mov    0x14(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @median_of_three_ints(i32* nocapture %0, i32 %1) {
%3 = alloca [3 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = add nsw i32 %1, -1
%7 = bitcast [3 x i32]* %3 to i8*
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
store i32 0, i32* %8
%9 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
%10 = sdiv i32 %6, 2
store i32 %10, i32* %9
%11 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
store i32 %6, i32* %11
%12 = bitcast i32* %4 to i8*
%13 = load i32, i32* %0
store i32 %13, i32* %4
%14 = bitcast i32* %5 to i8*
%15 = sext i32 %10 to i64
%16 = getelementptr inbounds i32, i32* %0, i64 %15
%17 = load i32, i32* %16
store i32 %17, i32* %5
%18 = sext i32 %6 to i64
%19 = getelementptr inbounds i32, i32* %0, i64 %18
%20 = load i32, i32* %19
%21 = icmp sgt i32 %13, %17
br i1 %21, label %22, label %26
22: ; preds = %2
%23 = call i32 @swap(i32* %4, i32* %5)
%24 = call i32 @swap(i32* %8, i32* %9)
%25 = load i32, i32* %4
br label %26
26: ; preds = %22, %2
%27 = phi i32 [ %25, %22 ], [ %13, %2 ]
%28 = icmp slt i32 %20, %27
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i32, i32* %8
br label %38
31: ; preds = %26
%32 = load i32, i32* %5
%33 = icmp sgt i32 %20, %32
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32, i32* %9
br label %38
36: ; preds = %31
%37 = load i32, i32* %11
br label %38
38: ; preds = %36, %34, %29
%39 = phi i32 [ %30, %29 ], [ %35, %34 ], [ %37, %36 ]
ret i32 %39
}