#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 sockaddr_can {
  int /*<<< orphan*/ can_ifindex;
  int /*<<< orphan*/ can_family;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  char *ifr_name;
  int /*<<< orphan*/ ifr_ifindex;
};
struct can_frame {
  int dummy;
};
typedef int /*<<< orphan*/ can_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_CAN;
int /*<<< orphan*/ CAN_RAW;
int /*<<< orphan*/ PF_CAN;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bldc_get_decoded_ppm(int, int, float *, float *,
                                        int /*<<< orphan*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char const *);
int main(int varc, char *varv[]) {
  int can_socket;
  const char *ifname = varv[1];
  struct ifreq can_ifr;
  struct sockaddr_can can_addr;
  struct can_frame test_frame;
  int nbytes;
  printf("bldctest \n");
  printf("run with arguments:\n");
  printf("  1. caninterface\n\n");
  printf("Starting application\n");
  can_socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  strcpy(can_ifr.ifr_name, ifname);
  if (ioctl(can_socket, SIOCGIFINDEX, &can_ifr) < 0) {
    printf("ERROR unable to connect to interface: %s\n", can_ifr.ifr_name);
    return -1;
  }
  printf("Connected to socket: %s\n", can_ifr.ifr_name);
  can_addr.can_family = AF_CAN;
  can_addr.can_ifindex = can_ifr.ifr_ifindex;
  if (bind(can_socket, (struct sockaddr *)&can_addr, sizeof(can_addr)) < 0) {
    perror("Error in socket bind");
    return -2;
  }
  float servo_ms, servo_ms_last;
  bldc_get_decoded_ppm(can_socket, 6, &servo_ms, &servo_ms_last, NULL);
  printf("BLDC ppm: %f, %f\n", servo_ms, servo_ms_last);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1237 <main+0xc7>
mov    -0x30(%rbp),%rsi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cf <main+0x15f>
mov    -0x30(%rbp),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    128e <main+0x11e>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12cf <main+0x15f>
mov    -0x14(%rbp),%edi
mov    $0x6,%esi
lea    -0x48(%rbp),%rdx
lea    -0x4c(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <bldc_get_decoded_ppm@plt>
movss  -0x48(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  -0x4c(%rbp),%xmm1
cvtss2sd %xmm1,%xmm1
lea    0xde1(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x8(%rsi),%rbx
lea    0xed9(%rip),%rdi
call   1040 <puts@plt>
lea    0xed7(%rip),%rdi
call   1040 <puts@plt>
lea    0xedf(%rip),%rdi
call   1040 <puts@plt>
lea    0xee6(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
mov    0x18(%rsp),%rsi
test   %rax,%rax
js     127b <main+0xfb>
lea    0xe20(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x20(%rsp),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1297 <main+0x117>
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x6,%esi
xor    %r8d,%r8d
call   1070 <bldc_get_decoded_ppm@plt>
movss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
movss  0x8(%rsp),%xmm1
cvtss2sd %xmm1,%xmm1
lea    0xde6(%rip),%rdi
mov    $0x2,%al
call   1050 <printf@plt>
jmp    128e <main+0x10e>
lea    0xd7e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xda5(%rip),%rdi
call   1090 <perror@plt>
mov    $0xfffffffe,%ebx
jmp    128e <main+0x10e>

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
%struct.sockaddr_can = type { i32, i32 }
%struct.can_frame = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"bldctest \0A\00"
@.str.1 = [21 x i8] c"run with arguments:\0A\00"
@.str.2 = [20 x i8] c" 1. caninterface\0A\0A\00"
@.str.3 = [22 x i8] c"Starting application\0A\00"
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.4 = [42 x i8] c"ERROR unable to connect to interface: %s\0A\00"
@.str.5 = [25 x i8] c"Connected to socket: %s\0A\00"
@AF_CAN = global i32 0
@.str.6 = [21 x i8] c"Error in socket bind\00"
@.str.7 = [18 x i8] c"BLDC ppm: %f, %f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.ifreq
%9 = alloca %struct.sockaddr_can
%10 = alloca %struct.can_frame
%11 = alloca i32
%12 = alloca float
%13 = alloca float
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %7
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @PF_CAN
%22 = load i32, i32* @SOCK_RAW
%23 = load i32, i32* @CAN_RAW
%24 = call i32 @socket(i32 %21, i32 %22, i32 %23)
store i32 %24, i32* %6
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = load i8*, i8** %7
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i32, i32* %6
%30 = load i32, i32* @SIOCGIFINDEX
%31 = call i64 @ioctl(i32 %29, i32 %30, %struct.ifreq* %8)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %2
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %35)
store i32 -1, i32* %3
br label %60
37: ; preds = %2
%38 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @AF_CAN
%42 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %9, i32 0, i32 1
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %9, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_can* %9 to %struct.sockaddr*
%48 = call i64 @bind(i32 %46, %struct.sockaddr* %47, i32 8)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %37
%51 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
store i32 -2, i32* %3
br label %60
52: ; preds = %37
%53 = load i32, i32* %6
%54 = call i32 @bldc_get_decoded_ppm(i32 %53, i32 6, float* %12, float* %13, i32* null)
%55 = load float, float* %12
%56 = fpext float %55 to double
%57 = load float, float* %13
%58 = fpext float %57 to double
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), double %56, double %58)
store i32 0, i32* %3
br label %60
60: ; preds = %52, %50, %33
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
%struct.sockaddr_can = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.4 = [42 x i8] c"ERROR unable to connect to interface: %s\0A\00"
@.str.5 = [25 x i8] c"Connected to socket: %s\0A\00"
@AF_CAN = global i32 0
@.str.6 = [21 x i8] c"Error in socket bind\00"
@.str.7 = [18 x i8] c"BLDC ppm: %f, %f\0A\00"
@str = [10 x i8] c"bldctest \00"
@str.8 = [20 x i8] c"run with arguments:\00"
@str.9 = [19 x i8] c" 1. caninterface\0A\00"
@str.10 = [21 x i8] c"Starting application\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.ifreq
%4 = alloca %struct.sockaddr_can
%5 = alloca float
%6 = alloca float
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = bitcast %struct.ifreq* %3 to i8*
%10 = bitcast %struct.sockaddr_can* %4 to i8*
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.10, i64 0, i64 0))
%15 = load i32, i32* @PF_CAN
%16 = load i32, i32* @SOCK_RAW
%17 = load i32, i32* @CAN_RAW
%18 = tail call i32 @socket(i32 %15, i32 %16, i32 %17)
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%20 = tail call i32 @strcpy(i8* undef, i8* %8)
%21 = load i32, i32* @SIOCGIFINDEX
%22 = call i64 @ioctl(i32 %18, i32 %21, %struct.ifreq* %3)
%23 = icmp slt i64 %22, 0
%24 = load i8*, i8** %19
br i1 %23, label %25, label %27
25: ; preds = %2
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %24)
br label %48
27: ; preds = %2
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %24)
%29 = load i32, i32* @AF_CAN
%30 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i64 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i64 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_can* %4 to %struct.sockaddr*
%35 = call i64 @bind(i32 %18, %struct.sockaddr* %34, i32 8)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %48
39: ; preds = %27
%40 = bitcast float* %5 to i8*
%41 = bitcast float* %6 to i8*
%42 = call i32 @bldc_get_decoded_ppm(i32 %18, i32 6, float* %5, float* %6, i32* null)
%43 = load float, float* %5
%44 = fpext float %43 to double
%45 = load float, float* %6
%46 = fpext float %45 to double
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), double %44, double %46)
br label %48
48: ; preds = %39, %37, %25
%49 = phi i32 [ -1, %25 ], [ -2, %37 ], [ 0, %39 ]
ret i32 %49
}