#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_CAN;
int /*<<< orphan*/ CAN_RAW;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_CAN;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int can_create_socket(const char *name) {
  int fd;
  fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  if (fd < 0) {
    perror("Creating can socket failed");
    return -1;
  }
  struct ifreq ifr;
  strlcpy(ifr.ifr_name, name, IFNAMSIZ);
  if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
    perror("Getting interface number failed");
    return -1;
  }
  struct sockaddr_can addr;
  addr.can_family = AF_CAN;
  addr.can_ifindex = ifr.ifr_ifindex;
  if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_can)) < 0) {
    perror("Binding can socket failed");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<can_create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a1 <can_create_socket+0x51>
lea    0xe70(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <can_create_socket+0xe8>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
call   1040 <strlcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11ed <can_create_socket+0x9d>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <can_create_socket+0xe8>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    1232 <can_create_socket+0xe2>
lea    0xe1a(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <can_create_socket+0xe8>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_create_socket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11e0 <can_create_socket+0x90>
mov    %eax,%ebx
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1040 <strlcpy@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11e9 <can_create_socket+0x99>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     11f2 <can_create_socket+0xa2>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe19(%rip),%rdi
jmp    11f9 <can_create_socket+0xa9>
lea    0xe2b(%rip),%rdi
jmp    11f9 <can_create_socket+0xa9>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11d6 <can_create_socket+0x86>

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 = [27 x i8] c"Creating can socket failed\00"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [32 x i8] c"Getting interface number failed\00"
@AF_CAN = global i32 0
@.str.2 = [26 x i8] c"Binding can socket failed\00"
define i32 @can_create_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.ifreq
%6 = alloca %struct.sockaddr_can
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 %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
15: ; preds = %1
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strlcpy(i32 %17, i8* %18, i32 %19)
%21 = load i32, i32* %4
%22 = load i32, i32* @SIOCGIFINDEX
%23 = call i64 @ioctl(i32 %21, i32 %22, %struct.ifreq* %5)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
27: ; preds = %15
%28 = load i32, i32* @AF_CAN
%29 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %6, i32 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %6, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_can* %6 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 8)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %41
39: ; preds = %27
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %37, %25, %13
%42 = load i32, i32* %2
ret i32 %42
}

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 = [27 x i8] c"Creating can socket failed\00"
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [32 x i8] c"Getting interface number failed\00"
@AF_CAN = global i32 0
@.str.2 = [26 x i8] c"Binding can socket failed\00"
define i32 @can_create_socket(i8* %0) {
%2 = alloca %struct.ifreq
%3 = alloca %struct.sockaddr_can
%4 = load i32, i32* @PF_CAN
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @CAN_RAW
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %36
11: ; preds = %1
%12 = bitcast %struct.ifreq* %2 to i8*
%13 = load i32, i32* @IFNAMSIZ
%14 = tail call i32 @strlcpy(i32 undef, i8* %0, i32 %13)
%15 = load i32, i32* @SIOCGIFINDEX
%16 = call i64 @ioctl(i32 %7, i32 %15, %struct.ifreq* %2)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %34
20: ; preds = %11
%21 = bitcast %struct.sockaddr_can* %3 to i8*
%22 = load i32, i32* @AF_CAN
%23 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_can* %3 to %struct.sockaddr*
%28 = call i64 @bind(i32 %7, %struct.sockaddr* %27, i32 8)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %20, %30
%33 = phi i32 [ -1, %30 ], [ %7, %20 ]
br label %34
34: ; preds = %32, %18
%35 = phi i32 [ -1, %18 ], [ %33, %32 ]
br label %36
36: ; preds = %34, %9
%37 = phi i32 [ -1, %9 ], [ %35, %34 ]
ret i32 %37
}