#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EVIOCGNAME(int);
int /*<<< orphan*/ close(int);
int device_open(int);
int ioctl(int, int /*<<< orphan*/, char *);
scalar_t__ strcmp(char *, char *);
int device_find_byname(char *name) {
  int fd, i, rc;
  char buf[1024];
  for (i = 0; i < 32; i++) {
    fd = device_open(i);
    if (fd > 0) {
      rc = ioctl(fd, EVIOCGNAME(sizeof(buf)), buf);
      if (rc >= 0 && strcmp(name, buf) == 0) {
        close(fd);
        return i;
      }
    }
    close(fd);
  }
  return -1;
}


x86 assembly (clang, -O0):
<device_find_byname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x20,-0x18(%rbp)
jge    1201 <device_find_byname+0xb1>
mov    -0x18(%rbp),%edi
call   1030 <device_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11eb <device_find_byname+0x9b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x424(%rbp)
mov    $0x400,%edi
call   1070 <EVIOCGNAME@plt>
mov    -0x424(%rbp),%edi
mov    %eax,%esi
lea    -0x420(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11e6 <device_find_byname+0x96>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11e6 <device_find_byname+0x96>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1208 <device_find_byname+0xb8>
jmp    11eb <device_find_byname+0x9b>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1166 <device_find_byname+0x16>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_find_byname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
xor    %ebx,%ebx
mov    %rsp,%r15
jmp    117f <device_find_byname+0x2f>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%ebx
cmp    $0x20,%ebx
je     11bf <device_find_byname+0x6f>
mov    %ebx,%edi
call   1030 <device_open@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1170 <device_find_byname+0x20>
mov    $0x400,%edi
call   1070 <EVIOCGNAME@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    %r15,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
js     1170 <device_find_byname+0x20>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1170 <device_find_byname+0x20>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11c4 <device_find_byname+0x74>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @device_find_byname(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32 0, i32* %5
br label %8
8: ; preds = %36, %1
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 32
br i1 %10, label %11, label %39
11: ; preds = %8
%12 = load i32, i32* %5
%13 = call i32 @device_open(i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %11
%17 = load i32, i32* %4
%18 = call i32 @EVIOCGNAME(i32 1024)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%20 = call i32 @ioctl(i32 %17, i32 %18, i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %16
%24 = load i8*, i8** %3
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%26 = call i64 @strcmp(i8* %24, i8* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %5
store i32 %31, i32* %2
br label %40
32: ; preds = %23, %16
br label %33
33: ; preds = %32, %11
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %8
39: ; preds = %8
store i32 -1, i32* %2
br label %40
40: ; preds = %39, %28
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
define i32 @device_find_byname(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %1, %17
%5 = phi i32 [ 0, %1 ], [ %19, %17 ]
%6 = call i32 @device_open(i32 %5)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %4
%9 = call i32 @EVIOCGNAME(i32 1024)
%10 = call i32 @ioctl(i32 %6, i32 %9, i8* %3)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = call i64 @strcmp(i8* %0, i8* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @close(i32 %6)
br label %21
17: ; preds = %8, %12, %4
%18 = call i32 @close(i32 %6)
%19 = add nuw nsw i32 %5, 1
%20 = icmp eq i32 %19, 32
br i1 %20, label %21, label %4
21: ; preds = %17, %15
%22 = phi i32 [ %5, %15 ], [ -1, %17 ]
ret i32 %22
}