#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 data_getwonder(int);
int data_getwonderside(int);
int data_getwonderstages(int);
int data_getnextwonderstage(int p) {
  int side = data_getwonderside(p);
  int stage = data_getwonderstages(p);
  int wonder = data_getwonder(p);
  if (side == 0) {
    if (stage >= 3)
      return -1;
  } else {
    if (wonder == 3 && stage >= 2)
      return -1;
    if (wonder != 9 && stage >= 3)
      return -1;
    if (wonder == 9 && stage >= 4)
      return -1;
  }
  return side * 3 + 1 + stage;
}


x86 assembly (clang, -O0):
<data_getnextwonderstage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <data_getwonderside@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <data_getwonderstages@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <data_getwonder@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1181 <data_getnextwonderstage+0x51>
cmpl   $0x3,-0x10(%rbp)
jl     117c <data_getnextwonderstage+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <data_getnextwonderstage+0xc3>
jmp    11e6 <data_getnextwonderstage+0xb6>
cmpl   $0x3,-0x14(%rbp)
jne    11a1 <data_getnextwonderstage+0x71>
cmpl   $0x2,-0x10(%rbp)
jl     11a1 <data_getnextwonderstage+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <data_getnextwonderstage+0xc3>
cmpl   $0x9,-0x14(%rbp)
je     11c1 <data_getnextwonderstage+0x91>
cmpl   $0x3,-0x10(%rbp)
jl     11c1 <data_getnextwonderstage+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <data_getnextwonderstage+0xc3>
cmpl   $0x9,-0x14(%rbp)
jne    11e1 <data_getnextwonderstage+0xb1>
cmpl   $0x4,-0x10(%rbp)
jl     11e1 <data_getnextwonderstage+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <data_getnextwonderstage+0xc3>
jmp    11e6 <data_getnextwonderstage+0xb6>
imul   $0x3,-0xc(%rbp),%eax
add    $0x1,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<data_getnextwonderstage>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
call   1040 <data_getwonderside@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1030 <data_getwonderstages@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <data_getwonder@plt>
test   %r14d,%r14d
je     117a <data_getnextwonderstage+0x4a>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x3,%ecx
jne    1164 <data_getnextwonderstage+0x34>
cmp    $0x1,%ebx
jg     118d <data_getnextwonderstage+0x5d>
cmp    $0x9,%ecx
je     116e <data_getnextwonderstage+0x3e>
cmp    $0x2,%ebx
jg     118d <data_getnextwonderstage+0x5d>
cmp    $0x9,%ecx
jne    1184 <data_getnextwonderstage+0x54>
cmp    $0x3,%ebx
jg     118d <data_getnextwonderstage+0x5d>
jmp    1184 <data_getnextwonderstage+0x54>
mov    $0xffffffff,%eax
cmp    $0x2,%ebx
jg     118d <data_getnextwonderstage+0x5d>
lea    (%r14,%r14,2),%eax
add    %ebx,%eax
add    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @data_getnextwonderstage(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @data_getwonderside(i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %3
%10 = call i32 @data_getwonderstages(i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %3
%12 = call i32 @data_getwonder(i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %1
%16 = load i32, i32* %5
%17 = icmp sge i32 %16, 3
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %2
br label %48
19: ; preds = %15
br label %42
20: ; preds = %1
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 3
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* %5
%25 = icmp sge i32 %24, 2
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 -1, i32* %2
br label %48
27: ; preds = %23, %20
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, 9
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* %5
%32 = icmp sge i32 %31, 3
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 -1, i32* %2
br label %48
34: ; preds = %30, %27
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 9
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i32, i32* %5
%39 = icmp sge i32 %38, 4
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 -1, i32* %2
br label %48
41: ; preds = %37, %34
br label %42
42: ; preds = %41, %19
%43 = load i32, i32* %4
%44 = mul nsw i32 %43, 3
%45 = add nsw i32 %44, 1
%46 = load i32, i32* %5
%47 = add nsw i32 %45, %46
store i32 %47, i32* %2
br label %48
48: ; preds = %42, %40, %33, %26, %18
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
define i32 @data_getnextwonderstage(i32 %0) {
%2 = tail call i32 @data_getwonderside(i32 %0)
%3 = tail call i32 @data_getwonderstages(i32 %0)
%4 = tail call i32 @data_getwonder(i32 %0)
%5 = icmp eq i32 %2, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = icmp sgt i32 %3, 2
br i1 %7, label %24, label %20
8: ; preds = %1
%9 = icmp eq i32 %4, 3
%10 = icmp sgt i32 %3, 1
%11 = select i1 %9, i1 %10, i1 false
br i1 %11, label %24, label %12
12: ; preds = %8
%13 = icmp ne i32 %4, 9
%14 = icmp sgt i32 %3, 2
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %24, label %16
16: ; preds = %12
%17 = icmp eq i32 %4, 9
%18 = icmp sgt i32 %3, 3
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %24, label %20
20: ; preds = %16, %6
%21 = mul nsw i32 %2, 3
%22 = add nsw i32 %21, 1
%23 = add nsw i32 %22, %3
br label %24
24: ; preds = %16, %12, %8, %6, %20
%25 = phi i32 [ %23, %20 ], [ -1, %6 ], [ -1, %8 ], [ -1, %12 ], [ -1, %16 ]
ret i32 %25
}