#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__ floor(int);
int log(int);
scalar_t__ pow(int, int);
int deap_partner(int i, int *where, int last) {
  int level = (int)floor(log(i + 1) / log(2));
  int total_num = (int)pow(2, level);
  int partner;
  if (i <= total_num / 2) {
    *where = 1;
    partner = i + total_num / 2;
    if (partner > last)
      partner = (partner - 1) / 2;
  } else {
    *where = -1;
    partner = i - total_num / 2;
  }
  return partner;
}


x86 assembly (clang, -O0):
<deap_partner>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1030 <log@plt>
mov    %eax,-0x28(%rbp)
mov    $0x2,%edi
call   1030 <log@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <floor@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jg     11e1 <deap_partner+0xb1>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11dc <deap_partner+0xac>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
jmp    1206 <deap_partner+0xd6>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x4(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deap_partner>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x1(%rbx),%edi
call   1030 <log@plt>
mov    %eax,%ebp
mov    $0x2,%edi
call   1030 <log@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %eax,%edi
call   1050 <floor@plt>
mov    $0x2,%edi
mov    %eax,%esi
call   1040 <pow@plt>
mov    %rax,%rcx
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
cmp    %ebx,%eax
jge    1188 <deap_partner+0x58>
movl   $0xffffffff,(%r15)
sub    %eax,%ebx
mov    %ebx,%eax
jmp    11a5 <deap_partner+0x75>
movl   $0x1,(%r15)
add    %ebx,%eax
cmp    %r14d,%eax
jle    11a5 <deap_partner+0x75>
mov    %eax,%ecx
add    $0xffffffff,%ecx
shr    $0x1f,%ecx
add    %ecx,%eax
add    $0xffffffff,%eax
sar    %eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @deap_partner(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = load i32, i32* %4
%11 = add nsw i32 %10, 1
%12 = call i32 @log(i32 %11)
%13 = call i32 @log(i32 2)
%14 = sdiv i32 %12, %13
%15 = call i64 @floor(i32 %14)
%16 = trunc i64 %15 to i32
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = call i64 @pow(i32 2, i32 %17)
%19 = trunc i64 %18 to i32
store i32 %19, i32* %8
%20 = load i32, i32* %4
%21 = load i32, i32* %8
%22 = sdiv i32 %21, 2
%23 = icmp sle i32 %20, %22
br i1 %23, label %24, label %38
24: ; preds = %3
%25 = load i32*, i32** %5
store i32 1, i32* %25
%26 = load i32, i32* %4
%27 = load i32, i32* %8
%28 = sdiv i32 %27, 2
%29 = add nsw i32 %26, %28
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = load i32, i32* %6
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %24
%34 = load i32, i32* %9
%35 = sub nsw i32 %34, 1
%36 = sdiv i32 %35, 2
store i32 %36, i32* %9
br label %37
37: ; preds = %33, %24
br label %44
38: ; preds = %3
%39 = load i32*, i32** %5
store i32 -1, i32* %39
%40 = load i32, i32* %4
%41 = load i32, i32* %8
%42 = sdiv i32 %41, 2
%43 = sub nsw i32 %40, %42
store i32 %43, i32* %9
br label %44
44: ; preds = %38, %37
%45 = load i32, i32* %9
ret i32 %45
}

LLVM IR (-O2):
define i32 @deap_partner(i32 %0, i32* nocapture writeonly %1, i32 %2) {
%4 = add nsw i32 %0, 1
%5 = tail call i32 @log(i32 %4)
%6 = tail call i32 @log(i32 2)
%7 = sdiv i32 %5, %6
%8 = tail call i64 @floor(i32 %7)
%9 = trunc i64 %8 to i32
%10 = tail call i64 @pow(i32 2, i32 %9)
%11 = trunc i64 %10 to i32
%12 = sdiv i32 %11, 2
%13 = icmp slt i32 %12, %0
br i1 %13, label %20, label %14
14: ; preds = %3
store i32 1, i32* %1
%15 = add nsw i32 %12, %0
%16 = icmp sgt i32 %15, %2
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = add nsw i32 %15, -1
%19 = sdiv i32 %18, 2
br label %22
20: ; preds = %3
store i32 -1, i32* %1
%21 = sub nsw i32 %0, %12
br label %22
22: ; preds = %14, %17, %20
%23 = phi i32 [ %19, %17 ], [ %15, %14 ], [ %21, %20 ]
ret i32 %23
}