#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 State_q {
  int dummy;
};

/* Variables and functions */
scalar_t__ state_q_is_empty(struct State_q *);
int cycle(struct State_q **qs, int ranks, int last) {
  if (last == -1)
    last = 0;
  int i = (last + 1) % ranks;
  while (i != last && state_q_is_empty(qs[i]))
    i = (i + 1) % ranks;
  if (i == last && state_q_is_empty(qs[i]))
    return -1;
  return i;
}


x86 assembly (clang, -O0):
<cycle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1133 <cycle+0x23>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x1d(%rbp)
je     116c <cycle+0x5c>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <state_q_is_empty@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    117c <cycle+0x6c>
jmp    118e <cycle+0x7e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltd
idivl  -0x14(%rbp)
mov    %edx,-0x1c(%rbp)
jmp    1140 <cycle+0x30>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    11c1 <cycle+0xb1>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <state_q_is_empty@plt>
cmp    $0x0,%rax
je     11c1 <cycle+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <cycle+0xb7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cycle>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebp,%ebp
cmp    $0xffffffff,%edx
cmovne %edx,%ebp
mov    %ebp,%ebx
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    %ebx,%eax
cltd
idiv   %r15d
cmp    %ebp,%edx
je     1152 <cycle+0x42>
mov    %edx,%ebx
movslq %edx,%rax
mov    (%r14,%rax,8),%rdi
call   1030 <state_q_is_empty@plt>
test   %rax,%rax
jne    1130 <cycle+0x20>
jmp    116d <cycle+0x5d>
movslq %ebp,%rax
mov    (%r14,%rax,8),%rdi
call   1030 <state_q_is_empty@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
mov    %ebp,%ebx
test   %rcx,%rcx
jne    116f <cycle+0x5f>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.State_q = type { i32 }
define i32 @cycle(%struct.State_q** %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.State_q**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.State_q** %0, %struct.State_q*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 0, i32* %7
br label %12
12: ; preds = %11, %3
%13 = load i32, i32* %7
%14 = add nsw i32 %13, 1
%15 = load i32, i32* %6
%16 = srem i32 %14, %15
store i32 %16, i32* %8
br label %17
17: ; preds = %31, %12
%18 = load i32, i32* %8
%19 = load i32, i32* %7
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = load %struct.State_q**, %struct.State_q*** %5
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds %struct.State_q*, %struct.State_q** %22, i64 %24
%26 = load %struct.State_q*, %struct.State_q** %25
%27 = call i64 @state_q_is_empty(%struct.State_q* %26)
%28 = icmp ne i64 %27, 0
br label %29
29: ; preds = %21, %17
%30 = phi i1 [ false, %17 ], [ %28, %21 ]
br i1 %30, label %31, label %36
31: ; preds = %29
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
%34 = load i32, i32* %6
%35 = srem i32 %33, %34
store i32 %35, i32* %8
br label %17
36: ; preds = %29
%37 = load i32, i32* %8
%38 = load i32, i32* %7
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %49
40: ; preds = %36
%41 = load %struct.State_q**, %struct.State_q*** %5
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds %struct.State_q*, %struct.State_q** %41, i64 %43
%45 = load %struct.State_q*, %struct.State_q** %44
%46 = call i64 @state_q_is_empty(%struct.State_q* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %40
store i32 -1, i32* %4
br label %51
49: ; preds = %40, %36
%50 = load i32, i32* %8
store i32 %50, i32* %4
br label %51
51: ; preds = %49, %48
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
%struct.State_q = type { i32 }
define i32 @cycle(%struct.State_q** nocapture %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, -1
%5 = select i1 %4, i32 0, i32 %2
br label %6
6: ; preds = %11, %3
%7 = phi i32 [ %5, %3 ], [ %9, %11 ]
%8 = add nsw i32 %7, 1
%9 = srem i32 %8, %1
%10 = icmp eq i32 %9, %5
br i1 %10, label %17, label %11
11: ; preds = %6
%12 = sext i32 %9 to i64
%13 = getelementptr inbounds %struct.State_q*, %struct.State_q** %0, i64 %12
%14 = load %struct.State_q*, %struct.State_q** %13
%15 = tail call i64 @state_q_is_empty(%struct.State_q* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %6
17: ; preds = %6
%18 = sext i32 %5 to i64
%19 = getelementptr inbounds %struct.State_q*, %struct.State_q** %0, i64 %18
%20 = load %struct.State_q*, %struct.State_q** %19
%21 = tail call i64 @state_q_is_empty(%struct.State_q* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %11, %17
%24 = phi i32 [ %5, %17 ], [ %9, %11 ]
br label %25
25: ; preds = %17, %23
%26 = phi i32 [ %24, %23 ], [ -1, %17 ]
ret i32 %26
}