#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 */
struct avl_tree_node {
  int dummy;
};

/* Variables and functions */
int avl_get_balance_factor(struct avl_tree_node *const);
struct avl_tree_node *avl_get_child(struct avl_tree_node *const, int const);
struct avl_tree_node *avl_get_parent(struct avl_tree_node *const);
int /*<<< orphan*/ avl_replace_child(struct avl_tree_node **const,
                                     struct avl_tree_node *const,
                                     struct avl_tree_node *const,
                                     struct avl_tree_node *const);
int /*<<< orphan*/ avl_set_child(struct avl_tree_node *const, int const,
                                 struct avl_tree_node *const);
int /*<<< orphan*/ avl_set_parent(struct avl_tree_node *const,
                                  struct avl_tree_node *const);
int /*<<< orphan*/ avl_set_parent_balance(struct avl_tree_node *const,
                                          struct avl_tree_node *const,
                                          int const);
__attribute__((always_)) struct avl_tree_node *
avl_do_double_rotate(struct avl_tree_node **const root_ptr,
                     struct avl_tree_node *const B,
                     struct avl_tree_node *const A, const int sign) {
  struct avl_tree_node *const E = avl_get_child(B, +sign);
  struct avl_tree_node *const F = avl_get_child(E, -sign);
  struct avl_tree_node *const G = avl_get_child(E, +sign);
  struct avl_tree_node *const P = avl_get_parent(A);
  const int e = avl_get_balance_factor(E);
  avl_set_child(A, -sign, G);
  avl_set_parent_balance(A, E, ((sign * e >= 0) ? 0 : -e));
  avl_set_child(B, +sign, F);
  avl_set_parent_balance(B, E, ((sign * e <= 0) ? 0 : -e));
  avl_set_child(E, +sign, A);
  avl_set_child(E, -sign, B);
  avl_set_parent_balance(E, P, 0);
  if (G) {
    avl_set_parent(G, A);
  }
  if (F) {
    avl_set_parent(F, B);
  }
  avl_replace_child(root_ptr, P, A, E);
  return E;
}


