#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 */
struct avl_tree_node const *avl_get_child(struct avl_tree_node const *, int);
struct avl_tree_node *avl_get_parent(struct avl_tree_node const *);
__attribute__((always_)) struct avl_tree_node *
avl_tree_next_or_prev_in_order(const struct avl_tree_node *node, int sign) {
  const struct avl_tree_node *next;
  if (avl_get_child(node, +sign)) {
    for (next = avl_get_child(node, +sign); avl_get_child(next, -sign);
         next = avl_get_child(next, -sign)) {
    }
  } else {
    for (next = avl_get_parent(node);
         next && node == avl_get_child(next, +sign);
         node = next, next = avl_get_parent(next)) {
    }
  }
  return (struct avl_tree_node *)next;
}


x86 assembly (clang, -O0):
<avl_tree_next_or_prev_in_order>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <avl_get_child@plt>
cmp    $0x0,%rax
je     118e <avl_tree_next_or_prev_in_order+0x6e>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <avl_get_child@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
sub    -0xc(%rbp),%esi
call   1030 <avl_get_child@plt>
cmp    $0x0,%rax
je     1189 <avl_tree_next_or_prev_in_order+0x69>
jmp    1172 <avl_tree_next_or_prev_in_order+0x52>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
sub    -0xc(%rbp),%esi
call   1030 <avl_get_child@plt>
mov    %rax,-0x18(%rbp)
jmp    1155 <avl_tree_next_or_prev_in_order+0x35>
jmp    1203 <avl_tree_next_or_prev_in_order+0xe3>
mov    -0x8(%rbp),%rdi
call   1040 <avl_get_parent@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
je     11cf <avl_tree_next_or_prev_in_order+0xaf>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <avl_get_child@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11df <avl_tree_next_or_prev_in_order+0xbf>
jmp    11fe <avl_tree_next_or_prev_in_order+0xde>
jmp    11e4 <avl_tree_next_or_prev_in_order+0xc4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <avl_get_parent@plt>
mov    %rax,-0x18(%rbp)
jmp    119b <avl_tree_next_or_prev_in_order+0x7b>
jmp    1203 <avl_tree_next_or_prev_in_order+0xe3>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<avl_tree_next_or_prev_in_order>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <avl_get_child@plt>
test   %rax,%rax
je     1185 <avl_tree_next_or_prev_in_order+0x65>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <avl_get_child@plt>
mov    %rax,%rbx
neg    %ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <avl_get_child@plt>
test   %rax,%rax
je     1194 <avl_tree_next_or_prev_in_order+0x74>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <avl_get_child@plt>
mov    %rax,%rbx
jmp    1150 <avl_tree_next_or_prev_in_order+0x30>
xchg   %ax,%ax
mov    %rbx,%r14
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <avl_get_child@plt>
cmp    %rax,%r14
jne    1194 <avl_tree_next_or_prev_in_order+0x74>
mov    %rbx,%rdi
call   1040 <avl_get_parent@plt>
test   %rax,%rax
jne    1170 <avl_tree_next_or_prev_in_order+0x50>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.avl_tree_node = type { i32 }
define %struct.avl_tree_node* @avl_tree_next_or_prev_in_order(%struct.avl_tree_node* %0, i32 %1) {
%3 = alloca %struct.avl_tree_node*
%4 = alloca i32
%5 = alloca %struct.avl_tree_node*
store %struct.avl_tree_node* %0, %struct.avl_tree_node** %3
store i32 %1, i32* %4
%6 = load %struct.avl_tree_node*, %struct.avl_tree_node** %3
%7 = load i32, i32* %4
%8 = call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %6, i32 %7)
%9 = icmp ne %struct.avl_tree_node* %8, null
br i1 %9, label %10, label %27
10: ; preds = %2
%11 = load %struct.avl_tree_node*, %struct.avl_tree_node** %3
%12 = load i32, i32* %4
%13 = call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %11, i32 %12)
store %struct.avl_tree_node* %13, %struct.avl_tree_node** %5
br label %14
14: ; preds = %21, %10
%15 = load %struct.avl_tree_node*, %struct.avl_tree_node** %5
%16 = load i32, i32* %4
%17 = sub nsw i32 0, %16
%18 = call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %15, i32 %17)
%19 = icmp ne %struct.avl_tree_node* %18, null
br i1 %19, label %20, label %26
20: ; preds = %14
br label %21
21: ; preds = %20
%22 = load %struct.avl_tree_node*, %struct.avl_tree_node** %5
%23 = load i32, i32* %4
%24 = sub nsw i32 0, %23
%25 = call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %22, i32 %24)
store %struct.avl_tree_node* %25, %struct.avl_tree_node** %5
br label %14
26: ; preds = %14
br label %47
27: ; preds = %2
%28 = load %struct.avl_tree_node*, %struct.avl_tree_node** %3
%29 = call %struct.avl_tree_node* @avl_get_parent(%struct.avl_tree_node* %28)
store %struct.avl_tree_node* %29, %struct.avl_tree_node** %5
br label %30
30: ; preds = %42, %27
%31 = load %struct.avl_tree_node*, %struct.avl_tree_node** %5
%32 = icmp ne %struct.avl_tree_node* %31, null
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load %struct.avl_tree_node*, %struct.avl_tree_node** %3
%35 = load %struct.avl_tree_node*, %struct.avl_tree_node** %5
%36 = load i32, i32* %4
%37 = call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %35, i32 %36)
%38 = icmp eq %struct.avl_tree_node* %34, %37
br label %39
39: ; preds = %33, %30
%40 = phi i1 [ false, %30 ], [ %38, %33 ]
br i1 %40, label %41, label %46
41: ; preds = %39
br label %42
42: ; preds = %41
%43 = load %struct.avl_tree_node*, %struct.avl_tree_node** %5
store %struct.avl_tree_node* %43, %struct.avl_tree_node** %3
%44 = load %struct.avl_tree_node*, %struct.avl_tree_node** %5
%45 = call %struct.avl_tree_node* @avl_get_parent(%struct.avl_tree_node* %44)
store %struct.avl_tree_node* %45, %struct.avl_tree_node** %5
br label %30
46: ; preds = %39
br label %47
47: ; preds = %46, %26
%48 = load %struct.avl_tree_node*, %struct.avl_tree_node** %5
ret %struct.avl_tree_node* %48
}

