#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 js_event {
  int type;
  int number;
  int value;
};
typedef int /*<<< orphan*/ js;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, struct js_event *, int);
void read_joystick_axis(int fd, struct js_event js, int thrust, int js_range,
                        float js_d, int js_m, int rudder) {
  while (read(fd, &js, sizeof(js)) > 0) {
    if (js.type == 0x02 && js.number == 1) {
      thrust = -(js.value);
      thrust = (thrust / js_d) * js_m;
      printf("0%d\n", thrust);
    } else if (js.type == 0x02 && js.number == 2) {
      rudder = -(js.value);
      rudder = ((rudder + js_range) / js_d) * js_m;
      printf("1%d\n", rudder);
    }
  }
}


x86 assembly (clang, -O0):
<read_joystick_axis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%eax
mov    %rsi,-0x20(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    %edi,-0x24(%rbp)
mov    %ecx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
movss  %xmm0,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jle    120c <read_joystick_axis+0xec>
cmpl   $0x2,-0x10(%rbp)
jne    11b8 <read_joystick_axis+0x98>
cmpl   $0x1,-0xc(%rbp)
jne    11b8 <read_joystick_axis+0x98>
xor    %eax,%eax
sub    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
cvtsi2ssl -0x28(%rbp),%xmm0
divss  -0x30(%rbp),%xmm0
cvtsi2ssl -0x34(%rbp),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%esi
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
jmp    1207 <read_joystick_axis+0xe7>
cmpl   $0x2,-0x10(%rbp)
jne    1202 <read_joystick_axis+0xe2>
cmpl   $0x2,-0xc(%rbp)
jne    1202 <read_joystick_axis+0xe2>
xor    %eax,%eax
sub    -0x8(%rbp),%eax
mov    %eax,0x10(%rbp)
mov    0x10(%rbp),%eax
add    -0x2c(%rbp),%eax
cvtsi2ss %eax,%xmm0
divss  -0x30(%rbp),%xmm0
cvtsi2ssl -0x34(%rbp),%xmm1
mulss  %xmm1,%xmm0
cvttss2si %xmm0,%eax
mov    %eax,0x10(%rbp)
mov    0x10(%rbp),%esi
lea    0xe08(%rip),%rdi
call   1030 <printf@plt>
jmp    1207 <read_joystick_axis+0xe7>
jmp    1153 <read_joystick_axis+0x33>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_joystick_axis>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r9d,%ebx
movss  %xmm0,0x1c(%rsp)
mov    %r8d,%r14d
mov    %edi,%ebp
mov    %rsi,0x8(%rsp)
mov    %edx,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11dd <read_joystick_axis+0xbd>
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
movss  %xmm0,0x18(%rsp)
lea    0xe91(%rip),%r15
lea    0x8(%rsp),%rbx
lea    0xe8a(%rip),%r12
jmp    11b4 <read_joystick_axis+0x94>
nopl   (%rax)
sub    0x10(%rsp),%ecx
xorps  %xmm0,%xmm0
cvtsi2ss %ecx,%xmm0
divss  0x1c(%rsp),%xmm0
mulss  0x18(%rsp),%xmm0
cvttss2si %xmm0,%esi
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11dd <read_joystick_axis+0xbd>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%eax
cmp    $0x2,%edx
jne    11cb <read_joystick_axis+0xab>
xor    %ecx,%ecx
mov    %r15,%rdi
cmp    $0x1,%eax
je     1180 <read_joystick_axis+0x60>
cmp    $0x2,%edx
jne    11a0 <read_joystick_axis+0x80>
mov    %r14d,%ecx
mov    %r12,%rdi
cmp    $0x2,%eax
je     1180 <read_joystick_axis+0x60>
jmp    11a0 <read_joystick_axis+0x80>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.js_event = type { i32, i32, i32 }
@.str = [5 x i8] c"0%d\0A\00"
@.str.1 = [5 x i8] c"1%d\0A\00"
define void @read_joystick_axis(i32 %0, i64 %1, i32 %2, i32 %3, i32 %4, float %5, i32 %6, i32 %7) {
%9 = alloca %struct.js_event
%10 = alloca { i64, i32 }
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca float
%15 = alloca i32
%16 = alloca i32
%17 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %10, i32 0, i32 0
store i64 %1, i64* %17
%18 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %10, i32 0, i32 1
store i32 %2, i32* %18
%19 = bitcast %struct.js_event* %9 to i8*
%20 = bitcast { i64, i32 }* %10 to i8*
store i32 %0, i32* %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store float %5, float* %14
store i32 %6, i32* %15
store i32 %7, i32* %16
br label %21
21: ; preds = %72, %8
%22 = load i32, i32* %11
%23 = call i64 @read(i32 %22, %struct.js_event* %9, i32 12)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %73
25: ; preds = %21
%26 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i32 0, i32 0
%27 = load i32, i32* %26
%28 = icmp eq i32 %27, 2
br i1 %28, label %29, label %47
29: ; preds = %25
%30 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i32 0, i32 1
%31 = load i32, i32* %30
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %47
33: ; preds = %29
%34 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i32 0, i32 2
%35 = load i32, i32* %34
%36 = sub nsw i32 0, %35
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = sitofp i32 %37 to float
%39 = load float, float* %14
%40 = fdiv float %38, %39
%41 = load i32, i32* %15
%42 = sitofp i32 %41 to float
%43 = fmul float %40, %42
%44 = fptosi float %43 to i32
store i32 %44, i32* %12
%45 = load i32, i32* %12
%46 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %45)
br label %72
47: ; preds = %29, %25
%48 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i32 0, i32 0
%49 = load i32, i32* %48
%50 = icmp eq i32 %49, 2
br i1 %50, label %51, label %71
51: ; preds = %47
%52 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i32 0, i32 1
%53 = load i32, i32* %52
%54 = icmp eq i32 %53, 2
br i1 %54, label %55, label %71
55: ; preds = %51
%56 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i32 0, i32 2
%57 = load i32, i32* %56
%58 = sub nsw i32 0, %57
store i32 %58, i32* %16
%59 = load i32, i32* %16
%60 = load i32, i32* %13
%61 = add nsw i32 %59, %60
%62 = sitofp i32 %61 to float
%63 = load float, float* %14
%64 = fdiv float %62, %63
%65 = load i32, i32* %15
%66 = sitofp i32 %65 to float
%67 = fmul float %64, %66
%68 = fptosi float %67 to i32
store i32 %68, i32* %16
%69 = load i32, i32* %16
%70 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %55, %51, %47
br label %72
72: ; preds = %71, %33
br label %21
73: ; preds = %21
ret void
}

