#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 radix_tree_root {
  int dummy;
};
typedef void radix_tree_node;

/* Variables and functions */
int /*<<< orphan*/ BUG_ON(int);
void *entry_to_node(void *);
unsigned int radix_tree_descend(void *, void **, unsigned long);
scalar_t__ radix_tree_is_internal_node(void *);
int /*<<< orphan*/ radix_tree_load_root(struct radix_tree_root *, void **,
                                        unsigned long *);
int /*<<< orphan*/ root_tag_get(struct radix_tree_root *, unsigned int);
int /*<<< orphan*/ root_tag_set(struct radix_tree_root *, unsigned int);
int /*<<< orphan*/ tag_get(void *, unsigned int, unsigned int);
int /*<<< orphan*/ tag_set(void *, unsigned int, unsigned int);
void *radix_tree_tag_set(struct radix_tree_root *root, unsigned long index,
                         unsigned int tag) {
  struct radix_tree_node *node, *parent;
  unsigned long maxindex;
  radix_tree_load_root(root, &node, &maxindex);
  BUG_ON(index > maxindex);
  while (radix_tree_is_internal_node(node)) {
    unsigned offset;
    parent = entry_to_node(node);
    offset = radix_tree_descend(parent, &node, index);
    BUG_ON(!node);
    if (!tag_get(parent, tag, offset))
      tag_set(parent, tag, offset);
  }
  if (!root_tag_get(root, tag))
    root_tag_set(root, tag);
  return node;
}


