#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;
  scalar_t__ *ifr_name;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_CAN;
int /*<<< orphan*/ CAN_RAW;
int /*<<< orphan*/ PF_CAN;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
int can_open(const char *busname) {
  int i, sock;
  struct ifreq ifr;
  struct sockaddr_can sa;
  sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  if (sock <= 0) {
    perror("cannot create socket");
    return -1;
  }
  strncpy(ifr.ifr_name, busname, sizeof(ifr.ifr_name));
  ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = 0;
  if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
    perror("cannot ioctl for interface number");
    close(sock);
    return -1;
  }
  sa.can_family = AF_CAN;
  sa.can_ifindex = ifr.ifr_ifindex;
  i = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
  if (i < 0) {
    perror("cannot bind");
    close(sock);
    return -1;
  }
  printf("can bus opened, socket=%d, ifindex=%d\n", sock, ifr.ifr_ifindex);
  return sock;
}


x86 assembly (clang, -O0):
<can_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11c1 <can_open+0x51>
lea    0xe50(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <can_open+0x116>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,0x38(%rax)
mov    -0x18(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    121e <can_open+0xae>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <can_open+0x116>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    126e <can_open+0xfe>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1286 <can_open+0x116>
mov    -0x18(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_open>:
push   %rbx
mov    %rdi,%rbx
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
call   1050 <socket@plt>
test   %eax,%eax
jg     116b <can_open+0x3b>
lea    0xea1(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i64* }
%struct.sockaddr_can = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str = [21 x i8] c"cannot create socket\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [34 x i8] c"cannot ioctl for interface number\00"
@AF_CAN = global i32 0
@.str.2 = [12 x i8] c"cannot bind\00"
@.str.3 = [39 x i8] c"can bus opened, socket=%d, ifindex=%d\0A\00"
define i32 @can_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca %struct.sockaddr_can
store i8* %0, i8** %3
%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* %5
%12 = load i32, i32* %5
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %53
16: ; preds = %1
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%18 = load i64*, i64** %17
%19 = load i8*, i8** %3
%20 = call i32 @strncpy(i64* %18, i8* %19, i32 8)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%22 = load i64*, i64** %21
%23 = getelementptr inbounds i64, i64* %22, i64 7
store i64 0, i64* %23
%24 = load i32, i32* %5
%25 = load i32, i32* @SIOCGIFINDEX
%26 = call i64 @ioctl(i32 %24, i32 %25, %struct.ifreq* %6)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %2
br label %53
32: ; preds = %16
%33 = load i32, i32* @AF_CAN
%34 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %7, i32 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %7, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %5
%39 = bitcast %struct.sockaddr_can* %7 to %struct.sockaddr*
%40 = call i32 @bind(i32 %38, %struct.sockaddr* %39, i32 8)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %32
%44 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* %5
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %2
br label %53
47: ; preds = %32
%48 = load i32, i32* %5
%49 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %48, i32 %50)
%52 = load i32, i32* %5
store i32 %52, i32* %2
br label %53
53: ; preds = %47, %43, %28, %14
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str = [21 x i8] c"cannot create socket\00"
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
define i32 @can_open(i8* %0) {
%2 = load i32, i32* @PF_CAN
%3 = load i32, i32* @SOCK_RAW
%4 = load i32, i32* @CAN_RAW
%5 = tail call i32 @socket(i32 %2, i32 %3, i32 %4)
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
ret i32 -1
9: ; preds = %1
%10 = tail call i32 @strncpy(i64* undef, i8* %0, i32 8)
unreachable
}