#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 */
typedef int uint32_t;
struct sockaddr_storage {
  int /*<<< orphan*/ sa_family;
};
struct sockaddr {
  int /*<<< orphan*/ sa_family;
};
struct group_filter {
  int gf_interface;
  int gf_fmode;
  int gf_numsrc;
  int /*<<< orphan*/ *gf_slist;
  int /*<<< orphan*/ gf_group;
};
typedef int socklen_t;

/* Variables and functions */
int EINVAL;
size_t GROUP_FILTER_SIZE(int);
int /*<<< orphan*/ MCAST_MSFILTER;
int __get_sol(int /*<<< orphan*/, int);
int __libc_use_alloca(size_t);
int /*<<< orphan*/ __set_errno(int);
int __setsockopt(int, int, int /*<<< orphan*/, struct group_filter *, size_t);
scalar_t__ alloca(size_t);
int errno;
int /*<<< orphan*/ free(struct group_filter *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct sockaddr_storage const *,
                          int);
int setsourcefilter(int s, uint32_t interface, const struct sockaddr *group,
                    socklen_t grouplen, uint32_t fmode, uint32_t numsrc,
                    const struct sockaddr_storage *slist) {
  size_t needed = GROUP_FILTER_SIZE(numsrc);
  int use_alloca = __libc_use_alloca(needed);
  struct group_filter *gf;
  if (use_alloca)
    gf = (struct group_filter *)alloca(needed);
  else {
    gf = (struct group_filter *)malloc(needed);
    if (gf == NULL)
      return -1;
  }
  gf->gf_interface = interface;
  memcpy(&gf->gf_group, group, grouplen);
  gf->gf_fmode = fmode;
  gf->gf_numsrc = numsrc;
  memcpy(gf->gf_slist, slist, numsrc * sizeof(struct sockaddr_storage));
  int result;
  int sol = __get_sol(group->sa_family, grouplen);
  if (sol == -1) {
    __set_errno(EINVAL);
    result = -1;
  } else
    result = __setsockopt(s, sol, MCAST_MSFILTER, gf, needed);
  if (!use_alloca) {
    int save_errno = errno;
    free(gf);
    __set_errno(save_errno);
  }
  return result;
}


