#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 */
typedef int /*<<< orphan*/ jspath;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int get_joystick_number() {
  int i, js;
  char jspath[16];
  js = 1;
  for (i = 0; i < 10; i++) {
    snprintf(jspath, sizeof(jspath), "/dev/input/js%i", i);
    if (open(jspath, O_RDONLY) > 0) {
      js = i + 1;
    }
  }
  return js;
}


x86 assembly (clang, -O0):
<get_joystick_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0x8(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0xa,-0x4(%rbp)
jge    1190 <get_joystick_number+0x70>
lea    -0x20(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x10,%esi
lea    0xead(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
cmp    $0x0,%rax
jle    117d <get_joystick_number+0x5d>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1182 <get_joystick_number+0x62>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1136 <get_joystick_number+0x16>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_joystick_number>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xecf(%rip),%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1030 <snprintf@plt>
mov    0x2e9b(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x1,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
xor    %ebp,%ebp
test   %rax,%rax
setg   %bpl
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x2,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %rax,%rax
mov    $0x3,%eax
cmovg  %eax,%ebp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x3,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %rax,%rax
mov    $0x4,%eax
cmovg  %eax,%ebp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x4,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %rax,%rax
mov    $0x5,%eax
cmovg  %eax,%ebp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x5,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %rax,%rax
mov    $0x6,%eax
cmovg  %eax,%ebp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x6,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %rax,%rax
mov    $0x7,%eax
cmovg  %eax,%ebp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x7,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %rax,%rax
mov    $0x8,%eax
cmovg  %eax,%ebp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x8,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %rax,%rax
mov    $0x9,%eax
cmovg  %eax,%ebp
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %r14,%rdx
mov    $0x9,%ecx
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %rax,%rax
mov    $0xa,%eax
cmovle %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/dev/input/js%i\00"
@O_RDONLY = global i32 0
define i32 @get_joystick_number() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [16 x i8]
store i32 1, i32* %2
store i32 0, i32* %1
br label %4
4: ; preds = %19, %0
%5 = load i32, i32* %1
%6 = icmp slt i32 %5, 10
br i1 %6, label %7, label %22
7: ; preds = %4
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* %1
%10 = call i32 @snprintf(i8* %8, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i64 @open(i8* %11, i32 %12)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* %1
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %18
18: ; preds = %15, %7
br label %19
19: ; preds = %18
%20 = load i32, i32* %1
%21 = add nsw i32 %20, 1
store i32 %21, i32* %1
br label %4
22: ; preds = %4
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@.str = [16 x i8] c"/dev/input/js%i\00"
@O_RDONLY = global i32 0
define i32 @get_joystick_number() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = load i32, i32* @O_RDONLY
%5 = call i64 @open(i8* %2, i32 %4)
%6 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 1)
%7 = load i32, i32* @O_RDONLY
%8 = call i64 @open(i8* %2, i32 %7)
%9 = icmp sgt i64 %8, 0
%10 = select i1 %9, i32 2, i32 1
%11 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 2)
%12 = load i32, i32* @O_RDONLY
%13 = call i64 @open(i8* %2, i32 %12)
%14 = icmp sgt i64 %13, 0
%15 = select i1 %14, i32 3, i32 %10
%16 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 3)
%17 = load i32, i32* @O_RDONLY
%18 = call i64 @open(i8* %2, i32 %17)
%19 = icmp sgt i64 %18, 0
%20 = select i1 %19, i32 4, i32 %15
%21 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 4)
%22 = load i32, i32* @O_RDONLY
%23 = call i64 @open(i8* %2, i32 %22)
%24 = icmp sgt i64 %23, 0
%25 = select i1 %24, i32 5, i32 %20
%26 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 5)
%27 = load i32, i32* @O_RDONLY
%28 = call i64 @open(i8* %2, i32 %27)
%29 = icmp sgt i64 %28, 0
%30 = select i1 %29, i32 6, i32 %25
%31 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 6)
%32 = load i32, i32* @O_RDONLY
%33 = call i64 @open(i8* %2, i32 %32)
%34 = icmp sgt i64 %33, 0
%35 = select i1 %34, i32 7, i32 %30
%36 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 7)
%37 = load i32, i32* @O_RDONLY
%38 = call i64 @open(i8* %2, i32 %37)
%39 = icmp sgt i64 %38, 0
%40 = select i1 %39, i32 8, i32 %35
%41 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 8)
%42 = load i32, i32* @O_RDONLY
%43 = call i64 @open(i8* %2, i32 %42)
%44 = icmp sgt i64 %43, 0
%45 = select i1 %44, i32 9, i32 %40
%46 = call i32 @snprintf(i8* %2, i32 16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 9)
%47 = load i32, i32* @O_RDONLY
%48 = call i64 @open(i8* %2, i32 %47)
%49 = icmp sgt i64 %48, 0
%50 = select i1 %49, i32 10, i32 %45
ret i32 %50
}