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

/* Variables and functions */
int /*<<< orphan*/ CAN_RAW;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct can_frame *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, char *);
int read(int, struct can_frame *, int);
int /*<<< orphan*/ sleep(int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int main(int argc, char **argv) {
  int skt = socket(29, SOCK_RAW, CAN_RAW);
  struct ifreq ifr;
  strcpy(ifr.ifr_name, "vcan0");
  ioctl(skt, SIOCGIFINDEX, &ifr);
  struct sockaddr_can addr;
  addr.can_family = 29;
  addr.can_ifindex = ifr.ifr_ifindex;
  bind(skt, (struct sockaddr *)&addr, sizeof(addr));
  struct can_frame frame;
  memset(&frame, 0, sizeof(frame));
  while (1) {
    int bytes_read = read(skt, &frame, sizeof(frame));
    if (bytes_read < 0) {
      perror("bad read");
    } else {
      printf("%d bytes read -- %d:%s\n", bytes_read, frame.can_id, frame.data);
    }
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    $0x1d,%edi
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xe31(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
movl   $0x1d,-0x28(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1246 <main+0xb6>
lea    0xdca(%rip),%rdi
call   1090 <perror@plt>
jmp    125c <main+0xcc>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    -0x30(%rbp),%rcx
lea    0xdb8(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1217 <main+0x87>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    $0x1d,%edi
call   10b0 <socket@plt>
mov    %eax,%ebx
lea    0xe40(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
movl   $0x1d,0x8(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
lea    0xdf0(%rip),%r15
lea    0xdf2(%rip),%r12
jmp    123d <main+0xad>
nop
mov    0x18(%rsp),%edx
mov    0x20(%rsp),%rcx
mov    %r12,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1070 <read@plt>
test   %eax,%eax
jns    1220 <main+0x90>
mov    %r15,%rdi
call   1090 <perror@plt>
jmp    1233 <main+0xa3>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_can = type { i32, i32 }
%struct.can_frame = type { i32, i8* }
%struct.sockaddr = type { i32 }
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str = [6 x i8] c"vcan0\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [9 x i8] c"bad read\00"
@.str.2 = [24 x i8] c"%d bytes read -- %d:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca %struct.sockaddr_can
%9 = alloca %struct.can_frame
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @CAN_RAW
%13 = call i32 @socket(i32 29, i32 %11, i32 %12)
store i32 %13, i32* %6
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %6
%18 = load i32, i32* @SIOCGIFINDEX
%19 = call i32 @ioctl(i32 %17, i32 %18, %struct.ifreq* %7)
%20 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %8, i32 0, i32 0
store i32 29, i32* %20
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %8, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* %6
%25 = bitcast %struct.sockaddr_can* %8 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = call i32 @memset(%struct.can_frame* %9, i32 0, i32 16)
br label %28
28: ; preds = %2, %42
%29 = load i32, i32* %6
%30 = call i32 @read(i32 %29, %struct.can_frame* %9, i32 16)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %42
35: ; preds = %28
%36 = load i32, i32* %10
%37 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %9, i32 0, i32 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %9, i32 0, i32 1
%40 = load i8*, i8** %39
%41 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %38, i8* %40)
br label %42
42: ; preds = %35, %33
%43 = call i32 @sleep(i32 1)
br label %28
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_can = type { i32, i32 }
%struct.can_frame = type { i32, i8* }
%struct.sockaddr = type { i32 }
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str = [6 x i8] c"vcan0\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [9 x i8] c"bad read\00"
@.str.2 = [24 x i8] c"%d bytes read -- %d:%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.ifreq
%4 = alloca %struct.sockaddr_can
%5 = alloca %struct.can_frame
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @CAN_RAW
%8 = tail call i32 @socket(i32 29, i32 %6, i32 %7)
%9 = bitcast %struct.ifreq* %3 to i8*
%10 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @SIOCGIFINDEX
%12 = call i32 @ioctl(i32 %8, i32 %11, %struct.ifreq* %3)
%13 = bitcast %struct.sockaddr_can* %4 to i8*
%14 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i64 0, i32 0
store i32 29, i32* %14
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i64 0, i32 1
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_can* %4 to %struct.sockaddr*
%19 = call i32 @bind(i32 %8, %struct.sockaddr* %18, i32 8)
%20 = bitcast %struct.can_frame* %5 to i8*
%21 = call i32 @memset(%struct.can_frame* %5, i32 0, i32 16)
%22 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i64 0, i32 0
%23 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i64 0, i32 1
br label %24
24: ; preds = %33, %2
%25 = call i32 @read(i32 %8, %struct.can_frame* %5, i32 16)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %33
29: ; preds = %24
%30 = load i32, i32* %22
%31 = load i8*, i8** %23
%32 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %30, i8* %31)
br label %33
33: ; preds = %29, %27
%34 = call i32 @sleep(i32 1)
br label %24
}