#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_ifindex;
  int /*<<< orphan*/ can_family;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int 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 /*<<< orphan*/ PF_CAN;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ 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(int /*<<< orphan*/, char *);
int write(int, struct can_frame *, int);
int main(void) {
  int s;
  int nbytes;
  struct sockaddr_can addr;
  struct can_frame frame;
  struct ifreq ifr;
  char *ifname = "can0";
  if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    perror("Error while opening socket");
    return -1;
  }
  strcpy(ifr.ifr_name, ifname);
  ioctl(s, SIOCGIFINDEX, &ifr);
  addr.can_family = AF_CAN;
  addr.can_ifindex = ifr.ifr_ifindex;
  printf("%s at index %d\n", ifname, ifr.ifr_ifindex);
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("Error in socket bind");
    return -2;
  }
  frame.can_id = 0x01;
  frame.can_dlc = 2;
  frame.data[0] = 0x11;
  frame.data[1] = 0x22;
  nbytes = write(s, &frame, sizeof(struct can_frame));
  printf("Wrote %d bytes\n", nbytes);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ce <main+0x5e>
lea    0xe48(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1298 <main+0x128>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1249 <main+0xd9>
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1298 <main+0x128>
movl   $0x1,-0x28(%rbp)
movl   $0x2,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x11,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x22,0x4(%rax)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1040 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
test   %eax,%eax
jns    119e <main+0x3e>
lea    0xe73(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    120c <main+0xac>
mov    %eax,%ebx
lea    0xe59(%rip),%r14
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x10(%rsp),%edx
mov    %edx,0x8(%rsp)
lea    0xe40(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
mov    $0xfffffffe,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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 }
@.str = [5 x i8] c"can0\00"
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str.1 = [27 x i8] c"Error while opening socket\00"
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
@.str.2 = [16 x i8] c"%s at index %d\0A\00"
@.str.3 = [21 x i8] c"Error in socket bind\00"
@.str.4 = [16 x i8] c"Wrote %d bytes\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_can
%5 = alloca %struct.can_frame
%6 = alloca %struct.ifreq
%7 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %7
%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 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %51
15: ; preds = %0
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %7
%19 = call i32 @strcpy(i32 %17, i8* %18)
%20 = load i32, i32* %2
%21 = load i32, i32* @SIOCGIFINDEX
%22 = call i32 @ioctl(i32 %20, i32 %21, %struct.ifreq* %6)
%23 = load i32, i32* @AF_CAN
%24 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i8*, i8** %7
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %28, i32 %30)
%32 = load i32, i32* %2
%33 = bitcast %struct.sockaddr_can* %4 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 8)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %15
%37 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %1
br label %51
38: ; preds = %15
%39 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i32 0, i32 0
store i32 1, i32* %39
%40 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i32 0, i32 1
store i32 2, i32* %40
%41 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i32 0, i32 2
%42 = load i32*, i32** %41
%43 = getelementptr inbounds i32, i32* %42, i64 0
store i32 17, i32* %43
%44 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %5, i32 0, i32 2
%45 = load i32*, i32** %44
%46 = getelementptr inbounds i32, i32* %45, i64 1
store i32 34, i32* %46
%47 = load i32, i32* %2
%48 = call i32 @write(i32 %47, %struct.can_frame* %5, i32 16)
store i32 %48, i32* %3
%49 = load i32, i32* %3
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %49)
store i32 0, i32* %1
br label %51
51: ; preds = %38, %36, %13
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_can = type { i32, i32 }
%struct.ifreq = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"can0\00"
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str.1 = [27 x i8] c"Error while opening socket\00"
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
@.str.2 = [16 x i8] c"%s at index %d\0A\00"
@.str.3 = [21 x i8] c"Error in socket bind\00"
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 slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %26
12: ; preds = %0
%13 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @SIOCGIFINDEX
%15 = call i32 @ioctl(i32 %8, i32 %14, %struct.ifreq* %2)
%16 = load i32, i32* @AF_CAN
%17 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %1, i64 0, i32 0
store i32 %19, i32* %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %19)
%22 = bitcast %struct.sockaddr_can* %1 to %struct.sockaddr*
%23 = call i64 @bind(i32 %8, %struct.sockaddr* %22, i32 8)
%24 = icmp slt i64 %23, 0
%25 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %12, %10
%27 = phi i32 [ -1, %10 ], [ -2, %12 ]
ret i32 %27
}