#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 int /*<<< orphan*/ findElements;

/* Variables and functions */
int findRoot(int *, int, int);
int /*<<< orphan*/ printData(int *, int);
int /*<<< orphan*/ printf(char *, ...);
void testFindRoot(int data[], int size, int pathCompression) {
  int i;
  int findElements[] = {0, 1, 3, 7, 9, 11};
  int elementSize = sizeof(findElements) / sizeof(int);
  printf("----------Test FindRoot----------\n");
  printData(data, size);
  for (i = 0; i < elementSize; i++) {
    int root = findRoot(data, findElements[i], pathCompression);
    printf("Find by %s: %2d -> %2d\n",
           (pathCompression > 0) ? "path compression" : "normal",
           findElements[i], root);
    if (pathCompression > 0) {
      printData(data, size);
    }
  }
}


x86 assembly (clang, -O0):
<testFindRoot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0xeb7(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeb4(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xeb1(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x6,-0x34(%rbp)
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <printData@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1208 <testFindRoot+0xd8>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%esi
mov    -0x10(%rbp),%edx
call   1030 <findRoot@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%ecx
lea    0xeac(%rip),%rsi
lea    0xe94(%rip),%rax
cmp    $0x0,%ecx
cmovg  %rax,%rsi
movslq -0x14(%rbp),%rax
mov    -0x30(%rbp,%rax,4),%edx
mov    -0x38(%rbp),%ecx
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jle    11f5 <testFindRoot+0xc5>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <printData@plt>
jmp    11fa <testFindRoot+0xca>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118b <testFindRoot+0x5b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testFindRoot>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xeda(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <printData@plt>
test   %ebp,%ebp
lea    0xea9(%rip),%rax
lea    0xeb3(%rip),%r15
cmovg  %rax,%r15
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xe73(%rip),%rdi
mov    %r15,%rsi
xor    %edx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
test   %ebp,%ebp
jle    11de <testFindRoot+0x9e>
mov    %r14d,%esi
call   1060 <printData@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xe40(%rip),%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <printData@plt>
jmp    1202 <testFindRoot+0xc2>
mov    $0x1,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xe0f(%rip),%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xde8(%rip),%rdi
mov    %r15,%rsi
mov    $0x3,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
test   %ebp,%ebp
jle    126c <testFindRoot+0x12c>
mov    %r14d,%esi
call   1060 <printData@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xdb2(%rip),%rdi
mov    %r15,%rsi
mov    $0x7,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <printData@plt>
jmp    1290 <testFindRoot+0x150>
mov    $0x7,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xd81(%rip),%rdi
mov    %r15,%rsi
mov    $0x7,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xd5a(%rip),%rdi
mov    %r15,%rsi
mov    $0x9,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
jle    1302 <testFindRoot+0x1c2>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <printData@plt>
mov    %rbx,%rdi
mov    $0xb,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xd24(%rip),%rdi
mov    %r15,%rsi
mov    $0xb,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printData@plt>
mov    %rbx,%rdi
mov    $0xb,%esi
mov    %ebp,%edx
call   1040 <findRoot@plt>
lea    0xce8(%rip),%rdi
mov    %r15,%rsi
mov    $0xb,%edx
mov    %eax,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@__const.testFindRoot.findElements = [6 x i32] [i32 0, i32 1, i32 3, i32 7, i32 9, i32 11]
@.str = [35 x i8] c"----------Test FindRoot----------\0A\00"
@.str.1 = [24 x i8] c"Find by %s: %2d -> %2d\0A\00"
@.str.2 = [17 x i8] c"path compression\00"
@.str.3 = [7 x i8] c"normal\00"
define void @testFindRoot(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [6 x i32]
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = bitcast [6 x i32]* %8 to i8*
store i32 6, i32* %9
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%13 = load i32*, i32** %4
%14 = load i32, i32* %5
%15 = call i32 @printData(i32* %13, i32 %14)
store i32 0, i32* %7
br label %16
16: ; preds = %45, %3
%17 = load i32, i32* %7
%18 = load i32, i32* %9
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %48
20: ; preds = %16
%21 = load i32*, i32** %4
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 %23
%25 = load i32, i32* %24
%26 = load i32, i32* %6
%27 = call i32 @findRoot(i32* %21, i32 %25, i32 %26)
store i32 %27, i32* %10
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0)
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [6 x i32], [6 x i32]* %8, i64 0, i64 %33
%35 = load i32, i32* %34
%36 = load i32, i32* %10
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 %35, i32 %36)
%38 = load i32, i32* %6
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %20
%41 = load i32*, i32** %4
%42 = load i32, i32* %5
%43 = call i32 @printData(i32* %41, i32 %42)
br label %44
44: ; preds = %40, %20
br label %45
45: ; preds = %44
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %16
48: ; preds = %16
ret void
}

LLVM IR (-O2):
@.str.1 = [24 x i8] c"Find by %s: %2d -> %2d\0A\00"
@.str.2 = [17 x i8] c"path compression\00"
@.str.3 = [7 x i8] c"normal\00"
@str = [34 x i8] c"----------Test FindRoot----------\00"
define void @testFindRoot(i32* %0, i32 %1, i32 %2) {
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @printData(i32* %0, i32 %1)
%6 = icmp sgt i32 %2, 0
%7 = select i1 %6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0)
%8 = tail call i32 @findRoot(i32* %0, i32 0, i32 %2)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 0, i32 %8)
br i1 %6, label %10, label %15
10: ; preds = %3
%11 = tail call i32 @printData(i32* %0, i32 %1)
%12 = tail call i32 @findRoot(i32* %0, i32 1, i32 %2)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 1, i32 %12)
%14 = tail call i32 @printData(i32* %0, i32 %1)
br label %18
15: ; preds = %3
%16 = tail call i32 @findRoot(i32* %0, i32 1, i32 %2)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 1, i32 %16)
br label %18
18: ; preds = %15, %10
%19 = tail call i32 @findRoot(i32* %0, i32 3, i32 %2)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 3, i32 %19)
br i1 %6, label %21, label %26
21: ; preds = %18
%22 = tail call i32 @printData(i32* %0, i32 %1)
%23 = tail call i32 @findRoot(i32* %0, i32 7, i32 %2)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 7, i32 %23)
%25 = tail call i32 @printData(i32* %0, i32 %1)
br label %29
26: ; preds = %18
%27 = tail call i32 @findRoot(i32* %0, i32 7, i32 %2)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 7, i32 %27)
br label %29
29: ; preds = %26, %21
%30 = tail call i32 @findRoot(i32* %0, i32 9, i32 %2)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 9, i32 %30)
br i1 %6, label %32, label %37
32: ; preds = %29
%33 = tail call i32 @printData(i32* %0, i32 %1)
%34 = tail call i32 @findRoot(i32* %0, i32 11, i32 %2)
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 11, i32 %34)
%36 = tail call i32 @printData(i32* %0, i32 %1)
br label %40
37: ; preds = %29
%38 = tail call i32 @findRoot(i32* %0, i32 11, i32 %2)
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 11, i32 %38)
br label %40
40: ; preds = %37, %32
ret void
}