#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 uint8_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ read(int, int *, int);
int ctl_init(const char *dev) {
  int fd;
  uint8_t buf[8];
  uint8_t num_btns = 0;
  uint8_t num_axes = 0;
  fd = open(dev, O_RDONLY);
  if (fd < 0) {
    return fd;
  }
  do {
    read(fd, buf, 8);
    printf("0:%#x 1:%#x 2:%#x 3:%#x 4:%#x 5:%#x 6:%#x 7:%#x\n", buf[0], buf[1],
           buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
    if (buf[6] == 0x81) {
      ++num_btns;
    }
    if (buf[6] == 0x82) {
      ++num_axes;
    }
  } while (buf[5] != 0x80);
  printf("Found Joystick: %i buttons, %i axes\n", num_btns, num_axes);
  return fd;
}


x86 assembly (clang, -O0):
<ctl_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1174 <ctl_init+0x44>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1219 <ctl_init+0xe9>
jmp    1179 <ctl_init+0x49>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x8,%edx
call   1040 <read@plt>
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x30(%rbp),%r9d
mov    -0x2c(%rbp),%r11d
mov    -0x28(%rbp),%r10d
mov    -0x24(%rbp),%eax
lea    0xe53(%rip),%rdi
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x81,-0x28(%rbp)
jne    11d7 <ctl_init+0xa7>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x82,-0x28(%rbp)
jne    11ed <ctl_init+0xbd>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11f2 <ctl_init+0xc2>
cmpl   $0x80,-0x2c(%rbp)
jne    1179 <ctl_init+0x49>
mov    -0x44(%rbp),%esi
mov    -0x48(%rbp),%edx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ctl_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     11f3 <ctl_init+0xc3>
lea    0xea2(%rip),%r12
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rsp,%rsi
mov    $0x8,%edx
call   1040 <read@plt>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
mov    0x10(%rsp),%r9d
mov    0x14(%rsp),%r13d
mov    0x18(%rsp),%r15d
mov    0x1c(%rsp),%r10d
sub    $0x8,%rsp
mov    %r12,%rdi
xor    %eax,%eax
push   %r10
push   %r15
push   %r13
call   1030 <printf@plt>
add    $0x20,%rsp
xor    %eax,%eax
cmp    $0x81,%r15d
sete   %al
add    %eax,%ebp
xor    %eax,%eax
cmp    $0x82,%r15d
sete   %al
add    %eax,%ebx
cmp    $0x80,%r13d
jne    1170 <ctl_init+0x40>
lea    0xe49(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [49 x i8] c"0:%#x 1:%#x 2:%#x 3:%#x 4:%#x 5:%#x 6:%#x 7:%#x\0A\00"
@.str.1 = [37 x i8] c"Found Joystick: %i buttons, %i axes\0A\00"
define i32 @ctl_init(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [8 x i32]
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* %4
store i32 %14, i32* %2
br label %60
15: ; preds = %1
br label %16
16: ; preds = %51, %15
%17 = load i32, i32* %4
%18 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%19 = call i32 @read(i32 %17, i32* %18, i32 8)
%20 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 2
%25 = load i32, i32* %24
%26 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 3
%27 = load i32, i32* %26
%28 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 4
%29 = load i32, i32* %28
%30 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 5
%31 = load i32, i32* %30
%32 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 6
%33 = load i32, i32* %32
%34 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 7
%35 = load i32, i32* %34
%36 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %23, i32 %25, i32 %27, i32 %29, i32 %31, i32 %33, i32 %35)
%37 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 6
%38 = load i32, i32* %37
%39 = icmp eq i32 %38, 129
br i1 %39, label %40, label %43
40: ; preds = %16
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %43
43: ; preds = %40, %16
%44 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 6
%45 = load i32, i32* %44
%46 = icmp eq i32 %45, 130
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %50
50: ; preds = %47, %43
br label %51
51: ; preds = %50
%52 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 5
%53 = load i32, i32* %52
%54 = icmp ne i32 %53, 128
br i1 %54, label %16, label %55
55: ; preds = %51
%56 = load i32, i32* %6
%57 = load i32, i32* %7
%58 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %56, i32 %57)
%59 = load i32, i32* %4
store i32 %59, i32* %2
br label %60
60: ; preds = %55, %13
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [49 x i8] c"0:%#x 1:%#x 2:%#x 3:%#x 4:%#x 5:%#x 6:%#x 7:%#x\0A\00"
@.str.1 = [37 x i8] c"Found Joystick: %i buttons, %i axes\0A\00"
define i32 @ctl_init(i8* nocapture %0) {
%2 = alloca [8 x i32]
%3 = bitcast [8 x i32]* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %38, label %7
7: ; preds = %1
%8 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 0
%9 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 1
%10 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 2
%11 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 3
%12 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 4
%13 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 5
%14 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 6
%15 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 7
br label %16
16: ; preds = %7, %16
%17 = phi i32 [ %31, %16 ], [ 0, %7 ]
%18 = phi i32 [ %34, %16 ], [ 0, %7 ]
%19 = call i32 @read(i32 %5, i32* %8, i32 8)
%20 = load i32, i32* %8
%21 = load i32, i32* %9
%22 = load i32, i32* %10
%23 = load i32, i32* %11
%24 = load i32, i32* %12
%25 = load i32, i32* %13
%26 = load i32, i32* %14
%27 = load i32, i32* %15
%28 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %21, i32 %22, i32 %23, i32 %24, i32 %25, i32 %26, i32 %27)
%29 = icmp eq i32 %26, 129
%30 = zext i1 %29 to i32
%31 = add nuw nsw i32 %17, %30
%32 = icmp eq i32 %26, 130
%33 = zext i1 %32 to i32
%34 = add nuw nsw i32 %18, %33
%35 = icmp eq i32 %25, 128
br i1 %35, label %36, label %16
36: ; preds = %16
%37 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %34)
br label %38
38: ; preds = %1, %36
ret i32 %5
}