#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 packet_mreq {
  int mr_ifindex;
  int /*<<< orphan*/ mr_address;
  int /*<<< orphan*/ mr_alen;
  int /*<<< orphan*/ mr_type;
};
typedef int /*<<< orphan*/ mr;
typedef int /*<<< orphan*/ __u8;

/* Variables and functions */
int /*<<< orphan*/ ETHER_ADDR_LEN;
int /*<<< orphan*/ PACKET_ADD_MEMBERSHIP;
int /*<<< orphan*/ PACKET_DROP_MEMBERSHIP;
int /*<<< orphan*/ PACKET_MR_MULTICAST;
int /*<<< orphan*/ PACKET_MR_UNICAST;
int /*<<< orphan*/ SOL_PACKET;
int errno;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, int /*<<< orphan*/ const *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct packet_mreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sa_log_debug(char *, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct packet_mreq *, int);
int fip_socket_add_addr(int s, int ifindex, bool add, const __u8 *mac,
                        bool multi) {
  struct packet_mreq mr;
  int rc = 0;
  memset(&mr, 0, sizeof(mr));
  mr.mr_ifindex = ifindex;
  mr.mr_type = multi ? PACKET_MR_MULTICAST : PACKET_MR_UNICAST;
  mr.mr_alen = ETHER_ADDR_LEN;
  memcpy(mr.mr_address, mac, ETHER_ADDR_LEN);
  if (setsockopt(s, SOL_PACKET,
                 add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP, &mr,
                 sizeof(mr)) < 0) {
    sa_log_debug("PACKET_%s_MEMBERSHIP:failed\n", add ? "ADD" : "DROP");
    rc = -errno;
  }
  return rc;
}


