#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 */
scalar_t__ ceil(double);
int SortedMedian(const int *values, size_t num_elems) {
  size_t index_median = 0;
  double num_elems_as_double = (double)num_elems;
  int median = 0;
  if (num_elems % 2 == 0) {
    size_t upper_idx = (size_t)ceil(num_elems_as_double / 2.0);
    size_t lower_idx = upper_idx - 1;
    median = (values[upper_idx] + values[lower_idx]) / 2;
  } else {
    index_median = (size_t)ceil(num_elems_as_double / 2.0 - 1.0);
    median = values[index_median];
  }
  return median;
}


x86 assembly (clang, -O0):
<SortedMedian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movaps 0xed1(%rip),%xmm1
movq   -0x10(%rbp),%xmm0
punpckldq %xmm1,%xmm0
movapd 0xed0(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    11b9 <SortedMedian+0xa9>
movsd  -0x20(%rbp),%xmm0
movsd  0xeae(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <ceil@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x38(%rbp),%rdx
add    (%rcx,%rdx,4),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
jmp    11ed <SortedMedian+0xdd>
movsd  -0x20(%rbp),%xmm0
movsd  0xe62(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe4e(%rip),%xmm1
subsd  %xmm1,%xmm0
call   1030 <ceil@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SortedMedian>:
push   %rbx
movq   %rsi,%xmm1
punpckldq 0xee2(%rip),%xmm1
mov    %rdi,%rbx
subpd  0xee7(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
mulsd  0xee3(%rip),%xmm0
test   $0x1,%sil
jne    115a <SortedMedian+0x4a>
call   1030 <ceil@plt>
mov    -0x4(%rbx,%rax,4),%ecx
add    (%rbx,%rax,4),%ecx
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
pop    %rbx
ret
addsd  0xec6(%rip),%xmm0
call   1030 <ceil@plt>
mov    (%rbx,%rax,4),%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @SortedMedian(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca double
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 0, i64* %5
%10 = load i64, i64* %4
%11 = uitofp i64 %10 to double
store double %11, double* %6
store i32 0, i32* %7
%12 = load i64, i64* %4
%13 = urem i64 %12, 2
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %31
15: ; preds = %2
%16 = load double, double* %6
%17 = fdiv double %16, 2.000000e+00
%18 = call i64 @ceil(double %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = sub i64 %19, 1
store i64 %20, i64* %9
%21 = load i32*, i32** %3
%22 = load i64, i64* %8
%23 = getelementptr inbounds i32, i32* %21, i64 %22
%24 = load i32, i32* %23
%25 = load i32*, i32** %3
%26 = load i64, i64* %9
%27 = getelementptr inbounds i32, i32* %25, i64 %26
%28 = load i32, i32* %27
%29 = add nsw i32 %24, %28
%30 = sdiv i32 %29, 2
store i32 %30, i32* %7
br label %40
31: ; preds = %2
%32 = load double, double* %6
%33 = fdiv double %32, 2.000000e+00
%34 = fsub double %33, 1.000000e+00
%35 = call i64 @ceil(double %34)
store i64 %35, i64* %5
%36 = load i32*, i32** %3
%37 = load i64, i64* %5
%38 = getelementptr inbounds i32, i32* %36, i64 %37
%39 = load i32, i32* %38
store i32 %39, i32* %7
br label %40
40: ; preds = %31, %15
%41 = load i32, i32* %7
ret i32 %41
}

LLVM IR (-O2):
define i32 @SortedMedian(i32* nocapture %0, i64 %1) {
%3 = uitofp i64 %1 to double
%4 = and i64 %1, 1
%5 = icmp eq i64 %4, 0
%6 = fmul double %3, 5.000000e-01
br i1 %5, label %7, label %16
7: ; preds = %2
%8 = tail call i64 @ceil(double %6)
%9 = add i64 %8, -1
%10 = getelementptr inbounds i32, i32* %0, i64 %8
%11 = load i32, i32* %10
%12 = getelementptr inbounds i32, i32* %0, i64 %9
%13 = load i32, i32* %12
%14 = add nsw i32 %13, %11
%15 = sdiv i32 %14, 2
br label %21
16: ; preds = %2
%17 = fadd double %6, -1.000000e+00
%18 = tail call i64 @ceil(double %17)
%19 = getelementptr inbounds i32, i32* %0, i64 %18
%20 = load i32, i32* %19
br label %21
21: ; preds = %16, %7
%22 = phi i32 [ %15, %7 ], [ %20, %16 ]
ret i32 %22
}