#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ sa_data;
};
struct sockaddr_ll {
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  TYPE_1__ ifr_hwaddr;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ sll;
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFHWADDR;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct sockaddr_ll *);
int /*<<< orphan*/ memcpy(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int create_socket(char *device, int *sockfd, unsigned char *mac) {
  int sock;
  struct ifreq ifr;
  struct sockaddr_ll sll;
  memset(&ifr, 0, sizeof(ifr));
  memset(&sll, 0, sizeof(sll));
  sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sock == 0) {
    printf("ERR: socket creation for device: %s\n", device);
    return -1;
  }
  strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
    memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
  } else {
    perror("error on MAC address read");
    return -1;
  }
  if (ioctl(sock, SIOCGIFINDEX, &ifr) == -1) {
    printf(" ERR: ioctl failed for device: %s\n", device);
    return -1;
  }
  sll.sll_family = AF_PACKET;
  sll.sll_ifindex = ifr.ifr_ifindex;
  sll.sll_protocol = htons(ETH_P_ALL);
  if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) == -1) {
    printf("ERR: bind failed for device: %s\n", device);
    return -1;
  }
  *sockfd = sock;
  return 0;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x60(%rbp),%edi
mov    -0x5c(%rbp),%esi
mov    %eax,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1220 <create_socket+0x90>
mov    -0x10(%rbp),%rsi
lea    0xdf1(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <create_socket+0x190>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%eax
jne    1265 <create_socket+0xd5>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    $0x6,%edx
call   1080 <memcpy@plt>
jmp    127d <create_socket+0xed>
lea    0xdb9(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <create_socket+0x190>
mov    -0x24(%rbp),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12b7 <create_socket+0x127>
mov    -0x10(%rbp),%rsi
lea    0xd99(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <create_socket+0x190>
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x58(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x58(%rbp),%rsi
mov    $0x18,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1310 <create_socket+0x180>
mov    -0x10(%rbp),%rsi
lea    0xd63(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <create_socket+0x190>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%ebp
mov    0x2e07(%rip),%rax
mov    (%rax),%ebx
mov    0x2dee(%rip),%r13
mov    0x0(%r13),%edi
call   1040 <htons@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   10b0 <socket@plt>
test   %eax,%eax
je     1298 <create_socket+0x108>
mov    %eax,%ebp
mov    0x1c(%rsp),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
jne    12cd <create_socket+0x13d>
mov    0x18(%rsp),%esi
mov    %r12,%rdi
mov    $0x6,%edx
call   1080 <memcpy@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12a1 <create_socket+0x111>
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0xc(%rsp),%eax
mov    %eax,0x28(%rsp)
mov    0x0(%r13),%edi
call   1040 <htons@plt>
mov    %eax,0x20(%rsp)
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     12aa <create_socket+0x11a>
mov    %ebp,(%r14)
xor    %eax,%eax
jmp    12be <create_socket+0x12e>
lea    0xd61(%rip),%rdi
jmp    12b1 <create_socket+0x121>
lea    0xd97(%rip),%rdi
jmp    12b1 <create_socket+0x121>
lea    0xdb1(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd51(%rip),%rdi
call   10a0 <perror@plt>
jmp    12b9 <create_socket+0x129>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, i32, i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_ll = type { i32, i32, i32, i32, %struct.TYPE_2__, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [37 x i8] c"ERR: socket creation for device: %s\0A\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [26 x i8] c"error on MAC address read\00"
@SIOCGIFINDEX = global i32 0
@.str.2 = [35 x i8] c" ERR: ioctl failed for device: %s\0A\00"
@AF_PACKET = global i32 0
@.str.3 = [33 x i8] c"ERR: bind failed for device: %s\0A\00"
define i32 @create_socket(i8* %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.ifreq
%10 = alloca %struct.sockaddr_ll
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
%11 = bitcast %struct.ifreq* %9 to %struct.sockaddr_ll*
%12 = call i32 @memset(%struct.sockaddr_ll* %11, i32 0, i32 24)
%13 = call i32 @memset(%struct.sockaddr_ll* %10, i32 0, i32 24)
%14 = load i32, i32* @PF_PACKET
%15 = load i32, i32* @SOCK_RAW
%16 = load i32, i32* @ETH_P_ALL
%17 = call i32 @htons(i32 %16)
%18 = call i32 @socket(i32 %14, i32 %15, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 -1, i32* %4
br label %70
24: ; preds = %3
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 5
%26 = load i32, i32* %25
%27 = load i8*, i8** %5
%28 = call i32 @strncpy(i32 %26, i8* %27, i32 4)
%29 = load i32, i32* %8
%30 = load i32, i32* @SIOCGIFHWADDR
%31 = bitcast %struct.ifreq* %9 to %struct.sockaddr_ll*
%32 = call i32 @ioctl(i32 %29, i32 %30, %struct.sockaddr_ll* %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %24
%35 = load i8*, i8** %7
%36 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 4
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @memcpy(i8* %35, i32 %38, i32 6)
br label %42
40: ; preds = %24
%41 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %70
42: ; preds = %34
%43 = load i32, i32* %8
%44 = load i32, i32* @SIOCGIFINDEX
%45 = bitcast %struct.ifreq* %9 to %struct.sockaddr_ll*
%46 = call i32 @ioctl(i32 %43, i32 %44, %struct.sockaddr_ll* %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = load i8*, i8** %5
%50 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %49)
store i32 -1, i32* %4
br label %70
51: ; preds = %42
%52 = load i32, i32* @AF_PACKET
%53 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %10, i32 0, i32 3
store i32 %52, i32* %53
%54 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i32 0, i32 1
%55 = load i32, i32* %54
%56 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %10, i32 0, i32 2
store i32 %55, i32* %56
%57 = load i32, i32* @ETH_P_ALL
%58 = call i32 @htons(i32 %57)
%59 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %10, i32 0, i32 0
store i32 %58, i32* %59
%60 = load i32, i32* %8
%61 = bitcast %struct.sockaddr_ll* %10 to %struct.sockaddr*
%62 = call i32 @bind(i32 %60, %struct.sockaddr* %61, i32 24)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %51
%65 = load i8*, i8** %5
%66 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %65)
store i32 -1, i32* %4
br label %70
67: ; preds = %51
%68 = load i32, i32* %8
%69 = load i32*, i32** %6
store i32 %68, i32* %69
store i32 0, i32* %4
br label %70
70: ; preds = %67, %64, %48, %40, %21
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32, i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_ll = type { i32, i32, i32, i32, %struct.TYPE_2__, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [37 x i8] c"ERR: socket creation for device: %s\0A\00"
@SIOCGIFHWADDR = global i32 0
@.str.1 = [26 x i8] c"error on MAC address read\00"
@SIOCGIFINDEX = global i32 0
@.str.2 = [35 x i8] c" ERR: ioctl failed for device: %s\0A\00"
@AF_PACKET = global i32 0
@.str.3 = [33 x i8] c"ERR: bind failed for device: %s\0A\00"
define i32 @create_socket(i8* %0, i32* nocapture writeonly %1, i8* %2) {
%4 = alloca %struct.ifreq
%5 = alloca %struct.sockaddr_ll
%6 = bitcast %struct.ifreq* %4 to i8*
%7 = bitcast %struct.sockaddr_ll* %5 to i8*
%8 = bitcast %struct.ifreq* %4 to %struct.sockaddr_ll*
%9 = call i32 @memset(%struct.sockaddr_ll* %8, i32 0, i32 24)
%10 = call i32 @memset(%struct.sockaddr_ll* %5, i32 0, i32 24)
%11 = load i32, i32* @PF_PACKET
%12 = load i32, i32* @SOCK_RAW
%13 = load i32, i32* @ETH_P_ALL
%14 = call i32 @htons(i32 %13)
%15 = call i32 @socket(i32 %11, i32 %12, i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %52
19: ; preds = %3
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 5
%21 = load i32, i32* %20
%22 = call i32 @strncpy(i32 %21, i8* %0, i32 4)
%23 = load i32, i32* @SIOCGIFHWADDR
%24 = call i32 @ioctl(i32 %15, i32 %23, %struct.sockaddr_ll* %8)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %19
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 4, i32 0
%28 = load i32, i32* %27
%29 = call i32 @memcpy(i8* %2, i32 %28, i32 6)
%30 = load i32, i32* @SIOCGIFINDEX
%31 = call i32 @ioctl(i32 %15, i32 %30, %struct.sockaddr_ll* %8)
%32 = icmp eq i32 %31, -1
br i1 %32, label %35, label %37
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %52
35: ; preds = %26
%36 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %52
37: ; preds = %26
%38 = load i32, i32* @AF_PACKET
%39 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i64 0, i32 3
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i64 0, i32 2
store i32 %41, i32* %42
%43 = load i32, i32* @ETH_P_ALL
%44 = call i32 @htons(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i64 0, i32 0
store i32 %44, i32* %45
%46 = bitcast %struct.sockaddr_ll* %5 to %struct.sockaddr*
%47 = call i32 @bind(i32 %15, %struct.sockaddr* %46, i32 24)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %37
%50 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %52
51: ; preds = %37
store i32 %15, i32* %1
br label %52
52: ; preds = %51, %49, %35, %33, %17
%53 = phi i32 [ -1, %17 ], [ -1, %35 ], [ -1, %49 ], [ 0, %51 ], [ -1, %33 ]
ret i32 %53
}