#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 Joystick(int);
int JoystickButton(int player_nr) {
  static int last_joy_button[4] = {0, 0, 0, 0};
  int joy_button = (Joystick(player_nr) & ((1 << 4) | (1 << 5)));
  int result;
  if (joy_button) {
    if (last_joy_button[player_nr])
      result = 1;
    else
      result = 2;
  } else {
    if (last_joy_button[player_nr])
      result = 3;
    else
      result = 0;
  }
  last_joy_button[player_nr] = joy_button;
  return result;
}


x86 assembly (clang, -O0):
<JoystickButton>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <Joystick@plt>
and    $0x30,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1160 <JoystickButton+0x50>
movslq -0x4(%rbp),%rcx
lea    0x2f02(%rip),%rax
cmpl   $0x0,(%rax,%rcx,4)
je     1154 <JoystickButton+0x44>
movl   $0x1,-0xc(%rbp)
jmp    115b <JoystickButton+0x4b>
movl   $0x2,-0xc(%rbp)
jmp    118d <JoystickButton+0x7d>
movslq -0x4(%rbp),%rcx
lea    0x2ed5(%rip),%rax
cmpl   $0x0,(%rax,%rcx,4)
je     1181 <JoystickButton+0x71>
movl   $0x3,-0xc(%rbp)
jmp    1188 <JoystickButton+0x78>
movl   $0x0,-0xc(%rbp)
jmp    118d <JoystickButton+0x7d>
mov    -0x8(%rbp),%edx
movslq -0x4(%rbp),%rcx
lea    0x2ea5(%rip),%rax
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<JoystickButton>:
push   %rbx
mov    %edi,%ebx
call   1030 <Joystick@plt>
movslq %ebx,%rdx
lea    0x2f1e(%rip),%rsi
xor    %ecx,%ecx
xor    %edi,%edi
cmpl   $0x0,(%rsi,%rdx,4)
setne  %cl
sete   %dil
add    $0x1,%edi
and    $0x30,%eax
lea    (%rcx,%rcx,2),%ecx
cmovne %edi,%ecx
mov    %eax,(%rsi,%rdx,4)
mov    %ecx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@JoystickButton.last_joy_button = internal global [4 x i32] zeroinitializer
define i32 @JoystickButton(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @Joystick(i32 %5)
%7 = and i32 %6, 48
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %1
%11 = load i32, i32* %2
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [4 x i32], [4 x i32]* @JoystickButton.last_joy_button, i64 0, i64 %12
%14 = load i32, i32* %13
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10
store i32 1, i32* %4
br label %18
17: ; preds = %10
store i32 2, i32* %4
br label %18
18: ; preds = %17, %16
br label %28
19: ; preds = %1
%20 = load i32, i32* %2
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [4 x i32], [4 x i32]* @JoystickButton.last_joy_button, i64 0, i64 %21
%23 = load i32, i32* %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 3, i32* %4
br label %27
26: ; preds = %19
store i32 0, i32* %4
br label %27
27: ; preds = %26, %25
br label %28
28: ; preds = %27, %18
%29 = load i32, i32* %3
%30 = load i32, i32* %2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [4 x i32], [4 x i32]* @JoystickButton.last_joy_button, i64 0, i64 %31
store i32 %29, i32* %32
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@JoystickButton.last_joy_button = internal unnamed_addr global [4 x i32] zeroinitializer
define i32 @JoystickButton(i32 %0) {
%2 = tail call i32 @Joystick(i32 %0)
%3 = and i32 %2, 48
%4 = icmp eq i32 %3, 0
%5 = sext i32 %0 to i64
%6 = getelementptr inbounds [4 x i32], [4 x i32]* @JoystickButton.last_joy_button, i64 0, i64 %5
%7 = load i32, i32* %6
%8 = icmp eq i32 %7, 0
%9 = select i1 %8, i32 0, i32 3
%10 = select i1 %8, i32 2, i32 1
%11 = select i1 %4, i32 %9, i32 %10
%12 = getelementptr inbounds [4 x i32], [4 x i32]* @JoystickButton.last_joy_button, i64 0, i64 %5
store i32 %3, i32* %12
ret i32 %11
}