LLVM IR (-O2):
%struct.avl_tree_node = type { i32 }
define %struct.avl_tree_node* @avl_tree_next_or_prev_in_order(%struct.avl_tree_node* %0, i32 %1) {
%3 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %0, i32 %1)
%4 = icmp eq %struct.avl_tree_node* %3, null
br i1 %4, label %15, label %5
5: ; preds = %2
%6 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %0, i32 %1)
%7 = sub nsw i32 0, %1
%8 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %6, i32 %7)
%9 = icmp eq %struct.avl_tree_node* %8, null
br i1 %9, label %26, label %10
10: ; preds = %5, %10
%11 = phi %struct.avl_tree_node* [ %12, %10 ], [ %6, %5 ]
%12 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %11, i32 %7)
%13 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %12, i32 %7)
%14 = icmp eq %struct.avl_tree_node* %13, null
br i1 %14, label %26, label %10
15: ; preds = %2
%16 = tail call %struct.avl_tree_node* @avl_get_parent(%struct.avl_tree_node* %0)
%17 = icmp eq %struct.avl_tree_node* %16, null
br i1 %17, label %26, label %18
18: ; preds = %15, %23
%19 = phi %struct.avl_tree_node* [ %24, %23 ], [ %16, %15 ]
%20 = phi %struct.avl_tree_node* [ %19, %23 ], [ %0, %15 ]
%21 = tail call %struct.avl_tree_node* @avl_get_child(%struct.avl_tree_node* %19, i32 %1)
%22 = icmp eq %struct.avl_tree_node* %20, %21
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call %struct.avl_tree_node* @avl_get_parent(%struct.avl_tree_node* %19)
%25 = icmp eq %struct.avl_tree_node* %24, null
br i1 %25, label %26, label %18
26: ; preds = %10, %23, %18, %5, %15
%27 = phi %struct.avl_tree_node* [ null, %15 ], [ %6, %5 ], [ null, %23 ], [ %19, %18 ], [ %12, %10 ]
ret %struct.avl_tree_node* %27
}