#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 {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ ifr_name;
};
struct can_frame {
  int can_id;
  int can_dlc;
  int *data;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_CAN;
int /*<<< orphan*/ CAN_RAW;
int EXIT_SUCCESS;
int /*<<< orphan*/ PF_CAN;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_can *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ puts(char *);
int read(int, struct can_frame *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
char *strerror(int /*<<< orphan*/);
int write(int, struct can_frame *, int);
int main(void) {
  int s;
  struct sockaddr_can addr;
  struct can_frame frame;
  int nbytes;
  int i;
  struct ifreq ifr;
  s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  if (s == -1) {
    fprintf(stderr, "Socket error:%s\n\a", strerror(errno));
    exit(1);
  }
  strcpy(ifr.ifr_name, "can0");
  ioctl(s, SIOCGIFINDEX, &ifr);
  bzero(&addr, sizeof(struct sockaddr_can));
  addr.can_family = AF_CAN;
  addr.can_ifindex = ifr.ifr_ifindex;
  bind(s, (struct sockaddr *)&addr, sizeof(addr));
  frame.can_id = 0x01;
  frame.can_dlc = 8;
  for (i = 0; i < frame.can_dlc; i++) {
    frame.data[i] = i;
  }
  for (i = 0; i < 1; i++) {
    nbytes = write(s, &frame, sizeof(struct can_frame));
    if (nbytes < 0) {
      fprintf(stderr, "raw write error:%s\n\a", strerror(errno));
      exit(1);
    }
    if (nbytes < sizeof(struct can_frame)) {
      fprintf(stderr, "write incomplete CAN frame\n\a");
      exit(1);
    }
    sleep(1);
    printf("can frame sent %d\n", i);
  }
  sleep(1);
  nbytes = read(s, &frame, sizeof(struct can_frame));
  if (nbytes < 0) {
    fprintf(stderr, "raw read error:%s\n\a", strerror(errno));
    exit(1);
  }
  if (nbytes < sizeof(struct can_frame)) {
    fprintf(stderr, "read incomplete CAN frame\n\a");
    exit(1);
  }
  printf("Frame can_id:%d, can_dlc:%d\n", frame.can_id, frame.can_dlc);
  for (i = 0; i < frame.can_dlc; i++) {
    printf("Frame can_data%d: %d\n", i, frame.data[i]);
  }
  close(s);
  puts("Hello Can World!");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1254 <main+0x74>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x2c(%rbp),%edi
lea    0xdb4(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1070 <ioctl@plt>
lea    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   10c0 <bzero@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <bind@plt>
movl   $0x1,-0x20(%rbp)
movl   $0x8,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12e6 <main+0x106>
mov    -0x28(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12be <main+0xde>
movl   $0x0,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jge    13a5 <main+0x1c5>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1050 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    134d <main+0x16d>
mov    0x2cb4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xcdb(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movslq -0x24(%rbp),%rax
cmp    $0x10,%rax
jae    137c <main+0x19c>
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
lea    0xcc1(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    -0x28(%rbp),%esi
lea    0xcb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12ed <main+0x10d>
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1090 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1405 <main+0x225>
mov    0x2bfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2be8(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xc68(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movslq -0x24(%rbp),%rax
cmp    $0x10,%rax
jae    1434 <main+0x254>
mov    0x2bb6(%rip),%rax
mov    (%rax),%edi
lea    0xc4d(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xc4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1485 <main+0x2a5>
mov    -0x28(%rbp),%esi
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xc39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    144f <main+0x26f>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
lea    0xc2b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2b18(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x60>
mov    0x2e1d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe36(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%ebx
lea    0xe29(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1060 <bind@plt>

LLVM IR (-O0):
%struct.sockaddr_can = type { i32, i32 }
%struct.can_frame = type { i32, i32, i32* }
%struct.ifreq = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"Socket error:%s\0A\07\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"can0\00"
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
@.str.2 = [21 x i8] c"raw write error:%s\0A\07\00"
@.str.3 = [29 x i8] c"write incomplete CAN frame\0A\07\00"
@.str.4 = [19 x i8] c"can frame sent %d\0A\00"
@.str.5 = [20 x i8] c"raw read error:%s\0A\07\00"
@.str.6 = [28 x i8] c"read incomplete CAN frame\0A\07\00"
@.str.7 = [29 x i8] c"Frame can_id:%d, can_dlc:%d\0A\00"
@.str.8 = [22 x i8] c"Frame can_data%d: %d\0A\00"
@.str.9 = [17 x i8] c"Hello Can World!\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_can
%4 = alloca %struct.can_frame
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
store i32 0, i32* %1
%8 = load i32, i32* @PF_CAN
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @CAN_RAW
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 @strcpy(i32 %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = load i32, i32* @SIOCGIFINDEX
%26 = call i32 @ioctl(i32 %24, i32 %25, %struct.ifreq* %7)
%27 = call i32 @bzero(%struct.sockaddr_can* %3, i32 8)
%28 = load i32, i32* @AF_CAN
%29 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %3, i32 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %3, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %2
%34 = bitcast %struct.sockaddr_can* %3 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 8)
%36 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i32 0, i32 0
store i32 1, i32* %36
%37 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i32 0, i32 1
store i32 8, i32* %37
store i32 0, i32* %6
br label %38
38: ; preds = %50, %20
%39 = load i32, i32* %6
%40 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i32 0, i32 1
%41 = load i32, i32* %40
%42 = icmp slt i32 %39, %41
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = load i32, i32* %6
%45 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i32 0, i32 2
%46 = load i32*, i32** %45
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
store i32 %44, i32* %49
br label %50
50: ; preds = %43
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %38
53: ; preds = %38
store i32 0, i32* %6
br label %54
54: ; preds = %80, %53
%55 = load i32, i32* %6
%56 = icmp slt i32 %55, 1
br i1 %56, label %57, label %83
57: ; preds = %54
%58 = load i32, i32* %2
%59 = call i32 @write(i32 %58, %struct.can_frame* %4, i32 16)
store i32 %59, i32* %5
%60 = load i32, i32* %5
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %65)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %57
%69 = load i32, i32* %5
%70 = sext i32 %69 to i64
%71 = icmp ult i64 %70, 16
br i1 %71, label %72, label %76
72: ; preds = %68
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %68
%77 = call i32 @sleep(i32 1)
%78 = load i32, i32* %6
%79 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %78)
br label %80
80: ; preds = %76
%81 = load i32, i32* %6
%82 = add nsw i32 %81, 1
store i32 %82, i32* %6
br label %54
83: ; preds = %54
%84 = call i32 @sleep(i32 1)
%85 = load i32, i32* %2
%86 = call i32 @read(i32 %85, %struct.can_frame* %4, i32 16)
store i32 %86, i32* %5
%87 = load i32, i32* %5
%88 = icmp slt i32 %87, 0
br i1 %88, label %89, label %95
89: ; preds = %83
%90 = load i32, i32* @stderr
%91 = load i32, i32* @errno
%92 = call i8* @strerror(i32 %91)
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %90, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %92)
%94 = call i32 @exit(i32 1)
unreachable
95: ; preds = %83
%96 = load i32, i32* %5
%97 = sext i32 %96 to i64
%98 = icmp ult i64 %97, 16
br i1 %98, label %99, label %103
99: ; preds = %95
%100 = load i32, i32* @stderr
%101 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%102 = call i32 @exit(i32 1)
unreachable
103: ; preds = %95
%104 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i32 0, i32 0
%105 = load i32, i32* %104
%106 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i32 0, i32 1
%107 = load i32, i32* %106
%108 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %105, i32 %107)
store i32 0, i32* %6
br label %109
109: ; preds = %123, %103
%110 = load i32, i32* %6
%111 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i32 0, i32 1
%112 = load i32, i32* %111
%113 = icmp slt i32 %110, %112
br i1 %113, label %114, label %126
114: ; preds = %109
%115 = load i32, i32* %6
%116 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i32 0, i32 2
%117 = load i32*, i32** %116
%118 = load i32, i32* %6
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds i32, i32* %117, i64 %119
%121 = load i32, i32* %120
%122 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i32 %115, i32 %121)
br label %123
123: ; preds = %114
%124 = load i32, i32* %6
%125 = add nsw i32 %124, 1
store i32 %125, i32* %6
br label %109
126: ; preds = %109
%127 = load i32, i32* %2
%128 = call i32 @close(i32 %127)
%129 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.9, i64 0, i64 0))
%130 = load i32, i32* @EXIT_SUCCESS
ret i32 %130
}

LLVM IR (-O2):
%struct.sockaddr_can = type { i32, i32 }
%struct.ifreq = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"Socket error:%s\0A\07\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"can0\00"
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_can
%2 = alloca %struct.ifreq
%3 = bitcast %struct.sockaddr_can* %1 to i8*
%4 = bitcast %struct.ifreq* %2 to i8*
%5 = load i32, i32* @PF_CAN
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @CAN_RAW
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @SIOCGIFINDEX
%19 = call i32 @ioctl(i32 %8, i32 %18, %struct.ifreq* %2)
%20 = call i32 @bzero(%struct.sockaddr_can* %1, i32 8)
%21 = load i32, i32* @AF_CAN
%22 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %1, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %1, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_can* %1 to %struct.sockaddr*
%27 = call i32 @bind(i32 %8, %struct.sockaddr* %26, i32 8)
unreachable
}