#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 */
typedef int /*<<< orphan*/ tv;
struct timeval {
  int member_1;
  int /*<<< orphan*/ member_0;
};
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;
};
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;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int can_socket_open(char *interface) {
  struct sockaddr_can addr;
  struct ifreq ifr;
  struct timeval tv = {0, 100000};
  int sock, bytes_read;
  if ((sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
    fprintf(stderr, "Error: Failed to create socket.\n");
    return -1;
  }
  setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
  strcpy(ifr.ifr_name, interface);
  ioctl(sock, SIOCGIFINDEX, &ifr);
  addr.can_family = AF_CAN;
  addr.can_ifindex = ifr.ifr_ifindex;
  bind(sock, (struct sockaddr *)&addr, sizeof(addr));
  return sock;
}


x86 assembly (clang, -O0):
<can_socket_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xe8d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11c4 <can_socket_open+0x64>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <can_socket_open+0xd2>
mov    -0x2c(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_socket_open>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
movabs $0x186a000000000,%rax
mov    %rax,0x8(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
test   %eax,%eax
js     1205 <can_socket_open+0xa5>
mov    %eax,%ebx
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
jmp    121f <can_socket_open+0xbf>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_can = type { i32, i32 }
%struct.ifreq = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.can_socket_open.tv = %struct.timeval { i32 0, i32 100000 }
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Error: Failed to create socket.\0A\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
define i32 @can_socket_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_can
%5 = alloca %struct.ifreq
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = bitcast %struct.timeval* %6 to i8*
%10 = load i32, i32* @PF_CAN
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @CAN_RAW
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %40
18: ; preds = %1
%19 = load i32, i32* %7
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_RCVTIMEO
%22 = bitcast %struct.timeval* %6 to i8*
%23 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, i8* %22, i32 8)
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load i8*, i8** %3
%27 = call i32 @strcpy(i32 %25, i8* %26)
%28 = load i32, i32* %7
%29 = load i32, i32* @SIOCGIFINDEX
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.ifreq* %5)
%31 = load i32, i32* @AF_CAN
%32 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* %7
%37 = bitcast %struct.sockaddr_can* %4 to %struct.sockaddr*
%38 = call i32 @bind(i32 %36, %struct.sockaddr* %37, i32 8)
%39 = load i32, i32* %7
store i32 %39, i32* %2
br label %40
40: ; preds = %18, %15
%41 = load i32, i32* %2
ret i32 %41
}

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 = [33 x i8] c"Error: Failed to create socket.\0A\00"
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
define i32 @can_socket_open(i8* %0) {
%2 = alloca %struct.sockaddr_can
%3 = alloca %struct.ifreq
%4 = alloca i64
%5 = bitcast %struct.sockaddr_can* %2 to i8*
%6 = bitcast %struct.ifreq* %3 to i8*
%7 = bitcast i64* %4 to i8*
store i64 429496729600000, i64* %4
%8 = load i32, i32* @PF_CAN
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @CAN_RAW
%11 = tail call i32 @socket(i32 %8, i32 %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %30
16: ; preds = %1
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_RCVTIMEO
%19 = call i32 @setsockopt(i32 %11, i32 %17, i32 %18, i8* %7, i32 8)
%20 = call i32 @strcpy(i32 undef, i8* %0)
%21 = load i32, i32* @SIOCGIFINDEX
%22 = call i32 @ioctl(i32 %11, i32 %21, %struct.ifreq* %3)
%23 = load i32, i32* @AF_CAN
%24 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %2, i64 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %2, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_can* %2 to %struct.sockaddr*
%29 = call i32 @bind(i32 %11, %struct.sockaddr* %28, i32 8)
br label %30
30: ; preds = %16, %13
%31 = phi i32 [ -1, %13 ], [ %11, %16 ]
ret i32 %31
}