#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_child(struct rb3_head *, int);
scalar_t__ rb3_has_child(struct rb3_head *, int);
struct rb3_head *rb3_get_minmax_in_subtree(struct rb3_head *head, int dir) {
  if (!head)
    return NULL;
  while (rb3_has_child(head, dir))
    head = rb3_get_child(head, dir);
  return head;
}


x86 assembly (clang, -O0):
<rb3_get_minmax_in_subtree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1147 <rb3_get_minmax_in_subtree+0x27>
movq   $0x0,-0x8(%rbp)
jmp    117f <rb3_get_minmax_in_subtree+0x5f>
jmp    114c <rb3_get_minmax_in_subtree+0x2c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <rb3_has_child@plt>
cmp    $0x0,%rax
je     1177 <rb3_get_minmax_in_subtree+0x57>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <rb3_get_child@plt>
mov    %rax,-0x10(%rbp)
jmp    114c <rb3_get_minmax_in_subtree+0x2c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rb3_get_minmax_in_subtree>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     114e <rb3_get_minmax_in_subtree+0x2e>
mov    %esi,%ebp
mov    %rdi,%rbx
nopl   (%rax)
mov    %ebp,%esi
call   1040 <rb3_has_child@plt>
test   %rax,%rax
je     1150 <rb3_get_minmax_in_subtree+0x30>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <rb3_get_child@plt>
mov    %rax,%rbx
mov    %rax,%rdi
jmp    1130 <rb3_get_minmax_in_subtree+0x10>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.rb3_head = type { i32 }
define %struct.rb3_head* @rb3_get_minmax_in_subtree(%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
%6 = load %struct.rb3_head*, %struct.rb3_head** %4
%7 = icmp ne %struct.rb3_head* %6, null
br i1 %7, label %9, label %8
8: ; preds = %2
store %struct.rb3_head* null, %struct.rb3_head** %3
br label %21
9: ; preds = %2
br label %10
10: ; preds = %15, %9
%11 = load %struct.rb3_head*, %struct.rb3_head** %4
%12 = load i32, i32* %5
%13 = call i64 @rb3_has_child(%struct.rb3_head* %11, i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load %struct.rb3_head*, %struct.rb3_head** %4
%17 = load i32, i32* %5
%18 = call %struct.rb3_head* @rb3_get_child(%struct.rb3_head* %16, i32 %17)
store %struct.rb3_head* %18, %struct.rb3_head** %4
br label %10
19: ; preds = %10
%20 = load %struct.rb3_head*, %struct.rb3_head** %4
store %struct.rb3_head* %20, %struct.rb3_head** %3
br label %21
21: ; preds = %19, %8
%22 = load %struct.rb3_head*, %struct.rb3_head** %3
ret %struct.rb3_head* %22
}

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