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

/* Variables and functions */
int EINVAL;
int GROUP_FILTER_SIZE(void *);
int /*<<< orphan*/ MCAST_MSFILTER;
int MIN(void *, void *);
int __get_sol(int /*<<< orphan*/, int);
int __getsockopt(int, int, int /*<<< orphan*/, struct group_filter *, int *);
int __libc_use_alloca(int);
int /*<<< orphan*/ __set_errno(int);
scalar_t__ alloca(int);
int errno;
int /*<<< orphan*/ free(struct group_filter *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(struct sockaddr_storage *, struct sockaddr const *,
                          int);
int getsourcefilter(int s, uint32_t interface, const struct sockaddr *group,
                    socklen_t grouplen, uint32_t *fmode, uint32_t *numsrc,
                    struct sockaddr_storage *slist) {
  socklen_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_numsrc = *numsrc;
  int result;
  int sol = __get_sol(group->sa_family, grouplen);
  if (sol == -1) {
    __set_errno(EINVAL);
    result = -1;
  } else {
    result = __getsockopt(s, sol, MCAST_MSFILTER, gf, &needed);
    if (result == 0) {
      *fmode = gf->gf_fmode;
      memcpy(slist, gf->gf_slist,
             MIN(*numsrc, gf->gf_numsrc) * sizeof(struct sockaddr_storage));
      *numsrc = gf->gf_numsrc;
    }
  }
  if (!use_alloca) {
    int save_errno = errno;
    free(gf);
    __set_errno(save_errno);
  }
  return result;
}


x86 assembly (clang, -O0):
<getsourcefilter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1040 <GROUP_FILTER_SIZE@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   10b0 <__libc_use_alloca@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     11f7 <getsourcefilter+0x57>
mov    -0x34(%rbp),%edi
call   1080 <alloca@plt>
mov    %rax,-0x40(%rbp)
jmp    121f <getsourcefilter+0x7f>
mov    -0x34(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    121a <getsourcefilter+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <getsourcefilter+0x196>
jmp    121f <getsourcefilter+0x7f>
mov    -0x10(%rbp),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x40(%rbp),%rdi
add    $0x18,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
call   1070 <__get_sol@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    1282 <getsourcefilter+0xe2>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    1309 <getsourcefilter+0x169>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
mov    -0x40(%rbp),%rcx
lea    -0x34(%rbp),%r8
call   10c0 <__getsockopt@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1304 <getsourcefilter+0x164>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
call   1060 <MIN@plt>
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rsi
cltq
shl    $0x2,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
jmp    1309 <getsourcefilter+0x169>
cmpl   $0x0,-0x38(%rbp)
jne    1330 <getsourcefilter+0x190>
mov    0x2cbe(%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    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsourcefilter>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r13
mov    %r8,0x10(%rsp)
mov    %ecx,%ebx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,0x8(%rsp)
mov    (%r9),%rdi
call   1040 <GROUP_FILTER_SIZE@plt>
mov    %eax,%ebp
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   10b0 <__libc_use_alloca@plt>
mov    %eax,%r14d
mov    %ebp,%edi
test   %eax,%eax
je     11ea <getsourcefilter+0x4a>
call   1080 <alloca@plt>
mov    %rax,%rbp
jmp    11fb <getsourcefilter+0x5b>
call   10a0 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12bf <getsourcefilter+0x11f>
mov    %r12,0x20(%rbp)
lea    0x18(%rbp),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1090 <memcpy@plt>
mov    0x0(%r13),%rax
mov    %rax,0x0(%rbp)
mov    (%r15),%edi
mov    %ebx,%esi
call   1070 <__get_sol@plt>
cmp    $0xffffffff,%eax
je     128b <getsourcefilter+0xeb>
mov    0x2dbd(%rip),%rcx
mov    (%rcx),%edx
lea    0xc(%rsp),%r8
mov    0x8(%rsp),%edi
mov    %eax,%esi
mov    %rbp,%rcx
call   10c0 <__getsockopt@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1284 <getsourcefilter+0xe4>
mov    0x50(%rsp),%r15
mov    0x10(%rbp),%rax
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
mov    0x0(%rbp),%rsi
mov    0x8(%rbp),%rbx
mov    0x0(%r13),%rdi
call   1060 <MIN@plt>
lea    0x0(,%rax,4),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <memcpy@plt>
mov    0x0(%rbp),%rax
mov    %rax,0x0(%r13)
xor    %ebx,%ebx
test   %r14d,%r14d
jne    12c4 <getsourcefilter+0x124>
jmp    12a3 <getsourcefilter+0x103>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%ebx
test   %r14d,%r14d
jne    12c4 <getsourcefilter+0x124>
mov    0x2d2e(%rip),%rax
mov    (%rax),%r14d
mov    %rbp,%rdi
call   1050 <free@plt>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
jmp    12c4 <getsourcefilter+0x124>
mov    $0xffffffff,%ebx
mov    %ebx,%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 { i8*, %struct.sockaddr*, i8*, %struct.sockaddr_storage, i8* }
@EINVAL = global i32 0
@MCAST_MSFILTER = global i32 0
@errno = global i32 0
define i32 @getsourcefilter(i32 %0, i8* %1, %struct.sockaddr* %2, i32 %3, i8** %4, i8** %5, %struct.sockaddr_storage* %6) {
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca %struct.sockaddr*
%12 = alloca i32
%13 = alloca i8**
%14 = alloca i8**
%15 = alloca %struct.sockaddr_storage*
%16 = alloca i32
%17 = alloca i32
%18 = alloca %struct.group_filter*
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
store i32 %0, i32* %9
store i8* %1, i8** %10
store %struct.sockaddr* %2, %struct.sockaddr** %11
store i32 %3, i32* %12
store i8** %4, i8*** %13
store i8** %5, i8*** %14
store %struct.sockaddr_storage* %6, %struct.sockaddr_storage** %15
%22 = load i8**, i8*** %14
%23 = load i8*, i8** %22
%24 = call i32 @GROUP_FILTER_SIZE(i8* %23)
store i32 %24, i32* %16
%25 = load i32, i32* %16
%26 = call i32 @__libc_use_alloca(i32 %25)
store i32 %26, i32* %17
%27 = load i32, i32* %17
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %7
%30 = load i32, i32* %16
%31 = call i64 @alloca(i32 %30)
%32 = inttoptr i64 %31 to %struct.group_filter*
store %struct.group_filter* %32, %struct.group_filter** %18
br label %41
33: ; preds = %7
%34 = load i32, i32* %16
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to %struct.group_filter*
store %struct.group_filter* %36, %struct.group_filter** %18
%37 = load %struct.group_filter*, %struct.group_filter** %18
%38 = icmp eq %struct.group_filter* %37, null
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 -1, i32* %8
br label %107
40: ; preds = %33
br label %41
41: ; preds = %40, %29
%42 = load i8*, i8** %10
%43 = load %struct.group_filter*, %struct.group_filter** %18
%44 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %43, i32 0, i32 4
store i8* %42, i8** %44
%45 = load %struct.group_filter*, %struct.group_filter** %18
%46 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %45, i32 0, i32 3
%47 = load %struct.sockaddr*, %struct.sockaddr** %11
%48 = load i32, i32* %12
%49 = call i32 @memcpy(%struct.sockaddr_storage* %46, %struct.sockaddr* %47, i32 %48)
%50 = load i8**, i8*** %14
%51 = load i8*, i8** %50
%52 = load %struct.group_filter*, %struct.group_filter** %18
%53 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %52, i32 0, i32 0
store i8* %51, i8** %53
%54 = load %struct.sockaddr*, %struct.sockaddr** %11
%55 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = load i32, i32* %12
%58 = call i32 @__get_sol(i32 %56, i32 %57)
store i32 %58, i32* %20
%59 = load i32, i32* %20
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %41
%62 = load i32, i32* @EINVAL
%63 = call i32 @__set_errno(i32 %62)
store i32 -1, i32* %19
br label %96
64: ; preds = %41
%65 = load i32, i32* %9
%66 = load i32, i32* %20
%67 = load i32, i32* @MCAST_MSFILTER
%68 = load %struct.group_filter*, %struct.group_filter** %18
%69 = call i32 @__getsockopt(i32 %65, i32 %66, i32 %67, %struct.group_filter* %68, i32* %16)
store i32 %69, i32* %19
%70 = load i32, i32* %19
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %95
72: ; preds = %64
%73 = load %struct.group_filter*, %struct.group_filter** %18
%74 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %73, i32 0, i32 2
%75 = load i8*, i8** %74
%76 = load i8**, i8*** %13
store i8* %75, i8** %76
%77 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %15
%78 = load %struct.group_filter*, %struct.group_filter** %18
%79 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %78, i32 0, i32 1
%80 = load %struct.sockaddr*, %struct.sockaddr** %79
%81 = load i8**, i8*** %14
%82 = load i8*, i8** %81
%83 = load %struct.group_filter*, %struct.group_filter** %18
%84 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %83, i32 0, i32 0
%85 = load i8*, i8** %84
%86 = call i32 @MIN(i8* %82, i8* %85)
%87 = sext i32 %86 to i64
%88 = mul i64 %87, 4
%89 = trunc i64 %88 to i32
%90 = call i32 @memcpy(%struct.sockaddr_storage* %77, %struct.sockaddr* %80, i32 %89)
%91 = load %struct.group_filter*, %struct.group_filter** %18
%92 = getelementptr inbounds %struct.group_filter, %struct.group_filter* %91, i32 0, i32 0
%93 = load i8*, i8** %92
%94 = load i8**, i8*** %14
store i8* %93, i8** %94
br label %95
95: ; preds = %72, %64
br label %96
96: ; preds = %95, %61
%97 = load i32, i32* %17
%98 = icmp ne i32 %97, 0
br i1 %98, label %105, label %99
99: ; preds = %96
%100 = load i32, i32* @errno
store i32 %100, i32* %21
%101 = load %struct.group_filter*, %struct.group_filter** %18
%102 = call i32 @free(%struct.group_filter* %101)
%103 = load i32, i32* %21
%104 = call i32 @__set_errno(i32 %103)
br label %105
105: ; preds = %99, %96
%106 = load i32, i32* %19
store i32 %106, i32* %8
br label %107
107: ; preds = %105, %39
%108 = load i32, i32* %8
ret i32 %108
}

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