LLVM IR (-O2):
%struct.js_event = type { i32, i32, i32 }
@.str = [5 x i8] c"0%d\0A\00"
@.str.1 = [5 x i8] c"1%d\0A\00"
define void @read_joystick_axis(i32 %0, i64 %1, i32 %2, i32 %3, i32 %4, float %5, i32 %6, i32 %7) {
%9 = alloca %struct.js_event
%10 = bitcast %struct.js_event* %9 to i64*
store i64 %1, i64* %10
%11 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i64 0, i32 2
store i32 %2, i32* %11
%12 = call i64 @read(i32 %0, %struct.js_event* %9, i32 12)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %40
14: ; preds = %8
%15 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i64 0, i32 0
%16 = getelementptr inbounds %struct.js_event, %struct.js_event* %9, i64 0, i32 1
%17 = sitofp i32 %6 to float
br label %18
18: ; preds = %14, %37
%19 = load i32, i32* %15
%20 = icmp eq i32 %19, 2
%21 = load i32, i32* %16
%22 = icmp eq i32 %21, 1
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = icmp eq i32 %21, 2
%26 = select i1 %20, i1 %25, i1 false
br i1 %26, label %27, label %37
27: ; preds = %24, %18
%28 = phi i32 [ 0, %18 ], [ %4, %24 ]
%29 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %18 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %24 ]
%30 = load i32, i32* %11
%31 = sub i32 %28, %30
%32 = sitofp i32 %31 to float
%33 = fdiv float %32, %5
%34 = fmul float %33, %17
%35 = fptosi float %34 to i32
%36 = tail call i32 @printf(i8* %29, i32 %35)
br label %37
37: ; preds = %27, %24
%38 = call i64 @read(i32 %0, %struct.js_event* %9, i32 12)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %18, label %40
40: ; preds = %37, %8
ret void
}