#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 open_powermate(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int find_powermate(int mode) {
  char devname[256];
  int i, r;
  for (i = 0; i < 16; i++) {
    sprintf(devname, "/dev/input/event%d", i);
    r = open_powermate(devname, mode);
    if (r >= 0)
      return r;
  }
  return -1;
}


x86 assembly (clang, -O0):
<find_powermate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x114(%rbp)
cmpl   $0x10,-0x114(%rbp)
jge    11a7 <find_powermate+0x87>
lea    -0x110(%rbp),%rdi
mov    -0x114(%rbp),%edx
lea    0xea7(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <open_powermate@plt>
mov    %eax,-0x118(%rbp)
cmpl   $0x0,-0x118(%rbp)
jl     118e <find_powermate+0x6e>
mov    -0x118(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ae <find_powermate+0x8e>
jmp    1193 <find_powermate+0x73>
mov    -0x114(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x114(%rbp)
jmp    1138 <find_powermate+0x18>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_powermate>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edi,%r15d
xor    %ebp,%ebp
lea    0xec7(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <sprintf@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <open_powermate@plt>
test   %eax,%eax
jns    1169 <find_powermate+0x49>
add    $0x1,%ebp
cmp    $0x10,%ebp
jne    1140 <find_powermate+0x20>
mov    $0xffffffff,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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