x86 assembly (clang, -O0):
<radix_tree_tag_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10b0 <radix_tree_load_root@plt>
mov    -0x10(%rbp),%rax
cmp    -0x30(%rbp),%rax
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <BUG_ON@plt>
mov    -0x20(%rbp),%rdi
call   1070 <radix_tree_is_internal_node@plt>
cmp    $0x0,%rax
je     123d <radix_tree_tag_set+0xad>
mov    -0x20(%rbp),%rdi
call   1060 <entry_to_node@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <radix_tree_descend@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <BUG_ON@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x34(%rbp),%edx
call   10a0 <tag_get@plt>
cmp    $0x0,%eax
jne    1238 <radix_tree_tag_set+0xa8>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1080 <tag_set@plt>
jmp    11c9 <radix_tree_tag_set+0x39>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <root_tag_get@plt>
cmp    $0x0,%eax
jne    125e <radix_tree_tag_set+0xce>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <root_tag_set@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<radix_tree_tag_set>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   10b0 <radix_tree_load_root@plt>
xor    %edi,%edi
cmp    %r15,0x10(%rsp)
setb   %dil
call   1050 <BUG_ON@plt>
mov    0x8(%rsp),%rdi
call   1070 <radix_tree_is_internal_node@plt>
test   %rax,%rax
je     123d <radix_tree_tag_set+0xad>
lea    0x8(%rsp),%r12
jmp    11ef <radix_tree_tag_set+0x5f>
nopl   0x0(%rax)
mov    0x8(%rsp),%rdi
call   1070 <radix_tree_is_internal_node@plt>
test   %rax,%rax
je     123d <radix_tree_tag_set+0xad>
mov    0x8(%rsp),%rdi
call   1060 <entry_to_node@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <radix_tree_descend@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmpq   $0x0,0x8(%rsp)
sete   %dil
call   1050 <BUG_ON@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %ebp,%edx
call   10a0 <tag_get@plt>
test   %eax,%eax
jne    11e0 <radix_tree_tag_set+0x50>
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %ebp,%edx
call   1080 <tag_set@plt>
jmp    11e0 <radix_tree_tag_set+0x50>
mov    %r14,%rdi
mov    %r13d,%esi
call   1040 <root_tag_get@plt>
test   %eax,%eax
jne    1257 <radix_tree_tag_set+0xc7>
mov    %r14,%rdi
mov    %r13d,%esi
call   1090 <root_tag_set@plt>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.radix_tree_root = type { i32 }
%struct.radix_tree_node = type opaque
define i8* @radix_tree_tag_set(%struct.radix_tree_root* %0, i64 %1, i32 %2) {
%4 = alloca %struct.radix_tree_root*
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.radix_tree_node*
%8 = alloca %struct.radix_tree_node*
%9 = alloca i64
%10 = alloca i32
store %struct.radix_tree_root* %0, %struct.radix_tree_root** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%11 = load %struct.radix_tree_root*, %struct.radix_tree_root** %4
%12 = bitcast %struct.radix_tree_node** %7 to i8**
%13 = call i32 @radix_tree_load_root(%struct.radix_tree_root* %11, i8** %12, i64* %9)
%14 = load i64, i64* %5
%15 = load i64, i64* %9
%16 = icmp ugt i64 %14, %15
%17 = zext i1 %16 to i32
%18 = call i32 @BUG_ON(i32 %17)
br label %19
19: ; preds = %51, %3
%20 = load %struct.radix_tree_node*, %struct.radix_tree_node** %7
%21 = bitcast %struct.radix_tree_node* %20 to i8*
%22 = call i64 @radix_tree_is_internal_node(i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %52
24: ; preds = %19
%25 = load %struct.radix_tree_node*, %struct.radix_tree_node** %7
%26 = bitcast %struct.radix_tree_node* %25 to i8*
%27 = call i8* @entry_to_node(i8* %26)
%28 = bitcast i8* %27 to %struct.radix_tree_node*
store %struct.radix_tree_node* %28, %struct.radix_tree_node** %8
%29 = load %struct.radix_tree_node*, %struct.radix_tree_node** %8
%30 = bitcast %struct.radix_tree_node* %29 to i8*
%31 = bitcast %struct.radix_tree_node** %7 to i8**
%32 = load i64, i64* %5
%33 = call i32 @radix_tree_descend(i8* %30, i8** %31, i64 %32)
store i32 %33, i32* %10
%34 = load %struct.radix_tree_node*, %struct.radix_tree_node** %7
%35 = icmp ne %struct.radix_tree_node* %34, null
%36 = xor i1 %35, true
%37 = zext i1 %36 to i32
%38 = call i32 @BUG_ON(i32 %37)
%39 = load %struct.radix_tree_node*, %struct.radix_tree_node** %8
%40 = bitcast %struct.radix_tree_node* %39 to i8*
%41 = load i32, i32* %6
%42 = load i32, i32* %10
%43 = call i32 @tag_get(i8* %40, i32 %41, i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %24
%46 = load %struct.radix_tree_node*, %struct.radix_tree_node** %8
%47 = bitcast %struct.radix_tree_node* %46 to i8*
%48 = load i32, i32* %6
%49 = load i32, i32* %10
%50 = call i32 @tag_set(i8* %47, i32 %48, i32 %49)
br label %51
51: ; preds = %45, %24
br label %19
52: ; preds = %19
%53 = load %struct.radix_tree_root*, %struct.radix_tree_root** %4
%54 = load i32, i32* %6
%55 = call i32 @root_tag_get(%struct.radix_tree_root* %53, i32 %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %52
%58 = load %struct.radix_tree_root*, %struct.radix_tree_root** %4
%59 = load i32, i32* %6
%60 = call i32 @root_tag_set(%struct.radix_tree_root* %58, i32 %59)
br label %61
61: ; preds = %57, %52
%62 = load %struct.radix_tree_node*, %struct.radix_tree_node** %7
%63 = bitcast %struct.radix_tree_node* %62 to i8*
ret i8* %63
}

LLVM IR (-O2):
%struct.radix_tree_root = type { i32 }
%struct.radix_tree_node = type opaque
define i8* @radix_tree_tag_set(%struct.radix_tree_root* %0, i64 %1, i32 %2) {
%4 = alloca %struct.radix_tree_node*
%5 = alloca i64
%6 = bitcast %struct.radix_tree_node** %4 to i8*
%7 = bitcast i64* %5 to i8*
%8 = bitcast %struct.radix_tree_node** %4 to i8**
%9 = call i32 @radix_tree_load_root(%struct.radix_tree_root* %0, i8** %8, i64* %5)
%10 = load i64, i64* %5
%11 = icmp ult i64 %10, %1
%12 = zext i1 %11 to i32
%13 = call i32 @BUG_ON(i32 %12)
%14 = load i8*, i8** %8
%15 = call i64 @radix_tree_is_internal_node(i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %33, label %17
17: ; preds = %3, %29
%18 = load i8*, i8** %8
%19 = call i8* @entry_to_node(i8* %18)
%20 = call i32 @radix_tree_descend(i8* %19, i8** %8, i64 %1)
%21 = load %struct.radix_tree_node*, %struct.radix_tree_node** %4
%22 = icmp eq %struct.radix_tree_node* %21, null
%23 = zext i1 %22 to i32
%24 = call i32 @BUG_ON(i32 %23)
%25 = call i32 @tag_get(i8* %19, i32 %2, i32 %20)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = call i32 @tag_set(i8* %19, i32 %2, i32 %20)
br label %29
29: ; preds = %27, %17
%30 = load i8*, i8** %8
%31 = call i64 @radix_tree_is_internal_node(i8* %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %17
33: ; preds = %29, %3
%34 = call i32 @root_tag_get(%struct.radix_tree_root* %0, i32 %2)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @root_tag_set(%struct.radix_tree_root* %0, i32 %2)
br label %38
38: ; preds = %36, %33
%39 = load i8*, i8** %8
ret i8* %39
}