#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 dat {
  int dummy;
};
typedef int /*<<< orphan*/ ca;

/* Variables and functions */
int find_newbase(struct dat *, int, unsigned char *, int);
int next_states(struct dat *, int, unsigned char *, int);
int /*<<< orphan*/ relocate(struct dat *, int, int, unsigned char *, int);
int resolve(struct dat *d, int s, unsigned char ch) {
  int cnt;
  int nbase;
  unsigned char ca[256];
  cnt = next_states(d, s, ca, sizeof(ca));
  ca[cnt++] = ch;
  nbase = find_newbase(d, s, ca, cnt);
  if (nbase < 0) {
    return -1;
  }
  relocate(d, s, nbase, ca, cnt);
  return nbase + ch;
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %al,-0x15(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x120(%rbp),%rdx
mov    $0x100,%ecx
call   1030 <next_states@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x15(%rbp),%cl
mov    -0x1c(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
cltq
mov    %cl,-0x120(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x120(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <find_newbase@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11a8 <resolve+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <resolve+0x9e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    -0x120(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
call   1050 <relocate@plt>
mov    -0x20(%rbp),%eax
movzbl -0x15(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%r12
mov    %rsp,%rbx
mov    %rbx,%rdx
mov    $0x100,%ecx
call   1030 <next_states@plt>
mov    %eax,%ebp
cltq
add    $0x1,%ebp
mov    %r14b,(%rsp,%rax,1)
mov    %r12,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1040 <find_newbase@plt>
test   %eax,%eax
js     1194 <resolve+0x64>
mov    %eax,%ebx
mov    %rsp,%rcx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %eax,%edx
mov    %ebp,%r8d
call   1050 <relocate@plt>
movzbl %r14b,%eax
add    %ebx,%eax
jmp    1199 <resolve+0x69>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dat = type { i32 }
define i32 @resolve(%struct.dat* %0, i32 %1, i8 zeroext %2) {
%4 = alloca i32
%5 = alloca %struct.dat*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
store %struct.dat* %0, %struct.dat** %5
store i32 %1, i32* %6
store i8 %2, i8* %7
%11 = load %struct.dat*, %struct.dat** %5
%12 = load i32, i32* %6
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%14 = call i32 @next_states(%struct.dat* %11, i32 %12, i8* %13, i32 256)
store i32 %14, i32* %8
%15 = load i8, i8* %7
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 1
store i32 %17, i32* %8
%18 = sext i32 %16 to i64
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %18
store i8 %15, i8* %19
%20 = load %struct.dat*, %struct.dat** %5
%21 = load i32, i32* %6
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%23 = load i32, i32* %8
%24 = call i32 @find_newbase(%struct.dat* %20, i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %3
store i32 -1, i32* %4
br label %39
28: ; preds = %3
%29 = load %struct.dat*, %struct.dat** %5
%30 = load i32, i32* %6
%31 = load i32, i32* %9
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%33 = load i32, i32* %8
%34 = call i32 @relocate(%struct.dat* %29, i32 %30, i32 %31, i8* %32, i32 %33)
%35 = load i32, i32* %9
%36 = load i8, i8* %7
%37 = zext i8 %36 to i32
%38 = add nsw i32 %35, %37
store i32 %38, i32* %4
br label %39
39: ; preds = %28, %27
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
%struct.dat = type { i32 }
define i32 @resolve(%struct.dat* %0, i32 %1, i8 zeroext %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i32 @next_states(%struct.dat* %0, i32 %1, i8* %5, i32 256)
%7 = add nsw i32 %6, 1
%8 = sext i32 %6 to i64
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %8
store i8 %2, i8* %9
%10 = call i32 @find_newbase(%struct.dat* %0, i32 %1, i8* %5, i32 %7)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %3
%13 = call i32 @relocate(%struct.dat* %0, i32 %1, i32 %10, i8* %5, i32 %7)
%14 = zext i8 %2 to i32
%15 = add nuw nsw i32 %10, %14
br label %16
16: ; preds = %3, %12
%17 = phi i32 [ %15, %12 ], [ -1, %3 ]
ret i32 %17
}