#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_GetRealId(int id) {
  int i;
  for (i = id; i < 16; i++) {
    char filename[256];
    struct stat filestat;
    sprintf((char *)&filename, "/dev/input/js%d", i);
    if (stat((char *)&filename, &filestat) != -1)
      return (i);
  }
  return (-1);
}


x86 assembly (clang, -O0):
<ljoy_GetRealId>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x10,-0xc(%rbp)
jge    118e <ljoy_GetRealId+0x6e>
lea    -0x110(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xeb1(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     117b <ljoy_GetRealId+0x5b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1195 <ljoy_GetRealId+0x75>
jmp    1180 <ljoy_GetRealId+0x60>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1134 <ljoy_GetRealId+0x14>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ljoy_GetRealId>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    $0xffffffff,%r14d
cmp    $0xf,%edi
jg     117a <ljoy_GetRealId+0x5a>
mov    %edi,%ebp
lea    0xebd(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r12
nopl   (%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    1177 <ljoy_GetRealId+0x57>
add    $0x1,%ebp
cmp    $0x10,%ebp
jne    1150 <ljoy_GetRealId+0x30>
jmp    117a <ljoy_GetRealId+0x5a>
mov    %ebp,%r14d
mov    %r14d,%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_GetRealId(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca %struct.stat
store i32 %0, i32* %3
%7 = load i32, i32* %3
store i32 %7, i32* %4
br label %8
8: ; preds = %21, %1
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 16
br i1 %10, label %11, label %24
11: ; preds = %8
%12 = bitcast [256 x i8]* %5 to i8*
%13 = load i32, i32* %4
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = bitcast [256 x i8]* %5 to i8*
%16 = call i32 @stat(i8* %15, %struct.stat* %6)
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %25
20: ; preds = %11
br label %21
21: ; preds = %20
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
br label %8
24: ; preds = %8
store i32 -1, i32* %2
br label %25
25: ; preds = %24, %18
%26 = load i32, i32* %2
ret i32 %26
}

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