#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 */
scalar_t__ a_button();
scalar_t__ b_button();
scalar_t__ c_button();
int /*<<< orphan*/ display_printf(int /*<<< orphan*/, int, char *);
int /*<<< orphan*/ msleep(int);
int selection_list(int length) {
  int current = 0;
  int counter = 1;
  int oldcurrent = 0;
  while (1) {
    if (a_button()) {
      while (a_button())
        msleep(1);
      return current;
    }
    if (c_button()) {
      while (c_button())
        msleep(1);
      oldcurrent = current;
      current--;
      counter = 1;
      if (current < 0)
        current = length - 1;
    }
    if (b_button()) {
      while (b_button())
        msleep(1);
      oldcurrent = current;
      current++;
      counter = 1;
      if (current >= length)
        current = 0;
    }
    if (counter == 1) {
      display_printf(0, oldcurrent + 1, " ");
      display_printf(0, current + 1, "*");
      counter = 0;
    }
  }
}


x86 assembly (clang, -O0):
<selection_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1030 <a_button@plt>
cmp    $0x0,%rax
je     11af <selection_list+0x5f>
jmp    1186 <selection_list+0x36>
mov    $0x0,%al
call   1030 <a_button@plt>
cmp    $0x0,%rax
je     11a6 <selection_list+0x56>
mov    $0x1,%edi
call   1050 <msleep@plt>
jmp    1186 <selection_list+0x36>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   1040 <c_button@plt>
cmp    $0x0,%rax
je     1213 <selection_list+0xc3>
jmp    11c5 <selection_list+0x75>
mov    $0x0,%al
call   1040 <c_button@plt>
cmp    $0x0,%rax
je     11e5 <selection_list+0x95>
mov    $0x1,%edi
call   1050 <msleep@plt>
jmp    11c5 <selection_list+0x75>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    120e <selection_list+0xbe>
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1213 <selection_list+0xc3>
mov    $0x0,%al
call   1070 <b_button@plt>
cmp    $0x0,%rax
je     1277 <selection_list+0x127>
jmp    1229 <selection_list+0xd9>
mov    $0x0,%al
call   1070 <b_button@plt>
cmp    $0x0,%rax
je     1249 <selection_list+0xf9>
mov    $0x1,%edi
call   1050 <msleep@plt>
jmp    1229 <selection_list+0xd9>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     1272 <selection_list+0x122>
movl   $0x0,-0x8(%rbp)
jmp    1277 <selection_list+0x127>
cmpl   $0x1,-0xc(%rbp)
jne    12b0 <selection_list+0x160>
mov    -0x10(%rbp),%esi
add    $0x1,%esi
xor    %edi,%edi
lea    0xd70(%rip),%rdx
call   1060 <display_printf@plt>
mov    -0x8(%rbp),%esi
add    $0x1,%esi
xor    %edi,%edi
lea    0xd5e(%rip),%rdx
call   1060 <display_printf@plt>
movl   $0x0,-0xc(%rbp)
jmp    1170 <selection_list+0x20>

