#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*/ 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;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int lxcan_open(const char *name) {
  struct sockaddr_can addr;
  struct ifreq ifr;
  int s;
  if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
    return -1;
  strcpy(ifr.ifr_name, "can0");
  if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
    close(s);
    return -1;
  }
  addr.can_family = AF_CAN;
  addr.can_ifindex = ifr.ifr_ifindex;
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<lxcan_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1194 <lxcan_open+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <lxcan_open+0xcd>
mov    -0x1c(%rbp),%edi
lea    0xe62(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11d6 <lxcan_open+0x86>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <lxcan_open+0xcd>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1217 <lxcan_open+0xc7>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <lxcan_open+0xcd>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lxcan_open>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11dd <lxcan_open+0x8d>
mov    %eax,%ebp
lea    0xe78(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     11d6 <lxcan_open+0x86>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11d6 <lxcan_open+0x86>
mov    %ebp,%ebx
jmp    11dd <lxcan_open+0x8d>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%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
@.str = [5 x i8] c"can0\00"
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
define i32 @lxcan_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_can
%5 = alloca %struct.ifreq
%6 = alloca i32
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* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %39
13: ; preds = %1
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %6
%18 = load i32, i32* @SIOCGIFINDEX
%19 = call i64 @ioctl(i32 %17, i32 %18, %struct.ifreq* %5)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %2
br label %39
24: ; preds = %13
%25 = load i32, i32* @AF_CAN
%26 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_can* %4 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 8)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %2
br label %39
37: ; preds = %24
%38 = load i32, i32* %6
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %34, %21, %12
%40 = load i32, i32* %2
ret i32 %40
}

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
@.str = [5 x i8] c"can0\00"
@SIOCGIFINDEX = global i32 0
@AF_CAN = global i32 0
define i32 @lxcan_open(i8* nocapture readnone %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 %27, label %11
11: ; preds = %1
%12 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @SIOCGIFINDEX
%14 = call i64 @ioctl(i32 %9, i32 %13, %struct.ifreq* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %11
%17 = load i32, i32* @AF_CAN
%18 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_can* %2 to %struct.sockaddr*
%23 = call i64 @bind(i32 %9, %struct.sockaddr* %22, i32 8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %16, %11
%26 = call i32 @close(i32 %9)
br label %27
27: ; preds = %25, %16, %1
%28 = phi i32 [ -1, %1 ], [ %9, %16 ], [ -1, %25 ]
ret i32 %28
}