x86 assembly (clang, -O0):
<avl_do_double_rotate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <avl_get_child@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
sub    -0x1c(%rbp),%esi
call   1040 <avl_get_child@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <avl_get_child@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <avl_get_parent@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <avl_get_balance_factor@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
sub    -0x1c(%rbp),%esi
mov    -0x38(%rbp),%rdx
call   1060 <avl_set_child@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x44(%rbp),%eax
cmp    $0x0,%eax
jl     1211 <avl_do_double_rotate+0xa1>
xor    %eax,%eax
mov    %eax,-0x5c(%rbp)
jmp    1219 <avl_do_double_rotate+0xa9>
xor    %eax,%eax
sub    -0x44(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rdi
mov    -0x5c(%rbp),%edx
call   1030 <avl_set_parent_balance@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rdx
call   1060 <avl_set_child@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x44(%rbp),%eax
cmp    $0x0,%eax
jg     1263 <avl_do_double_rotate+0xf3>
xor    %eax,%eax
mov    %eax,-0x74(%rbp)
jmp    126b <avl_do_double_rotate+0xfb>
xor    %eax,%eax
sub    -0x44(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x68(%rbp),%rsi
mov    -0x70(%rbp),%rdi
mov    -0x74(%rbp),%edx
call   1030 <avl_set_parent_balance@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1060 <avl_set_child@plt>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
sub    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1060 <avl_set_child@plt>
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1030 <avl_set_parent_balance@plt>
cmpq   $0x0,-0x38(%rbp)
je     12c4 <avl_do_double_rotate+0x154>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <avl_set_parent@plt>
cmpq   $0x0,-0x30(%rbp)
je     12dc <avl_do_double_rotate+0x16c>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <avl_set_parent@plt>
mov    -0x8(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1080 <avl_replace_child@plt>
mov    -0x28(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<avl_do_double_rotate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %rsi,0x20(%rsp)
mov    %rdi,0x30(%rsp)
mov    %rsi,%rdi
mov    %ecx,%esi
call   1040 <avl_get_child@plt>
mov    %rax,%r13
mov    %r15d,%esi
neg    %esi
mov    %esi,0x4(%rsp)
mov    %rax,%rdi
call   1040 <avl_get_child@plt>
mov    %rax,0x8(%rsp)
mov    %r13,%rdi
mov    %r15d,%esi
call   1040 <avl_get_child@plt>
mov    %rax,%r14
mov    %rax,0x28(%rsp)
mov    %rbx,%rdi
call   1090 <avl_get_parent@plt>
mov    %rax,0x18(%rsp)
mov    %r13,%rdi
call   1050 <avl_get_balance_factor@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %rbx,0x10(%rsp)
mov    0x4(%rsp),%esi
mov    %r14,%rdx
call   1060 <avl_set_child@plt>
mov    %ebp,%r12d
imul   %r15d,%r12d
neg    %ebp
mov    %r12d,%edx
sar    $0x1f,%edx
and    %ebp,%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1030 <avl_set_parent_balance@plt>
mov    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    0x8(%rsp),%rdx
call   1060 <avl_set_child@plt>
xor    %eax,%eax
test   %r12d,%r12d
cmovle %eax,%ebp
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <avl_set_parent_balance@plt>
mov    %r13,%rdi
mov    %r15d,%esi
mov    0x18(%rsp),%r15
mov    0x10(%rsp),%rbp
mov    %rbp,%rdx
call   1060 <avl_set_child@plt>
mov    %r13,%rdi
mov    0x4(%rsp),%esi
mov    %rbx,%rdx
call   1060 <avl_set_child@plt>
mov    %r13,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <avl_set_parent_balance@plt>
mov    0x28(%rsp),%rdi
test   %rdi,%rdi
je     127b <avl_do_double_rotate+0x10b>
mov    %rbp,%rsi
call   1070 <avl_set_parent@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     128d <avl_do_double_rotate+0x11d>
mov    %rbx,%rsi
call   1070 <avl_set_parent@plt>
mov    0x30(%rsp),%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %r13,%rcx
call   1080 <avl_replace_child@plt>
mov    %r13,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.avl_tree_node = type { i32 }
define %struct.avl_tree_node* @avl_do_double_rotate(%struct.avl_tree_node** %0, %struct.avl_tree_node* %1, %struct.avl_tree_node* %2, i32 %3) {
%5 = alloca %struct.avl_tree_node**
%6 = alloca %struct.avl_tree_node*
%7 = alloca %struct.avl_tree_node*
%8 = alloca i32
%9 = alloca %struct.avl_tree_node*
%10 = alloca %struct.avl_tree_node*
%11 = alloca %struct.avl_tree_node*
%12 = alloca %struct.avl_tree_node*
%13 = alloca i32
store %struct.avl_tree_node** %0, %struct.avl_tree_node*** %5
store %struct.avl_tree_node* %1, %struct.avl_tree_node** %6
store %struct.avl_tree_node* %2, %struct.avl_tree_node** %7
store i32 %3, i32* %8
%14 = load %struct.avl_tree_node*, %struct.avl_tree_node** %6
%15 = load i32, i32* %8
%16 = call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %14, i32 %15)
store %struct.avl_tree_node* %16, %struct.avl_tree_node** %9
%17 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%18 = load i32, i32* %8
%19 = sub nsw i32 0, %18
%20 = call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %17, i32 %19)
store %struct.avl_tree_node* %20, %struct.avl_tree_node** %10
%21 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%22 = load i32, i32* %8
%23 = call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %21, i32 %22)
store %struct.avl_tree_node* %23, %struct.avl_tree_node** %11
%24 = load %struct.avl_tree_node*, %struct.avl_tree_node** %7
%25 = call %struct.avl_tree_node* @avl_get_parent(%struct.avl_tree_node* %24)
store %struct.avl_tree_node* %25, %struct.avl_tree_node** %12
%26 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%27 = call i32 @avl_get_balance_factor(%struct.avl_tree_node* %26)
store i32 %27, i32* %13
%28 = load %struct.avl_tree_node*, %struct.avl_tree_node** %7
%29 = load i32, i32* %8
%30 = sub nsw i32 0, %29
%31 = load %struct.avl_tree_node*, %struct.avl_tree_node** %11
%32 = call i32 @avl_set_child(%struct.avl_tree_node* %28, i32 %30, %struct.avl_tree_node* %31)
%33 = load %struct.avl_tree_node*, %struct.avl_tree_node** %7
%34 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%35 = load i32, i32* %8
%36 = load i32, i32* %13
%37 = mul nsw i32 %35, %36
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %4
br label %43
40: ; preds = %4
%41 = load i32, i32* %13
%42 = sub nsw i32 0, %41
br label %43
43: ; preds = %40, %39
%44 = phi i32 [ 0, %39 ], [ %42, %40 ]
%45 = call i32 @avl_set_parent_balance(%struct.avl_tree_node* %33, %struct.avl_tree_node* %34, i32 %44)
%46 = load %struct.avl_tree_node*, %struct.avl_tree_node** %6
%47 = load i32, i32* %8
%48 = load %struct.avl_tree_node*, %struct.avl_tree_node** %10
%49 = call i32 @avl_set_child(%struct.avl_tree_node* %46, i32 %47, %struct.avl_tree_node* %48)
%50 = load %struct.avl_tree_node*, %struct.avl_tree_node** %6
%51 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%52 = load i32, i32* %8
%53 = load i32, i32* %13
%54 = mul nsw i32 %52, %53
%55 = icmp sle i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %43
br label %60
57: ; preds = %43
%58 = load i32, i32* %13
%59 = sub nsw i32 0, %58
br label %60
60: ; preds = %57, %56
%61 = phi i32 [ 0, %56 ], [ %59, %57 ]
%62 = call i32 @avl_set_parent_balance(%struct.avl_tree_node* %50, %struct.avl_tree_node* %51, i32 %61)
%63 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%64 = load i32, i32* %8
%65 = load %struct.avl_tree_node*, %struct.avl_tree_node** %7
%66 = call i32 @avl_set_child(%struct.avl_tree_node* %63, i32 %64, %struct.avl_tree_node* %65)
%67 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%68 = load i32, i32* %8
%69 = sub nsw i32 0, %68
%70 = load %struct.avl_tree_node*, %struct.avl_tree_node** %6
%71 = call i32 @avl_set_child(%struct.avl_tree_node* %67, i32 %69, %struct.avl_tree_node* %70)
%72 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%73 = load %struct.avl_tree_node*, %struct.avl_tree_node** %12
%74 = call i32 @avl_set_parent_balance(%struct.avl_tree_node* %72, %struct.avl_tree_node* %73, i32 0)
%75 = load %struct.avl_tree_node*, %struct.avl_tree_node** %11
%76 = icmp ne %struct.avl_tree_node* %75, null
br i1 %76, label %77, label %81
77: ; preds = %60
%78 = load %struct.avl_tree_node*, %struct.avl_tree_node** %11
%79 = load %struct.avl_tree_node*, %struct.avl_tree_node** %7
%80 = call i32 @avl_set_parent(%struct.avl_tree_node* %78, %struct.avl_tree_node* %79)
br label %81
81: ; preds = %77, %60
%82 = load %struct.avl_tree_node*, %struct.avl_tree_node** %10
%83 = icmp ne %struct.avl_tree_node* %82, null
br i1 %83, label %84, label %88
84: ; preds = %81
%85 = load %struct.avl_tree_node*, %struct.avl_tree_node** %10
%86 = load %struct.avl_tree_node*, %struct.avl_tree_node** %6
%87 = call i32 @avl_set_parent(%struct.avl_tree_node* %85, %struct.avl_tree_node* %86)
br label %88
88: ; preds = %84, %81
%89 = load %struct.avl_tree_node**, %struct.avl_tree_node*** %5
%90 = load %struct.avl_tree_node*, %struct.avl_tree_node** %12
%91 = load %struct.avl_tree_node*, %struct.avl_tree_node** %7
%92 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
%93 = call i32 @avl_replace_child(%struct.avl_tree_node** %89, %struct.avl_tree_node* %90, %struct.avl_tree_node* %91, %struct.avl_tree_node* %92)
%94 = load %struct.avl_tree_node*, %struct.avl_tree_node** %9
ret %struct.avl_tree_node* %94
}

LLVM IR (-O2):
%struct.avl_tree_node = type { i32 }
define %struct.avl_tree_node* @avl_do_double_rotate(%struct.avl_tree_node** %0, %struct.avl_tree_node* %1, %struct.avl_tree_node* %2, i32 %3) {
%5 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %1, i32 %3)
%6 = sub nsw i32 0, %3
%7 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %5, i32 %6)
%8 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %5, i32 %3)
%9 = tail call %struct.avl_tree_node* @avl_get_parent(%struct.avl_tree_node* %2)
%10 = tail call i32 @avl_get_balance_factor(%struct.avl_tree_node* %5)
%11 = tail call i32 @avl_set_child(%struct.avl_tree_node* %2, i32 %6, %struct.avl_tree_node* %8)
%12 = mul nsw i32 %10, %3
%13 = sub nsw i32 0, %10
%14 = icmp slt i32 %12, 0
%15 = select i1 %14, i32 %13, i32 0
%16 = tail call i32 @avl_set_parent_balance(%struct.avl_tree_node* %2, %struct.avl_tree_node* %5, i32 %15)
%17 = tail call i32 @avl_set_child(%struct.avl_tree_node* %1, i32 %3, %struct.avl_tree_node* %7)
%18 = icmp sgt i32 %12, 0
%19 = select i1 %18, i32 %13, i32 0
%20 = tail call i32 @avl_set_parent_balance(%struct.avl_tree_node* %1, %struct.avl_tree_node* %5, i32 %19)
%21 = tail call i32 @avl_set_child(%struct.avl_tree_node* %5, i32 %3, %struct.avl_tree_node* %2)
%22 = tail call i32 @avl_set_child(%struct.avl_tree_node* %5, i32 %6, %struct.avl_tree_node* %1)
%23 = tail call i32 @avl_set_parent_balance(%struct.avl_tree_node* %5, %struct.avl_tree_node* %9, i32 0)
%24 = icmp eq %struct.avl_tree_node* %8, null
br i1 %24, label %27, label %25
25: ; preds = %4
%26 = tail call i32 @avl_set_parent(%struct.avl_tree_node* %8, %struct.avl_tree_node* %2)
br label %27
27: ; preds = %25, %4
%28 = icmp eq %struct.avl_tree_node* %7, null
br i1 %28, label %31, label %29
29: ; preds = %27
%30 = tail call i32 @avl_set_parent(%struct.avl_tree_node* %7, %struct.avl_tree_node* %1)
br label %31
31: ; preds = %29, %27
%32 = tail call i32 @avl_replace_child(%struct.avl_tree_node** %0, %struct.avl_tree_node* %9, %struct.avl_tree_node* %2, %struct.avl_tree_node* %5)
ret %struct.avl_tree_node* %5
}