#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 /*<<< orphan*/ can_id;
  int /*<<< orphan*/ data;
  int /*<<< orphan*/ can_dlc;
};
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*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strlen(int /*<<< orphan*/);
int write(int, struct can_frame *, int);
int main(int argc, char **argv) {
  int skt = socket(29, SOCK_RAW, CAN_RAW);
  struct ifreq ifr;
  strcpy(ifr.ifr_name, "can0");
  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;
  int counter = 0;
  sprintf(frame.data, "hello");
  frame.can_dlc = strlen(frame.data);
  while (1) {
    frame.can_id = counter++;
    int bytes_sent = write(skt, &frame, sizeof(frame));
    if (bytes_sent < 0) {
      perror("bad write");
    } else {
      printf("%d bytes sent\n", bytes_sent);
    }
    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    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    $0x1d,%edi
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xe21(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <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>
movl   $0x0,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
lea    0xddd(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x34(%rbp),%edi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x3c(%rbp)
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1275 <main+0xd5>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
jmp    1284 <main+0xe4>
mov    -0x40(%rbp),%esi
lea    0xd96(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1238 <main+0x98>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    $0x1d,%edi
call   10c0 <socket@plt>
mov    %eax,%ebx
lea    0xe2f(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
movl   $0x1d,(%rsp)
mov    0x18(%rsp),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
lea    0xdf5(%rip),%rsi
call   10a0 <sprintf@plt>
call   1050 <strlen@plt>
mov    %eax,0x10(%rsp)
xor    %ebp,%ebp
lea    0x8(%rsp),%r14
lea    0xddf(%rip),%r15
lea    0xde2(%rip),%r12
jmp    1257 <main+0xb7>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0x1,%ebp
mov    %ebp,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    1240 <main+0xa0>
mov    %r15,%rdi
call   1090 <perror@plt>
jmp    124a <main+0xaa>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_can = type { i32, i32 }
%struct.can_frame = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str = [5 x i8] c"can0\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [6 x i8] c"hello\00"
@.str.2 = [10 x i8] c"bad write\00"
@.str.3 = [15 x i8] c"%d bytes sent\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
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @SOCK_RAW
%13 = load i32, i32* @CAN_RAW
%14 = call i32 @socket(i32 29, i32 %12, i32 %13)
store i32 %14, i32* %6
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i32 %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* %6
%19 = load i32, i32* @SIOCGIFINDEX
%20 = call i32 @ioctl(i32 %18, i32 %19, %struct.ifreq* %7)
%21 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %8, i32 0, i32 0
store i32 29, i32* %21
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %8, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* %6
%26 = bitcast %struct.sockaddr_can* %8 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 8)
store i32 0, i32* %10
%28 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %9, i32 0, i32 1
%29 = load i32, i32* %28
%30 = call i32 @sprintf(i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%31 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %9, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @strlen(i32 %32)
%34 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %9, i32 0, i32 2
store i32 %33, i32* %34
br label %35
35: ; preds = %2, %48
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
store i32 %37, i32* %10
%38 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %9, i32 0, i32 0
store i32 %36, i32* %38
%39 = load i32, i32* %6
%40 = call i32 @write(i32 %39, %struct.can_frame* %9, i32 12)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %48
45: ; preds = %35
%46 = load i32, i32* %11
%47 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %45, %43
%49 = call i32 @sleep(i32 1)
br label %35
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_can = type { i32, i32 }
%struct.can_frame = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str = [5 x i8] c"can0\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [6 x i8] c"hello\00"
@.str.2 = [10 x i8] c"bad write\00"
@.str.3 = [15 x i8] c"%d bytes sent\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 ([5 x i8], [5 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 @sprintf(i32 undef, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @strlen(i32 undef)
%23 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i64 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i64 0, i32 0
br label %25
25: ; preds = %34, %2
%26 = phi i32 [ 0, %2 ], [ %27, %34 ]
%27 = add nuw nsw i32 %26, 1
store i32 %26, i32* %24
%28 = call i32 @write(i32 %8, %struct.can_frame* %5, i32 12)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %34
32: ; preds = %25
%33 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %34
34: ; preds = %32, %30
%35 = call i32 @sleep(i32 1)
br label %25
}