x86 assembly (clang, -O0):
<fip_socket_add_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1191 <fip_socket_add_addr+0x51>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
jmp    119d <fip_socket_add_addr+0x5d>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11f1 <fip_socket_add_addr+0xb1>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    11fd <fip_socket_add_addr+0xbd>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x3c(%rbp),%esi
mov    -0x40(%rbp),%edi
mov    -0x44(%rbp),%edx
lea    -0x30(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1251 <fip_socket_add_addr+0x111>
mov    -0xc(%rbp),%ecx
lea    0xdf8(%rip),%rsi
lea    0xded(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xdc2(%rip),%rdi
call   1040 <sa_log_debug@plt>
xor    %eax,%eax
mov    0x2d7c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fip_socket_add_addr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebx
mov    %rcx,%r13
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edi,%r12d
lea    0x8(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %r14d,0x8(%rsp)
test   %ebx,%ebx
jne    1182 <fip_socket_add_addr+0x42>
mov    0x2e40(%rip),%rax
jmp    1189 <fip_socket_add_addr+0x49>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
mov    %edx,0x10(%rsp)
mov    0xc(%rsp),%edi
mov    %r13,%rsi
call   1060 <memcpy@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
test   %r15d,%r15d
jne    11bf <fip_socket_add_addr+0x7f>
mov    0x2e1b(%rip),%rax
jmp    11c6 <fip_socket_add_addr+0x86>
mov    0x2df2(%rip),%rax
lea    0x8(%rsp),%rcx
mov    (%rax),%edx
mov    %r12d,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jns    120c <fip_socket_add_addr+0xcc>
test   %r15d,%r15d
lea    0xe37(%rip),%rax
lea    0xe2c(%rip),%rsi
cmove  %rax,%rsi
lea    0xe04(%rip),%rdi
call   1040 <sa_log_debug@plt>
mov    0x2dc0(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.packet_mreq = type { i32, i32, i32, i32 }
@PACKET_MR_MULTICAST = global i32 0
@PACKET_MR_UNICAST = global i32 0
@ETHER_ADDR_LEN = global i32 0
@SOL_PACKET = global i32 0
@PACKET_ADD_MEMBERSHIP = global i32 0
@PACKET_DROP_MEMBERSHIP = global i32 0
@.str = [29 x i8] c"PACKET_%s_MEMBERSHIP:failed\0A\00"
@.str.1 = [4 x i8] c"ADD\00"
@.str.2 = [5 x i8] c"DROP\00"
@errno = global i32 0
define i32 @fip_socket_add_addr(i32 %0, i32 %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca %struct.packet_mreq
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32 %4, i32* %10
store i32 0, i32* %12
%13 = call i32 @memset(%struct.packet_mreq* %11, i32 0, i32 16)
%14 = load i32, i32* %7
%15 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %11, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %10
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %5
%19 = load i32, i32* @PACKET_MR_MULTICAST
br label %22
20: ; preds = %5
%21 = load i32, i32* @PACKET_MR_UNICAST
br label %22
22: ; preds = %20, %18
%23 = phi i32 [ %19, %18 ], [ %21, %20 ]
%24 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %11, i32 0, i32 3
store i32 %23, i32* %24
%25 = load i32, i32* @ETHER_ADDR_LEN
%26 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %11, i32 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %11, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i32*, i32** %9
%30 = load i32, i32* @ETHER_ADDR_LEN
%31 = call i32 @memcpy(i32 %28, i32* %29, i32 %30)
%32 = load i32, i32* %6
%33 = load i32, i32* @SOL_PACKET
%34 = load i32, i32* %8
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %22
%37 = load i32, i32* @PACKET_ADD_MEMBERSHIP
br label %40
38: ; preds = %22
%39 = load i32, i32* @PACKET_DROP_MEMBERSHIP
br label %40
40: ; preds = %38, %36
%41 = phi i32 [ %37, %36 ], [ %39, %38 ]
%42 = call i64 @setsockopt(i32 %32, i32 %33, i32 %41, %struct.packet_mreq* %11, i32 16)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %40
%45 = load i32, i32* %8
%46 = icmp ne i32 %45, 0
%47 = zext i1 %46 to i64
%48 = select i1 %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%49 = call i32 @sa_log_debug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %48)
%50 = load i32, i32* @errno
%51 = sub nsw i32 0, %50
store i32 %51, i32* %12
br label %52
52: ; preds = %44, %40
%53 = load i32, i32* %12
ret i32 %53
}

LLVM IR (-O2):
%struct.packet_mreq = type { i32, i32, i32, i32 }
@PACKET_MR_MULTICAST = global i32 0
@PACKET_MR_UNICAST = global i32 0
@ETHER_ADDR_LEN = global i32 0
@SOL_PACKET = global i32 0
@PACKET_ADD_MEMBERSHIP = global i32 0
@PACKET_DROP_MEMBERSHIP = global i32 0
@.str = [29 x i8] c"PACKET_%s_MEMBERSHIP:failed\0A\00"
@.str.1 = [4 x i8] c"ADD\00"
@.str.2 = [5 x i8] c"DROP\00"
@errno = global i32 0
define i32 @fip_socket_add_addr(i32 %0, i32 %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca %struct.packet_mreq
%7 = bitcast %struct.packet_mreq* %6 to i8*
%8 = call i32 @memset(%struct.packet_mreq* %6, i32 0, i32 16)
%9 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %6, i64 0, i32 0
store i32 %1, i32* %9
%10 = icmp eq i32 %4, 0
%11 = load i32, i32* @PACKET_MR_MULTICAST
%12 = load i32, i32* @PACKET_MR_UNICAST
%13 = select i1 %10, i32 %12, i32 %11
%14 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %6, i64 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @ETHER_ADDR_LEN
%16 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %6, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %6, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @memcpy(i32 %18, i32* %3, i32 %15)
%20 = load i32, i32* @SOL_PACKET
%21 = icmp eq i32 %2, 0
%22 = load i32, i32* @PACKET_ADD_MEMBERSHIP
%23 = load i32, i32* @PACKET_DROP_MEMBERSHIP
%24 = select i1 %21, i32 %23, i32 %22
%25 = call i64 @setsockopt(i32 %0, i32 %20, i32 %24, %struct.packet_mreq* %6, i32 16)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %5
%28 = select i1 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%29 = call i32 @sa_log_debug(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
br label %32
32: ; preds = %27, %5
%33 = phi i32 [ %31, %27 ], [ 0, %5 ]
ret i32 %33
}