#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int /*<<< orphan*/ s_addr;
};
struct TYPE_4__ {
  int /*<<< orphan*/ s_addr;
};
struct ip_mreq {
  TYPE_1__ imr_interface;
  TYPE_2__ imr_multiaddr;
};
typedef int /*<<< orphan*/ mreq;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IN_MULTICAST(int /*<<< orphan*/);
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_ADD_MEMBERSHIP;
int /*<<< orphan*/ inet_aton(char *, TYPE_2__ *);
int /*<<< orphan*/ memset(struct ip_mreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct ip_mreq *,
               int);
int socket_join_multicast(int sock, char *addr) {
  struct ip_mreq mreq;
  memset(&mreq, 0, sizeof(struct ip_mreq));
  if (!inet_aton(addr, &mreq.imr_multiaddr))
    return -1;
  if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr)))
    return 0;
  mreq.imr_interface.s_addr = INADDR_ANY;
  return setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
}


x86 assembly (clang, -O0):
<socket_join_multicast>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
add    $0x4,%rsi
call   1060 <inet_aton@plt>
cmp    $0x0,%eax
jne    1195 <socket_join_multicast+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <socket_join_multicast+0x9c>
mov    -0x14(%rbp),%edi
call   1070 <ntohl@plt>
mov    %eax,%edi
call   1050 <IN_MULTICAST@plt>
cmp    $0x0,%eax
jne    11b9 <socket_join_multicast+0x69>
movl   $0x0,-0x4(%rbp)
jmp    11ec <socket_join_multicast+0x9c>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_join_multicast>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0x8(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <inet_aton@plt>
test   %eax,%eax
je     11cc <socket_join_multicast+0x7c>
mov    0xc(%rsp),%edi
call   1070 <ntohl@plt>
mov    %eax,%edi
call   1050 <IN_MULTICAST@plt>
test   %eax,%eax
je     11d1 <socket_join_multicast+0x81>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r14d,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ebp
jmp    11d1 <socket_join_multicast+0x81>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ip_mreq = type { %struct.TYPE_3__, %struct.TYPE_4__ }
%struct.TYPE_3__ = type { i32 }
%struct.TYPE_4__ = type { i32 }
@INADDR_ANY = global i32 0
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
define i32 @socket_join_multicast(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ip_mreq
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = call i32 @memset(%struct.ip_mreq* %6, i32 0, i32 8)
%8 = load i8*, i8** %5
%9 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %6, i32 0, i32 1
%10 = call i32 @inet_aton(i8* %8, %struct.TYPE_4__* %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 -1, i32* %3
br label %29
13: ; preds = %2
%14 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %6, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @ntohl(i32 %16)
%18 = call i32 @IN_MULTICAST(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %13
store i32 0, i32* %3
br label %29
21: ; preds = %13
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %6, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %4
%26 = load i32, i32* @IPPROTO_IP
%27 = load i32, i32* @IP_ADD_MEMBERSHIP
%28 = call i32 @setsockopt(i32 %25, i32 %26, i32 %27, %struct.ip_mreq* %6, i32 8)
store i32 %28, i32* %3
br label %29
29: ; preds = %21, %20, %12
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.ip_mreq = type { %struct.TYPE_3__, %struct.TYPE_4__ }
%struct.TYPE_3__ = type { i32 }
%struct.TYPE_4__ = type { i32 }
@INADDR_ANY = global i32 0
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
define i32 @socket_join_multicast(i32 %0, i8* %1) {
%3 = alloca %struct.ip_mreq
%4 = bitcast %struct.ip_mreq* %3 to i8*
%5 = call i32 @memset(%struct.ip_mreq* %3, i32 0, i32 8)
%6 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %3, i64 0, i32 1
%7 = call i32 @inet_aton(i8* %1, %struct.TYPE_4__* %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %2
%10 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @ntohl(i32 %11)
%13 = call i32 @IN_MULTICAST(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %9
%16 = load i32, i32* @INADDR_ANY
%17 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %3, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @IPPROTO_IP
%19 = load i32, i32* @IP_ADD_MEMBERSHIP
%20 = call i32 @setsockopt(i32 %0, i32 %18, i32 %19, %struct.ip_mreq* %3, i32 8)
br label %21
21: ; preds = %9, %2, %15
%22 = phi i32 [ %20, %15 ], [ -1, %2 ], [ 0, %9 ]
ret i32 %22
}