#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void *rd_median_of_three(void *base, size_t nmemb, size_t size,
                         int (*cmp)(const void *, const void *)) {
  void *med = (void *)((uint8_t *)base + (nmemb / 2) * size);
  const void *last = (void *)((uint8_t *)base + (nmemb - 1) * size);
  if (cmp(last, base) < 0) {
    do {
      uint8_t tmp[size];
      memcpy((void *)&tmp, (void *)(base), size);
      memcpy((void *)(base), (void *)(last), size);
      memcpy((void *)(last), (void *)&tmp, size);
    } while (0);
  }
  if (cmp(med, base) < 0) {
    do {
      uint8_t tmp[size];
      memcpy((void *)&tmp, (void *)(base), size);
      memcpy((void *)(base), (void *)(med), size);
      memcpy((void *)(med), (void *)&tmp, size);
    } while (0);
  }
  if (cmp(last, med) < 0) {
    do {
      uint8_t tmp[size];
      memcpy((void *)&tmp, (void *)(med), size);
      memcpy((void *)(med), (void *)(last), size);
      memcpy((void *)(last), (void *)&tmp, size);
    } while (0);
  }
  return med;
}


x86 assembly (clang, -O0):
<rd_median_of_three>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x1,%rcx
imul   -0x18(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
imul   -0x18(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jge    11e2 <rd_median_of_three+0xd2>
jmp    117f <rd_median_of_three+0x6f>
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x68(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x68(%rbp),%rsi
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
jmp    11e2 <rd_median_of_three+0xd2>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jge    1261 <rd_median_of_three+0x151>
jmp    11fe <rd_median_of_three+0xee>
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x70(%rbp),%rsi
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
jmp    1261 <rd_median_of_three+0x151>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jge    12e0 <rd_median_of_three+0x1d0>
jmp    127d <rd_median_of_three+0x16d>
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x78(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x78(%rbp),%rsi
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
jmp    12e0 <rd_median_of_three+0x1d0>
mov    -0x28(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rd_median_of_three>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rdi,%r12
mov    %rsi,%rax
shr    %rax
imul   %rdx,%rax
lea    (%rdi,%rax,4),%r14
add    $0xffffffffffffffff,%rsi
imul   %rdx,%rsi
lea    (%rdi,%rsi,4),%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   *%rcx
test   %eax,%eax
jns    119f <rd_median_of_three+0x8f>
mov    %rsp,-0x30(%rbp)
mov    %r15,-0x38(%rbp)
mov    %rsp,%r15
lea    0xf(,%r13,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    -0x38(%rbp),%r15
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    -0x30(%rbp),%rsp
mov    %r14,%rdi
mov    %r12,%rsi
call   *%r15
test   %eax,%eax
jns    1201 <rd_median_of_three+0xf1>
mov    %rsp,-0x30(%rbp)
mov    %rbx,-0x38(%rbp)
mov    %r15,%rbx
mov    %rsp,%r15
lea    0xf(,%r13,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%r15
mov    -0x38(%rbp),%rbx
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    -0x30(%rbp),%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
call   *%r15
test   %eax,%eax
jns    1256 <rd_median_of_three+0x146>
mov    %rbx,%r12
mov    %rsp,%r15
mov    %rsp,%rbx
lea    0xf(,%r13,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
mov    %r15,%rsp
mov    %r14,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @rd_median_of_three(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32 (i8*, i8*)*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
%17 = load i8*, i8** %5
%18 = bitcast i8* %17 to i32*
%19 = load i64, i64* %6
%20 = udiv i64 %19, 2
%21 = load i64, i64* %7
%22 = mul i64 %20, %21
%23 = getelementptr inbounds i32, i32* %18, i64 %22
%24 = bitcast i32* %23 to i8*
store i8* %24, i8** %9
%25 = load i8*, i8** %5
%26 = bitcast i8* %25 to i32*
%27 = load i64, i64* %6
%28 = sub i64 %27, 1
%29 = load i64, i64* %7
%30 = mul i64 %28, %29
%31 = getelementptr inbounds i32, i32* %26, i64 %30
%32 = bitcast i32* %31 to i8*
store i8* %32, i8** %10
%33 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%34 = load i8*, i8** %10
%35 = load i8*, i8** %5
%36 = call i32 %33(i8* %34, i8* %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %57
38: ; preds = %4
br label %39
39: ; preds = %38
%40 = load i64, i64* %7
%41 = call i8* @llvm.stacksave()
store i8* %41, i8** %11
%42 = alloca i32, i64 %40
store i64 %40, i64* %12
%43 = bitcast i32* %42 to i8*
%44 = load i8*, i8** %5
%45 = load i64, i64* %7
%46 = call i32 @memcpy(i8* %43, i8* %44, i64 %45)
%47 = load i8*, i8** %5
%48 = load i8*, i8** %10
%49 = load i64, i64* %7
%50 = call i32 @memcpy(i8* %47, i8* %48, i64 %49)
%51 = load i8*, i8** %10
%52 = bitcast i32* %42 to i8*
%53 = load i64, i64* %7
%54 = call i32 @memcpy(i8* %51, i8* %52, i64 %53)
%55 = load i8*, i8** %11
br label %56
56: ; preds = %39
br label %57
57: ; preds = %56, %4
%58 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%59 = load i8*, i8** %9
%60 = load i8*, i8** %5
%61 = call i32 %58(i8* %59, i8* %60)
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %82
63: ; preds = %57
br label %64
64: ; preds = %63
%65 = load i64, i64* %7
%66 = call i8* @llvm.stacksave()
store i8* %66, i8** %13
%67 = alloca i32, i64 %65
store i64 %65, i64* %14
%68 = bitcast i32* %67 to i8*
%69 = load i8*, i8** %5
%70 = load i64, i64* %7
%71 = call i32 @memcpy(i8* %68, i8* %69, i64 %70)
%72 = load i8*, i8** %5
%73 = load i8*, i8** %9
%74 = load i64, i64* %7
%75 = call i32 @memcpy(i8* %72, i8* %73, i64 %74)
%76 = load i8*, i8** %9
%77 = bitcast i32* %67 to i8*
%78 = load i64, i64* %7
%79 = call i32 @memcpy(i8* %76, i8* %77, i64 %78)
%80 = load i8*, i8** %13
br label %81
81: ; preds = %64
br label %82
82: ; preds = %81, %57
%83 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%84 = load i8*, i8** %10
%85 = load i8*, i8** %9
%86 = call i32 %83(i8* %84, i8* %85)
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %107
88: ; preds = %82
br label %89
89: ; preds = %88
%90 = load i64, i64* %7
%91 = call i8* @llvm.stacksave()
store i8* %91, i8** %15
%92 = alloca i32, i64 %90
store i64 %90, i64* %16
%93 = bitcast i32* %92 to i8*
%94 = load i8*, i8** %9
%95 = load i64, i64* %7
%96 = call i32 @memcpy(i8* %93, i8* %94, i64 %95)
%97 = load i8*, i8** %9
%98 = load i8*, i8** %10
%99 = load i64, i64* %7
%100 = call i32 @memcpy(i8* %97, i8* %98, i64 %99)
%101 = load i8*, i8** %10
%102 = bitcast i32* %92 to i8*
%103 = load i64, i64* %7
%104 = call i32 @memcpy(i8* %101, i8* %102, i64 %103)
%105 = load i8*, i8** %15
br label %106
106: ; preds = %89
br label %107
107: ; preds = %106, %82
%108 = load i8*, i8** %9
ret i8* %108
}

LLVM IR (-O2):
define i8* @rd_median_of_three(i8* %0, i64 %1, i64 %2, i32 (i8*, i8*)* nocapture %3) {
%5 = bitcast i8* %0 to i32*
%6 = lshr i64 %1, 1
%7 = mul i64 %6, %2
%8 = getelementptr inbounds i32, i32* %5, i64 %7
%9 = bitcast i32* %8 to i8*
%10 = add i64 %1, -1
%11 = mul i64 %10, %2
%12 = getelementptr inbounds i32, i32* %5, i64 %11
%13 = bitcast i32* %12 to i8*
%14 = tail call i32 %3(i8* %13, i8* %0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %4
%17 = tail call i8* @llvm.stacksave()
%18 = alloca i32, i64 %2
%19 = bitcast i32* %18 to i8*
%20 = call i32 @memcpy(i8* %19, i8* %0, i64 %2)
%21 = call i32 @memcpy(i8* %0, i8* %13, i64 %2)
%22 = call i32 @memcpy(i8* %13, i8* %19, i64 %2)
br label %23
23: ; preds = %16, %4
%24 = call i32 %3(i8* %9, i8* %0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = call i8* @llvm.stacksave()
%28 = alloca i32, i64 %2
%29 = bitcast i32* %28 to i8*
%30 = call i32 @memcpy(i8* %29, i8* %0, i64 %2)
%31 = call i32 @memcpy(i8* %0, i8* %9, i64 %2)
%32 = call i32 @memcpy(i8* %9, i8* %29, i64 %2)
br label %33
33: ; preds = %26, %23
%34 = call i32 %3(i8* %13, i8* %9)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = call i8* @llvm.stacksave()
%38 = alloca i32, i64 %2
%39 = bitcast i32* %38 to i8*
%40 = call i32 @memcpy(i8* %39, i8* %9, i64 %2)
%41 = call i32 @memcpy(i8* %9, i8* %13, i64 %2)
%42 = call i32 @memcpy(i8* %13, i8* %39, i64 %2)
br label %43
43: ; preds = %36, %33
ret i8* %9
}