#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 */

/* Variables and functions */
int roots_find(char *);
int roots_search(char *q) {
  int r = 0;
  for (;;) {
    if ((r = roots_find(q)) >= 0)
      return r;
    if (!*q)
      return -1;
    q = q + *q;
    q++;
  }
}


x86 assembly (clang, -O0):
<roots_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <roots_find@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     1143 <roots_search+0x33>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1182 <roots_search+0x72>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    115c <roots_search+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1182 <roots_search+0x72>
mov    -0x10(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <roots_search+0x13>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<roots_search>:
push   %rbx
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1030 <roots_find@plt>
test   %eax,%eax
jns    1143 <roots_search+0x33>
movsbq (%rbx),%rax
test   %rax,%rax
je     113e <roots_search+0x2e>
add    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
jmp    1120 <roots_search+0x10>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @roots_search(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
br label %5
5: ; preds = %16, %1
%6 = load i8*, i8** %3
%7 = call i32 @roots_find(i8* %6)
store i32 %7, i32* %4
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = load i32, i32* %4
store i32 %10, i32* %2
br label %25
11: ; preds = %5
%12 = load i8*, i8** %3
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %11
store i32 -1, i32* %2
br label %25
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %17, i64 %21
store i8* %22, i8** %3
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %3
br label %5
25: ; preds = %15, %9
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
define i32 @roots_search(i8* %0) {
%2 = tail call i32 @roots_find(i8* %0)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %14, label %4
4: ; preds = %1, %8
%5 = phi i8* [ %11, %8 ], [ %0, %1 ]
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %4
%9 = sext i8 %6 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 1
%11 = getelementptr inbounds i8, i8* %10, i64 %9
%12 = tail call i32 @roots_find(i8* %11)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %4
14: ; preds = %8, %4, %1
%15 = phi i32 [ %2, %1 ], [ -1, %4 ], [ %12, %8 ]
ret i32 %15
}