#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 timespec {
  int tv_nsec;
};
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;
  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*/ TIMER_ABSTIME;
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
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 /*<<< orphan*/ tsnorm(struct timespec *);
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;
  struct timespec t;
  int interval = 10000000;
  clock_gettime(0, &t);
  sprintf(frame.data, "hello");
  frame.can_dlc = strlen(frame.data);
  while (1) {
    clock_nanosleep(0, TIMER_ABSTIME, &t, NULL);
    frame.can_id = 13;
    int bytes_sent = write(skt, &frame, sizeof(frame));
    if (bytes_sent < 0) {
      perror("bad write");
    } else {
    }
    t.tv_nsec += interval;
    tsnorm(&t);
  }
}


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    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    $0x1d,%edi
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xe11(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2dc2(%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   1090 <bind@plt>
movl   $0x989680,-0x44(%rbp)
xor    %edi,%edi
lea    -0x40(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x34(%rbp),%edi
lea    0xdc2(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x34(%rbp),%edi
call   1060 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x40(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10c0 <clock_nanosleep@plt>
movl   $0xd,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1050 <write@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    12a1 <main+0xf1>
lea    0xd74(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a6 <main+0xf6>
jmp    12a6 <main+0xf6>
mov    -0x44(%rbp),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x40(%rbp),%rdi
call   1080 <tsnorm@plt>
jmp    1253 <main+0xa3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
mov    $0x1d,%edi
call   10d0 <socket@plt>
mov    %eax,%ebp
lea    0xe1f(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    0x28(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
movl   $0x1d,0x10(%rsp)
mov    0x28(%rsp),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1090 <bind@plt>
lea    0x8(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1040 <clock_gettime@plt>
lea    0xdd3(%rip),%rsi
call   10b0 <sprintf@plt>
call   1060 <strlen@plt>
mov    %eax,0x20(%rsp)
mov    0x2d99(%rip),%r12
lea    0x18(%rsp),%r14
lea    0xdb8(%rip),%r15
jmp    1270 <main+0xc0>
cs nopw 0x0(%rax,%rax,1)
nop
addl   $0x989680,0x8(%rsp)
mov    %rbx,%rdi
call   1080 <tsnorm@plt>
mov    (%r12),%esi
xor    %edi,%edi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10c0 <clock_nanosleep@plt>
movl   $0xd,0x18(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <write@plt>
test   %eax,%eax
jns    1260 <main+0xb0>
mov    %r15,%rdi
call   10a0 <perror@plt>
jmp    1260 <main+0xb0>

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
%struct.sockaddr_can = type { i32, i32 }
%struct.can_frame = type { i32, i32, i32 }
%struct.timespec = type { 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"
@TIMER_ABSTIME = global i32 0
@.str.2 = [10 x i8] c"bad write\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 = alloca %struct.timespec
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @CAN_RAW
%9 = tail call i32 @socket(i32 29, i32 %7, i32 %8)
%10 = bitcast %struct.ifreq* %3 to i8*
%11 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @SIOCGIFINDEX
%13 = call i32 @ioctl(i32 %9, i32 %12, %struct.ifreq* %3)
%14 = bitcast %struct.sockaddr_can* %4 to i8*
%15 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i64 0, i32 0
store i32 29, i32* %15
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i64 0, i32 1
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_can* %4 to %struct.sockaddr*
%20 = call i32 @bind(i32 %9, %struct.sockaddr* %19, i32 8)
%21 = bitcast %struct.can_frame* %5 to i8*
%22 = bitcast %struct.timespec* %6 to i8*
%23 = call i32 @clock_gettime(i32 0, %struct.timespec* %6)
%24 = call i32 @sprintf(i32 undef, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @strlen(i32 undef)
%26 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i64 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i64 0, i32 0
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
br label %29
29: ; preds = %36, %2
%30 = load i32, i32* @TIMER_ABSTIME
%31 = call i32 @clock_nanosleep(i32 0, i32 %30, %struct.timespec* %6, i32* null)
store i32 13, i32* %27
%32 = call i32 @write(i32 %9, %struct.can_frame* %5, i32 12)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %29, %34
%37 = load i32, i32* %28
%38 = add nsw i32 %37, 10000000
store i32 %38, i32* %28
%39 = call i32 @tsnorm(%struct.timespec* %6)
br label %29
}