#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 mr_alen;
  int /*<<< orphan*/ mr_address;
  int /*<<< orphan*/ mr_type;
};
typedef int /*<<< orphan*/ mreq;

/* Variables and functions */
int /*<<< orphan*/ PACKET_ADD_MEMBERSHIP;
int /*<<< orphan*/ PACKET_MR_MULTICAST;
int /*<<< orphan*/ SOL_PACKET;
int errno;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, unsigned char const *, int);
int /*<<< orphan*/ memset(struct packet_mreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int const, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct packet_mreq *, int);
int mcast_register(const int fd, const int ifidx, const unsigned char *addr) {
  struct packet_mreq mreq;
  int ret_val;
  ret_val = 0;
  memset(&mreq, 0, sizeof(mreq));
  mreq.mr_ifindex = ifidx;
  mreq.mr_type = PACKET_MR_MULTICAST;
  mreq.mr_alen = 6;
  memcpy(mreq.mr_address, addr, mreq.mr_alen);
  if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) <
      0) {
    ret_val = errno;
    perror("setsockopt");
    errno = ret_val;
    ret_val = -1;
  }
  return (ret_val);
}


x86 assembly (clang, -O0):
<mcast_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x6,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11ea <mcast_register+0xaa>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%ecx
mov    0x2df7(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mcast_register>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r15d
mov    %rsp,%r12
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    %ebp,(%rsp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movl   $0x6,0x4(%rsp)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r12,%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11ca <mcast_register+0x8a>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e07(%rip),%rbx
mov    (%rbx),%ebp
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    %ebp,(%rbx)
mov    $0xffffffff,%r14d
jmp    11ba <mcast_register+0x7a>

LLVM IR (-O0):
%struct.packet_mreq = type { i32, i32, i32, i32 }
@PACKET_MR_MULTICAST = global i32 0
@SOL_PACKET = global i32 0
@PACKET_ADD_MEMBERSHIP = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"setsockopt\00"
define i32 @mcast_register(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.packet_mreq
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %8
%9 = call i32 @memset(%struct.packet_mreq* %7, i32 0, i32 16)
%10 = load i32, i32* %5
%11 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %7, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @PACKET_MR_MULTICAST
%13 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %7, i32 0, i32 3
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %7, i32 0, i32 1
store i32 6, i32* %14
%15 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %7, i32 0, i32 2
%16 = load i32, i32* %15
%17 = load i8*, i8** %6
%18 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %7, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i32 %16, i8* %17, i32 %19)
%21 = load i32, i32* %4
%22 = load i32, i32* @SOL_PACKET
%23 = load i32, i32* @PACKET_ADD_MEMBERSHIP
%24 = call i64 @setsockopt(i32 %21, i32 %22, i32 %23, %struct.packet_mreq* %7, i32 16)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %3
%27 = load i32, i32* @errno
store i32 %27, i32* %8
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* %8
store i32 %29, i32* @errno
store i32 -1, i32* %8
br label %30
30: ; preds = %26, %3
%31 = load i32, i32* %8
ret i32 %31
}

LLVM IR (-O2):
%struct.packet_mreq = type { i32, i32, i32, i32 }
@PACKET_MR_MULTICAST = global i32 0
@SOL_PACKET = global i32 0
@PACKET_ADD_MEMBERSHIP = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"setsockopt\00"
define i32 @mcast_register(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.packet_mreq
%5 = bitcast %struct.packet_mreq* %4 to i8*
%6 = call i32 @memset(%struct.packet_mreq* %4, i32 0, i32 16)
%7 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %4, i64 0, i32 0
store i32 %1, i32* %7
%8 = load i32, i32* @PACKET_MR_MULTICAST
%9 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %4, i64 0, i32 3
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %4, i64 0, i32 1
store i32 6, i32* %10
%11 = getelementptr inbounds %struct.packet_mreq, %struct.packet_mreq* %4, i64 0, i32 2
%12 = load i32, i32* %11
%13 = call i32 @memcpy(i32 %12, i8* %2, i32 6)
%14 = load i32, i32* @SOL_PACKET
%15 = load i32, i32* @PACKET_ADD_MEMBERSHIP
%16 = call i64 @setsockopt(i32 %0, i32 %14, i32 %15, %struct.packet_mreq* %4, i32 16)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i32, i32* @errno
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %19, i32* @errno
br label %21
21: ; preds = %18, %3
%22 = phi i32 [ -1, %18 ], [ 0, %3 ]
ret i32 %22
}