x86 assembly (clang, -O2):
<selection_list>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r12d
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <a_button@plt>
test   %rax,%rax
jne    125a <selection_list+0x10a>
mov    $0x1,%r14b
lea    0xe88(%rip),%r15
xor    %r13d,%r13d
xor    %ebp,%ebp
nop
xor    %eax,%eax
call   1040 <c_button@plt>
test   %rax,%rax
je     11d0 <selection_list+0x80>
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <c_button@plt>
test   %rax,%rax
je     11b0 <selection_list+0x60>
mov    $0x1,%edi
call   1050 <msleep@plt>
jmp    1190 <selection_list+0x40>
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
mov    %ebp,%ebx
cmovle %r12d,%ebx
add    $0xffffffff,%ebx
xor    %eax,%eax
call   1070 <b_button@plt>
mov    %ebp,%r13d
test   %rax,%rax
jne    11ea <selection_list+0x9a>
jmp    120e <selection_list+0xbe>
nopl   0x0(%rax)
xor    %eax,%eax
call   1070 <b_button@plt>
test   %rax,%rax
je     1206 <selection_list+0xb6>
mov    %ebp,%ebx
jmp    11ea <selection_list+0x9a>
mov    $0x1,%edi
call   1050 <msleep@plt>
xor    %eax,%eax
call   1070 <b_button@plt>
test   %rax,%rax
jne    11e0 <selection_list+0x90>
lea    0x1(%rbx),%esi
cmp    %r12d,%esi
mov    $0x0,%ebp
cmovl  %esi,%ebp
jmp    1217 <selection_list+0xc7>
mov    %ebp,%ebx
test   $0x1,%r14b
je     1235 <selection_list+0xe5>
mov    %ebx,%ebp
lea    0x1(%r13),%esi
mov    %r13d,%ebx
xor    %edi,%edi
lea    0xde0(%rip),%rdx
call   1060 <display_printf@plt>
lea    0x1(%rbp),%esi
xor    %edi,%edi
mov    %r15,%rdx
call   1060 <display_printf@plt>
mov    %ebx,%r13d
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <a_button@plt>
test   %rax,%rax
je     1180 <selection_list+0x30>
jmp    125a <selection_list+0x10a>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1050 <msleep@plt>
xor    %eax,%eax
call   1030 <a_button@plt>
test   %rax,%rax
jne    1250 <selection_list+0x100>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [2 x i8] c"*\00"
define i32 @selection_list(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 1, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %1, %64
%7 = call i64 (...) @a_button()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %6
br label %10
10: ; preds = %13, %9
%11 = call i64 (...) @a_button()
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @msleep(i32 1)
br label %10
15: ; preds = %10
%16 = load i32, i32* %3
ret i32 %16
17: ; preds = %6
%18 = call i64 (...) @c_button()
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %36
20: ; preds = %17
br label %21
21: ; preds = %24, %20
%22 = call i64 (...) @c_button()
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @msleep(i32 1)
br label %21
26: ; preds = %21
%27 = load i32, i32* %3
store i32 %27, i32* %5
%28 = load i32, i32* %3
%29 = add nsw i32 %28, -1
store i32 %29, i32* %3
store i32 1, i32* %4
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* %2
%34 = sub nsw i32 %33, 1
store i32 %34, i32* %3
br label %35
35: ; preds = %32, %26
br label %36
36: ; preds = %35, %17
%37 = call i64 (...) @b_button()
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %36
br label %40
40: ; preds = %43, %39
%41 = call i64 (...) @b_button()
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @msleep(i32 1)
br label %40
45: ; preds = %40
%46 = load i32, i32* %3
store i32 %46, i32* %5
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
store i32 1, i32* %4
%49 = load i32, i32* %3
%50 = load i32, i32* %2
%51 = icmp sge i32 %49, %50
br i1 %51, label %52, label %53
52: ; preds = %45
store i32 0, i32* %3
br label %53
53: ; preds = %52, %45
br label %54
54: ; preds = %53, %36
%55 = load i32, i32* %4
%56 = icmp eq i32 %55, 1
br i1 %56, label %57, label %64
57: ; preds = %54
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
%60 = call i32 @display_printf(i32 0, i32 %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%61 = load i32, i32* %3
%62 = add nsw i32 %61, 1
%63 = call i32 @display_printf(i32 0, i32 %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %64
64: ; preds = %57, %54
br label %6
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [2 x i8] c"*\00"
define i32 @selection_list(i32 %0) {
%2 = tail call i64 (...) @a_button()
%3 = icmp eq i64 %2, 0
br i1 %3, label %13, label %4
4: ; preds = %59, %1
%5 = phi i32 [ 0, %1 ], [ %61, %59 ]
%6 = tail call i64 (...) @a_button()
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4, %8
%9 = tail call i32 @msleep(i32 1)
%10 = tail call i64 (...) @a_button()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %8
12: ; preds = %8, %4
ret i32 %5
13: ; preds = %1, %59
%14 = phi i32 [ %60, %59 ], [ 0, %1 ]
%15 = phi i1 [ false, %59 ], [ true, %1 ]
%16 = phi i32 [ %61, %59 ], [ 0, %1 ]
%17 = tail call i64 (...) @c_button()
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %13
%20 = tail call i64 (...) @c_button()
%21 = icmp eq i64 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %19, %22
%23 = tail call i32 @msleep(i32 1)
%24 = tail call i64 (...) @c_button()
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %22
26: ; preds = %13
%27 = tail call i64 (...) @b_button()
%28 = icmp eq i64 %27, 0
br i1 %28, label %47, label %35
29: ; preds = %22, %19
%30 = icmp slt i32 %16, 1
%31 = select i1 %30, i32 %0, i32 %16
%32 = add nsw i32 %31, -1
%33 = tail call i64 (...) @b_button()
%34 = icmp eq i64 %33, 0
br i1 %34, label %48, label %35
35: ; preds = %29, %26
%36 = phi i32 [ %32, %29 ], [ %16, %26 ]
%37 = tail call i64 (...) @b_button()
%38 = icmp eq i64 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %35, %39
%40 = tail call i32 @msleep(i32 1)
%41 = tail call i64 (...) @b_button()
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %39
43: ; preds = %39, %35
%44 = add nsw i32 %36, 1
%45 = icmp slt i32 %44, %0
%46 = select i1 %45, i32 %44, i32 0
br label %52
47: ; preds = %26
br i1 %15, label %48, label %59
48: ; preds = %29, %47
%49 = phi i32 [ %16, %47 ], [ %32, %29 ]
%50 = phi i32 [ %14, %47 ], [ %16, %29 ]
%51 = add nsw i32 %50, 1
br label %52
52: ; preds = %48, %43
%53 = phi i32 [ %51, %48 ], [ %44, %43 ]
%54 = phi i32 [ %50, %48 ], [ %36, %43 ]
%55 = phi i32 [ %49, %48 ], [ %46, %43 ]
%56 = tail call i32 @display_printf(i32 0, i32 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%57 = add nsw i32 %55, 1
%58 = tail call i32 @display_printf(i32 0, i32 %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %59
59: ; preds = %52, %47
%60 = phi i32 [ %54, %52 ], [ %14, %47 ]
%61 = phi i32 [ %55, %52 ], [ %16, %47 ]
%62 = tail call i64 (...) @a_button()
%63 = icmp eq i64 %62, 0
br i1 %63, label %13, label %4
}