#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 /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ can_ifindex;
  int /*<<< orphan*/ can_family;
  int /*<<< orphan*/ ifr_name;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ can_ifindex;
  int /*<<< orphan*/ can_family;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ifr;
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*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct sockaddr_can *);
int /*<<< orphan*/ memset(struct sockaddr_can *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int socketcan_open(const char *iface) {
  int fd;
  struct sockaddr_can addr;
  struct ifreq ifr;
  fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  if (fd < 0)
    return -1;
  memset(&ifr, 0, sizeof(ifr));
  strcpy(ifr.ifr_name, iface);
  if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0)
    goto error;
  memset(&addr, 0, sizeof(addr));
  addr.can_family = AF_CAN;
  addr.can_ifindex = ifr.ifr_ifindex;
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    goto error;
  return fd;
error:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<socketcan_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a5 <socketcan_open+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <socketcan_open+0xe1>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11e5 <socketcan_open+0x85>
jmp    1232 <socketcan_open+0xd2>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1227 <socketcan_open+0xc7>
jmp    1232 <socketcan_open+0xd2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1241 <socketcan_open+0xe1>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketcan_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     121e <socketcan_open+0xbe>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x14(%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1217 <socketcan_open+0xb7>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x1c(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1217 <socketcan_open+0xb7>
mov    %ebp,%r14d
jmp    121e <socketcan_open+0xbe>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_can = type { i32, i32, i32, i32 }
%struct.ifreq = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
define i32 @socketcan_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_can
%6 = alloca %struct.ifreq
store i8* %0, i8** %3
%7 = load i32, i32* @PF_CAN
%8 = load i32, i32* @SOCK_RAW
%9 = load i32, i32* @CAN_RAW
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %44
14: ; preds = %1
%15 = bitcast %struct.ifreq* %6 to %struct.sockaddr_can*
%16 = call i32 @memset(%struct.sockaddr_can* %15, i32 0, i32 16)
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 3
%18 = load i32, i32* %17
%19 = load i8*, i8** %3
%20 = call i32 @strcpy(i32 %18, i8* %19)
%21 = load i32, i32* %4
%22 = load i32, i32* @SIOCGIFINDEX
%23 = bitcast %struct.ifreq* %6 to %struct.sockaddr_can*
%24 = call i64 @ioctl(i32 %21, i32 %22, %struct.sockaddr_can* %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %14
br label %41
27: ; preds = %14
%28 = call i32 @memset(%struct.sockaddr_can* %5, i32 0, i32 16)
%29 = load i32, i32* @AF_CAN
%30 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %5, i32 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %5, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* %4
%35 = bitcast %struct.sockaddr_can* %5 to %struct.sockaddr*
%36 = call i64 @bind(i32 %34, %struct.sockaddr* %35, i32 16)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %27
br label %41
39: ; preds = %27
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %44
41: ; preds = %38, %26
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %2
br label %44
44: ; preds = %41, %39, %13
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_can = type { i32, i32, i32, i32 }
%struct.ifreq = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
define i32 @socketcan_open(i8* %0) {
%2 = alloca %struct.sockaddr_can
%3 = alloca %struct.ifreq
%4 = bitcast %struct.sockaddr_can* %2 to i8*
%5 = bitcast %struct.ifreq* %3 to i8*
%6 = load i32, i32* @PF_CAN
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @CAN_RAW
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %32, label %11
11: ; preds = %1
%12 = bitcast %struct.ifreq* %3 to %struct.sockaddr_can*
%13 = call i32 @memset(%struct.sockaddr_can* %12, i32 0, i32 16)
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 3
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* %0)
%17 = load i32, i32* @SIOCGIFINDEX
%18 = call i64 @ioctl(i32 %9, i32 %17, %struct.sockaddr_can* %12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %30, label %20
20: ; preds = %11
%21 = call i32 @memset(%struct.sockaddr_can* %2, i32 0, i32 16)
%22 = load i32, i32* @AF_CAN
%23 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %2, i64 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %2, i64 0, i32 1
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_can* %2 to %struct.sockaddr*
%28 = call i64 @bind(i32 %9, %struct.sockaddr* %27, i32 16)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %20, %11
%31 = call i32 @close(i32 %9)
br label %32
32: ; preds = %20, %1, %30
%33 = phi i32 [ -1, %30 ], [ -1, %1 ], [ %9, %20 ]
ret i32 %33
}