#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 void rb3_head;

/* Variables and functions */
void *rb3_get_parent(void *);
int rb3_get_parent_dir(void *);
scalar_t__ rb3_is_base(void *);
extern 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 ((void *)0);
    return head;
  }
  return ((void *)0);
}


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   %r14
push   %rbx
test   %rdi,%rdi
je     118f <rb3_get_prevnext_ancestor+0x5f>
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <rb3_get_parent_dir@plt>
mov    %rbx,%rdi
cmp    %ebp,%eax
jne    116e <rb3_get_prevnext_ancestor+0x3e>
call   1030 <rb3_get_parent@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1150 <rb3_get_prevnext_ancestor+0x20>
jmp    1192 <rb3_get_prevnext_ancestor+0x62>
call   1030 <rb3_get_parent@plt>
test   %rax,%rax
je     118f <rb3_get_prevnext_ancestor+0x5f>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <rb3_is_base@plt>
xor    %r14d,%r14d
test   %rax,%rax
cmove  %rbx,%r14
jmp    1192 <rb3_get_prevnext_ancestor+0x62>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rb3_head = type opaque
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 = %17, %2
%7 = load %struct.rb3_head*, %struct.rb3_head** %4
%8 = icmp ne %struct.rb3_head* %7, null
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load %struct.rb3_head*, %struct.rb3_head** %4
%11 = bitcast %struct.rb3_head* %10 to i8*
%12 = call i32 @rb3_get_parent_dir(i8* %11)
%13 = load i32, i32* %5
%14 = icmp eq i32 %12, %13
br label %15
15: ; preds = %9, %6
%16 = phi i1 [ false, %6 ], [ %14, %9 ]
br i1 %16, label %17, label %22
17: ; preds = %15
%18 = load %struct.rb3_head*, %struct.rb3_head** %4
%19 = bitcast %struct.rb3_head* %18 to i8*
%20 = call i8* @rb3_get_parent(i8* %19)
%21 = bitcast i8* %20 to %struct.rb3_head*
store %struct.rb3_head* %21, %struct.rb3_head** %4
br label %6
22: ; preds = %15
%23 = load %struct.rb3_head*, %struct.rb3_head** %4
%24 = icmp ne %struct.rb3_head* %23, null
br i1 %24, label %25, label %40
25: ; preds = %22
%26 = load %struct.rb3_head*, %struct.rb3_head** %4
%27 = bitcast %struct.rb3_head* %26 to i8*
%28 = call i8* @rb3_get_parent(i8* %27)
%29 = bitcast i8* %28 to %struct.rb3_head*
store %struct.rb3_head* %29, %struct.rb3_head** %4
%30 = load %struct.rb3_head*, %struct.rb3_head** %4
%31 = icmp ne %struct.rb3_head* %30, null
br i1 %31, label %32, label %37
32: ; preds = %25
%33 = load %struct.rb3_head*, %struct.rb3_head** %4
%34 = bitcast %struct.rb3_head* %33 to i8*
%35 = call i64 @rb3_is_base(i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %32, %25
store %struct.rb3_head* null, %struct.rb3_head** %3
br label %41
38: ; preds = %32
%39 = load %struct.rb3_head*, %struct.rb3_head** %4
store %struct.rb3_head* %39, %struct.rb3_head** %3
br label %41
40: ; preds = %22
store %struct.rb3_head* null, %struct.rb3_head** %3
br label %41
41: ; preds = %40, %38, %37
%42 = load %struct.rb3_head*, %struct.rb3_head** %3
ret %struct.rb3_head* %42
}

LLVM IR (-O2):
%struct.rb3_head = type opaque
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 %22, label %4
4: ; preds = %2, %9
%5 = phi %struct.rb3_head* [ %11, %9 ], [ %0, %2 ]
%6 = bitcast %struct.rb3_head* %5 to i8*
%7 = tail call i32 @rb3_get_parent_dir(i8* %6)
%8 = icmp eq i32 %7, %1
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = tail call i8* @rb3_get_parent(i8* %6)
%11 = bitcast i8* %10 to %struct.rb3_head*
%12 = icmp eq i8* %10, null
br i1 %12, label %22, label %4
13: ; preds = %4
%14 = bitcast %struct.rb3_head* %5 to i8*
%15 = tail call i8* @rb3_get_parent(i8* %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = bitcast i8* %15 to %struct.rb3_head*
%19 = tail call i64 @rb3_is_base(i8* %15)
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, %struct.rb3_head* %18, %struct.rb3_head* null
br label %22
22: ; preds = %9, %2, %17, %13
%23 = phi %struct.rb3_head* [ null, %13 ], [ %21, %17 ], [ null, %2 ], [ null, %9 ]
ret %struct.rb3_head* %23
}