#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 rb3_head {
  int dummy;
};

/* Variables and functions */
struct rb3_head *rb3_get_parent(struct rb3_head *);
int rb3_get_parent_dir(struct rb3_head *);
scalar_t__ rb3_is_base(struct rb3_head *);
struct rb3_head *rb3_get_prevnext_ancestor(struct rb3_head *head, int dir) {
  while (head && rb3_get_parent_dir(head) == dir) {
    head = rb3_get_parent(head);
  }
  if (head) {
    head = rb3_get_parent(head);
    if (!head || rb3_is_base(head))
      return NULL;
    return head;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<rb3_get_prevnext_ancestor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x15(%rbp)
je     1161 <rb3_get_prevnext_ancestor+0x31>
mov    -0x10(%rbp),%rdi
call   1050 <rb3_get_parent_dir@plt>
cmp    -0x14(%rbp),%eax
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1171 <rb3_get_prevnext_ancestor+0x41>
jmp    1183 <rb3_get_prevnext_ancestor+0x53>
mov    -0x10(%rbp),%rdi
call   1030 <rb3_get_parent@plt>
mov    %rax,-0x10(%rbp)
jmp    113f <rb3_get_prevnext_ancestor+0xf>
cmpq   $0x0,-0x10(%rbp)
je     11d3 <rb3_get_prevnext_ancestor+0xa3>
mov    -0x10(%rbp),%rdi
call   1030 <rb3_get_parent@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b9 <rb3_get_prevnext_ancestor+0x89>
mov    -0x10(%rbp),%rdi
call   1040 <rb3_is_base@plt>
cmp    $0x0,%rax
je     11c6 <rb3_get_prevnext_ancestor+0x96>
movq   $0x0,-0x8(%rbp)
jmp    11db <rb3_get_prevnext_ancestor+0xab>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11db <rb3_get_prevnext_ancestor+0xab>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rb3_get_prevnext_ancestor>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     118a <rb3_get_prevnext_ancestor+0x5a>
mov    %esi,%r15d
mov    %rdi,%rbx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1050 <rb3_get_parent_dir@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <rb3_get_parent@plt>
mov    %rax,%rbx
cmp    %r15d,%ebp
jne    1171 <rb3_get_prevnext_ancestor+0x41>
test   %rbx,%rbx
jne    1150 <rb3_get_prevnext_ancestor+0x20>
jmp    118d <rb3_get_prevnext_ancestor+0x5d>
test   %rbx,%rbx
je     118a <rb3_get_prevnext_ancestor+0x5a>
mov    %rbx,%rdi
call   1040 <rb3_is_base@plt>
xor    %r14d,%r14d
test   %rax,%rax
cmove  %rbx,%r14
jmp    118d <rb3_get_prevnext_ancestor+0x5d>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rb3_head = type { i32 }
define %struct.rb3_head* @rb3_get_prevnext_ancestor(%struct.rb3_head* %0, i32 %1) {
%3 = alloca %struct.rb3_head*
%4 = alloca %struct.rb3_head*
%5 = alloca i32
store %struct.rb3_head* %0, %struct.rb3_head** %4
store i32 %1, i32* %5
br label %6
6: ; preds = %16, %2
%7 = load %struct.rb3_head*, %struct.rb3_head** %4
%8 = icmp ne %struct.rb3_head* %7, null
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = load %struct.rb3_head*, %struct.rb3_head** %4
%11 = call i32 @rb3_get_parent_dir(%struct.rb3_head* %10)
%12 = load i32, i32* %5
%13 = icmp eq i32 %11, %12
br label %14
14: ; preds = %9, %6
%15 = phi i1 [ false, %6 ], [ %13, %9 ]
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = load %struct.rb3_head*, %struct.rb3_head** %4
%18 = call %struct.rb3_head* @rb3_get_parent(%struct.rb3_head* %17)
store %struct.rb3_head* %18, %struct.rb3_head** %4
br label %6
19: ; preds = %14
%20 = load %struct.rb3_head*, %struct.rb3_head** %4
%21 = icmp ne %struct.rb3_head* %20, null
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = load %struct.rb3_head*, %struct.rb3_head** %4
%24 = call %struct.rb3_head* @rb3_get_parent(%struct.rb3_head* %23)
store %struct.rb3_head* %24, %struct.rb3_head** %4
%25 = load %struct.rb3_head*, %struct.rb3_head** %4
%26 = icmp ne %struct.rb3_head* %25, null
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load %struct.rb3_head*, %struct.rb3_head** %4
%29 = call i64 @rb3_is_base(%struct.rb3_head* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27, %22
store %struct.rb3_head* null, %struct.rb3_head** %3
br label %35
32: ; preds = %27
%33 = load %struct.rb3_head*, %struct.rb3_head** %4
store %struct.rb3_head* %33, %struct.rb3_head** %3
br label %35
34: ; preds = %19
store %struct.rb3_head* null, %struct.rb3_head** %3
br label %35
35: ; preds = %34, %32, %31
%36 = load %struct.rb3_head*, %struct.rb3_head** %3
ret %struct.rb3_head* %36
}

LLVM IR (-O2):
%struct.rb3_head = type { i32 }
define %struct.rb3_head* @rb3_get_prevnext_ancestor(%struct.rb3_head* %0, i32 %1) {
%3 = icmp eq %struct.rb3_head* %0, null
br i1 %3, label %16, label %4
4: ; preds = %2, %10
%5 = phi %struct.rb3_head* [ %8, %10 ], [ %0, %2 ]
%6 = tail call i32 @rb3_get_parent_dir(%struct.rb3_head* %5)
%7 = icmp eq i32 %6, %1
%8 = tail call %struct.rb3_head* @rb3_get_parent(%struct.rb3_head* %5)
%9 = icmp eq %struct.rb3_head* %8, null
br i1 %7, label %10, label %11
10: ; preds = %4
br i1 %9, label %16, label %4
11: ; preds = %4
br i1 %9, label %16, label %12
12: ; preds = %11
%13 = tail call i64 @rb3_is_base(%struct.rb3_head* %8)
%14 = icmp eq i64 %13, 0
%15 = select i1 %14, %struct.rb3_head* %8, %struct.rb3_head* null
br label %16
16: ; preds = %10, %2, %12, %11
%17 = phi %struct.rb3_head* [ null, %11 ], [ %15, %12 ], [ null, %2 ], [ null, %10 ]
ret %struct.rb3_head* %17
}