x86 assembly (clang, -O0):
<setsourcefilter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <GROUP_FILTER_SIZE@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10b0 <__libc_use_alloca@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11e5 <setsourcefilter+0x55>
mov    -0x30(%rbp),%rdi
call   1080 <alloca@plt>
mov    %rax,-0x40(%rbp)
jmp    120e <setsourcefilter+0x7e>
mov    -0x30(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1209 <setsourcefilter+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <setsourcefilter+0x14b>
jmp    120e <setsourcefilter+0x7e>
mov    -0xc(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdi
add    $0x18,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <memcpy@plt>
mov    -0x20(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
call   1060 <__get_sol@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    128f <setsourcefilter+0xff>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    12ae <setsourcefilter+0x11e>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    0x2d4c(%rip),%rax
mov    (%rax),%edx
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1070 <__setsockopt@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    12d5 <setsourcefilter+0x145>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <free@plt>
mov    -0x4c(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setsourcefilter>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9d,%r14d
mov    %r8d,0x10(%rsp)
mov    %ecx,%r12d
mov    %rdx,%r13
mov    %esi,%r15d
mov    %edi,0xc(%rsp)
mov    %r9d,%edi
call   1040 <GROUP_FILTER_SIZE@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   10b0 <__libc_use_alloca@plt>
mov    %eax,0x14(%rsp)
mov    %rbp,%rdi
test   %eax,%eax
je     11db <setsourcefilter+0x4b>
call   1080 <alloca@plt>
mov    %rax,%rbx
jmp    11ec <setsourcefilter+0x5c>
call   10a0 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     128a <setsourcefilter+0xfa>
mov    %r15d,(%rbx)
lea    0x18(%rbx),%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1090 <memcpy@plt>
mov    0x10(%rsp),%eax
mov    %eax,0x4(%rbx)
mov    %r14d,0x8(%rbx)
mov    0x10(%rbx),%rdi
shl    $0x2,%r14d
mov    0x50(%rsp),%rsi
mov    %r14d,%edx
call   1090 <memcpy@plt>
mov    0x0(%r13),%edi
mov    %r12d,%esi
call   1060 <__get_sol@plt>
cmp    $0xffffffff,%eax
je     1254 <setsourcefilter+0xc4>
mov    0x2db2(%rip),%rcx
mov    (%rcx),%edx
mov    0xc(%rsp),%edi
mov    %eax,%esi
mov    %rbx,%rcx
mov    %rbp,%r8
call   1070 <__setsockopt@plt>
mov    %eax,%ebp
cmpl   $0x0,0x14(%rsp)
jne    128f <setsourcefilter+0xff>
jmp    126e <setsourcefilter+0xde>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%ebp
cmpl   $0x0,0x14(%rsp)
jne    128f <setsourcefilter+0xff>
mov    0x2d63(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rdi
call   1050 <free@plt>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
jmp    128f <setsourcefilter+0xff>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.group_filter = type { i32, i32, i32, i32*, i32 }
@EINVAL = global i32 0
@MCAST_MSFILTER = global i32 0
@errno = global i32 0
define i32 @setsourcefilter(i32 %0, i32 %1, %struct.sockaddr* %2, i32 %3, i32 %4, i32 %5, %struct.sockaddr_storage* %6) {
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.sockaddr_storage*
%16 = alloca i64
%17 = alloca i32
%18 = alloca %struct.group_filter*
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
store i32 %0, i32* %9
store i32 %1, i32* %10
store %struct.sockaddr* %2, %struct.sockaddr** %11
store i32 %3, i32* %12
store i32 %4, i32* %13
store i32 %5, i32* %14
store %struct.sockaddr_storage* %6, %struct.sockaddr_storage** %15
%22 = load i32, i32* %14
%23 = call i64 @GROUP_FILTER_SIZE(i32 %22)
store i64 %23, i64* %16
%24 = load i64, i64* %16
%25 = call i32 @__libc_use_alloca(i64 %24)
store i32 %25, i32* %17
%26 = load i32, i32* %17
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %7
%29 = load i64, i64* %16
%30 = call i64 @alloca(i64 %29)
%31 = inttoptr i64 %30 to %struct.group_filter*
store %struct.group_filter* %31, %struct.group_filter** %18
br label %40
32: ; preds = %7
%33 = load i64, i64* %16
%34 = call i64 @malloc(i64 %33)
%35 = inttoptr i64 %34 to %struct.group_filter*
store %struct.group_filter* %35, %struct.group_filter** %18
%36 = load %struct.group_filter*, %struct.group_filter** %18
%37 = icmp eq %struct.group_filter* %36, null
br i1 %37, label %38, label %39
38: ; preds = %32
store i32 -1, i32* %8
br label %93
39: ; preds = %32
br label %40
40: ; preds = %39, %28
%41 = load i32, i32* %10
%42 = load %struct.group_filter*, %struct.group_filter** %18
%43 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %42, i32 0, i32 0
store i32 %41, i32* %43
%44 = load %struct.group_filter*, %struct.group_filter** %18
%45 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %44, i32 0, i32 4
%46 = load %struct.sockaddr*, %struct.sockaddr** %11
%47 = bitcast %struct.sockaddr* %46 to %struct.sockaddr_storage*
%48 = load i32, i32* %12
%49 = call i32 @memcpy(i32* %45, %struct.sockaddr_storage* %47, i32 %48)
%50 = load i32, i32* %13
%51 = load %struct.group_filter*, %struct.group_filter** %18
%52 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %51, i32 0, i32 1
store i32 %50, i32* %52
%53 = load i32, i32* %14
%54 = load %struct.group_filter*, %struct.group_filter** %18
%55 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %54, i32 0, i32 2
store i32 %53, i32* %55
%56 = load %struct.group_filter*, %struct.group_filter** %18
%57 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %56, i32 0, i32 3
%58 = load i32*, i32** %57
%59 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %15
%60 = load i32, i32* %14
%61 = sext i32 %60 to i64
%62 = mul i64 %61, 4
%63 = trunc i64 %62 to i32
%64 = call i32 @memcpy(i32* %58, %struct.sockaddr_storage* %59, i32 %63)
%65 = load %struct.sockaddr*, %struct.sockaddr** %11
%66 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = load i32, i32* %12
%69 = call i32 @__get_sol(i32 %67, i32 %68)
store i32 %69, i32* %20
%70 = load i32, i32* %20
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %75
72: ; preds = %40
%73 = load i32, i32* @EINVAL
%74 = call i32 @__set_errno(i32 %73)
store i32 -1, i32* %19
br label %82
75: ; preds = %40
%76 = load i32, i32* %9
%77 = load i32, i32* %20
%78 = load i32, i32* @MCAST_MSFILTER
%79 = load %struct.group_filter*, %struct.group_filter** %18
%80 = load i64, i64* %16
%81 = call i32 @__setsockopt(i32 %76, i32 %77, i32 %78, %struct.group_filter* %79, i64 %80)
store i32 %81, i32* %19
br label %82
82: ; preds = %75, %72
%83 = load i32, i32* %17
%84 = icmp ne i32 %83, 0
br i1 %84, label %91, label %85
85: ; preds = %82
%86 = load i32, i32* @errno
store i32 %86, i32* %21
%87 = load %struct.group_filter*, %struct.group_filter** %18
%88 = call i32 @free(%struct.group_filter* %87)
%89 = load i32, i32* %21
%90 = call i32 @__set_errno(i32 %89)
br label %91
91: ; preds = %85, %82
%92 = load i32, i32* %19
store i32 %92, i32* %8
br label %93
93: ; preds = %91, %38
%94 = load i32, i32* %8
ret i32 %94
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.group_filter = type { i32, i32, i32, i32*, i32 }
@EINVAL = global i32 0
@MCAST_MSFILTER = global i32 0
@errno = global i32 0
define i32 @setsourcefilter(i32 %0, i32 %1, %struct.sockaddr* %2, i32 %3, i32 %4, i32 %5, %struct.sockaddr_storage* %6) {
%8 = tail call i64 @GROUP_FILTER_SIZE(i32 %5)
%9 = tail call i32 @__libc_use_alloca(i64 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i64 @alloca(i64 %8)
br label %16
13: ; preds = %7
%14 = tail call i64 @malloc(i64 %8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %45, label %16
16: ; preds = %13, %11
%17 = phi i64 [ %12, %11 ], [ %14, %13 ]
%18 = inttoptr i64 %17 to %struct.group_filter*
%19 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %18, i64 0, i32 0
store i32 %1, i32* %19
%20 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %18, i64 0, i32 4
%21 = bitcast %struct.sockaddr* %2 to %struct.sockaddr_storage*
%22 = tail call i32 @memcpy(i32* %20, %struct.sockaddr_storage* %21, i32 %3)
%23 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %18, i64 0, i32 1
store i32 %4, i32* %23
%24 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %18, i64 0, i32 2
store i32 %5, i32* %24
%25 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %18, i64 0, i32 3
%26 = load i32*, i32** %25
%27 = shl i32 %5, 2
%28 = tail call i32 @memcpy(i32* %26, %struct.sockaddr_storage* %6, i32 %27)
%29 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%30 = load i32, i32* %29
%31 = tail call i32 @__get_sol(i32 %30, i32 %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %16
%34 = load i32, i32* @EINVAL
%35 = tail call i32 @__set_errno(i32 %34)
br label %39
36: ; preds = %16
%37 = load i32, i32* @MCAST_MSFILTER
%38 = tail call i32 @__setsockopt(i32 %0, i32 %31, i32 %37, %struct.group_filter* %18, i64 %8)
br label %39
39: ; preds = %36, %33
%40 = phi i32 [ -1, %33 ], [ %38, %36 ]
br i1 %10, label %41, label %45
41: ; preds = %39
%42 = load i32, i32* @errno
%43 = tail call i32 @free(%struct.group_filter* %18)
%44 = tail call i32 @__set_errno(i32 %42)
br label %45
45: ; preds = %39, %41, %13
%46 = phi i32 [ -1, %13 ], [ %40, %41 ], [ %40, %39 ]
ret i32 %46
}