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

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, int);
int stat(char *, struct stat *);
int ljoy_GetJoysticksNum() {
  int num = 0;
  int i = 0;
  for (i = 0; i < 16; i++) {
    char filename[256];
    struct stat filestat;
    sprintf((char *)&filename, "/dev/input/js%d", i);
    if (stat((char *)&filename, &filestat) != -1)
      num++;
  }
  return (num);
}


x86 assembly (clang, -O0):
<ljoy_GetJoysticksNum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x10,-0x8(%rbp)
jge    1198 <ljoy_GetJoysticksNum+0x78>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xea5(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     1185 <ljoy_GetJoysticksNum+0x65>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    118a <ljoy_GetJoysticksNum+0x6a>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1140 <ljoy_GetJoysticksNum+0x20>
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ljoy_GetJoysticksNum>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
xor    %ebx,%ebx
lea    0xec8(%rip),%r14
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <stat@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
setne  %cl
add    %ecx,%ebp
add    $0x1,%ebx
cmp    $0x10,%ebx
jne    1150 <ljoy_GetJoysticksNum+0x30>
mov    %ebp,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [16 x i8] c"/dev/input/js%d\00"
define i32 @ljoy_GetJoysticksNum() {
%1 = alloca [256 x i8]
%2 = alloca %struct.stat
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.stat* %2 to i8*
br label %5
5: ; preds = %0, %5
%6 = phi i32 [ 0, %0 ], [ %13, %5 ]
%7 = phi i32 [ 0, %0 ], [ %12, %5 ]
%8 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %6)
%9 = call i32 @stat(i8* %3, %struct.stat* %2)
%10 = icmp ne i32 %9, -1
%11 = zext i1 %10 to i32
%12 = add nuw nsw i32 %7, %11
%13 = add nuw nsw i32 %6, 1
%14 = icmp eq i32 %13, 16
br i1 %14, label %15, label %5
15: ; preds = %5
ret i32 %12
}