#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_family;
  int /*<<< orphan*/ can_ifindex;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int CAN_RAW;
int PF_CAN;
int /*<<< orphan*/ SIOCGIFINDEX;
int SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ logcat(char *, int /*<<< orphan*/);
int socket(int, int, int);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int can_socket_init(char *interface) {
  struct ifreq ifr;
  struct sockaddr_can addr;
  int family = PF_CAN, type = SOCK_RAW, proto = CAN_RAW;
  int s;
  if (interface == NULL)
    return -1;
  s = socket(family, type, proto);
  if (s < 0) {
    logcat("socket: %s\n", strerror(errno));
    return -1;
  }
  addr.can_family = family;
  strcpy(ifr.ifr_name, interface);
  if (ioctl(s, SIOCGIFINDEX, &ifr)) {
    logcat("ioctl: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  addr.can_ifindex = ifr.ifr_ifindex;
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    logcat("bind: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<can_socket_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <can_socket_init+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <can_socket_init+0x142>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1090 <socket@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11fa <can_socket_init+0x8a>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xe17(%rip),%rdi
call   1060 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <can_socket_init+0x142>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     125b <can_socket_init+0xeb>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xdca(%rip),%rdi
call   1060 <logcat@plt>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <can_socket_init+0x142>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    12ac <can_socket_init+0x13c>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%esi
lea    0xd84(%rip),%rdi
call   1060 <logcat@plt>
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <can_socket_init+0x142>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_socket_init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     1242 <can_socket_init+0xd2>
mov    %rdi,%rbx
mov    0x2e3d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1090 <socket@plt>
test   %eax,%eax
js     1202 <can_socket_init+0x92>
mov    %eax,%r15d
mov    %ebp,0x8(%rsp)
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r15d,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
je     1220 <can_socket_init+0xb0>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1b(%rip),%rdi
mov    %eax,%esi
call   1060 <logcat@plt>
mov    %r15d,%edi
call   1050 <close@plt>
jmp    1242 <can_socket_init+0xd2>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde9(%rip),%rdi
mov    %eax,%esi
call   1060 <logcat@plt>
jmp    1242 <can_socket_init+0xd2>
mov    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1250 <can_socket_init+0xe0>
mov    %r15d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdb2(%rip),%rdi
jmp    11f1 <can_socket_init+0x81>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
%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 = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [11 x i8] c"ioctl: %s\0A\00"
@.str.2 = [10 x i8] c"bind: %s\0A\00"
define i32 @can_socket_init(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca %struct.sockaddr_can
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @PF_CAN
store i32 %10, i32* %6
%11 = load i32, i32* @SOCK_RAW
store i32 %11, i32* %7
%12 = load i32, i32* @CAN_RAW
store i32 %12, i32* %8
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %60
16: ; preds = %1
%17 = load i32, i32* %6
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @logcat(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 -1, i32* %2
br label %60
27: ; preds = %16
%28 = load i32, i32* %6
%29 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %5, i32 0, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%31 = load i32, i32* %30
%32 = load i8*, i8** %3
%33 = call i32 @strcpy(i32 %31, i8* %32)
%34 = load i32, i32* %9
%35 = load i32, i32* @SIOCGIFINDEX
%36 = call i64 @ioctl(i32 %34, i32 %35, %struct.ifreq* %4)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %27
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 @logcat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %9
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %2
br label %60
44: ; preds = %27
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %5, i32 0, i32 1
store i32 %46, i32* %47
%48 = load i32, i32* %9
%49 = bitcast %struct.sockaddr_can* %5 to %struct.sockaddr*
%50 = call i64 @bind(i32 %48, %struct.sockaddr* %49, i32 8)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %44
%53 = load i32, i32* @errno
%54 = call i32 @strerror(i32 %53)
%55 = call i32 @logcat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %9
%57 = call i32 @close(i32 %56)
store i32 -1, i32* %2
br label %60
58: ; preds = %44
%59 = load i32, i32* %9
store i32 %59, i32* %2
br label %60
60: ; preds = %58, %52, %38, %23, %15
%61 = load i32, i32* %2
ret i32 %61
}

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