#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_4__ {
  int /*<<< orphan*/ s_addr;
};
struct TYPE_3__ {
  int /*<<< orphan*/ s_addr;
};
struct ip_mreq {
  TYPE_2__ imr_interface;
  TYPE_1__ imr_multiaddr;
};
typedef int /*<<< orphan*/ mreq;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_ADD_MEMBERSHIP;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct ip_mreq *,
               int);
int sock_add_multicast_membership(int sockfd, const char *multicast_addr) {
  struct ip_mreq mreq;
  mreq.imr_multiaddr.s_addr = inet_addr(multicast_addr);
  mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  int ret;
  ret = setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
  if (ret == -1) {
    perror("setsockopt");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sock_add_multicast_membership>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b5 <sock_add_multicast_membership+0x75>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <sock_add_multicast_membership+0x7c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_add_multicast_membership>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %rsi,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     1198 <sock_add_multicast_membership+0x58>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    1192 <sock_add_multicast_membership+0x52>

LLVM IR (-O0):
%struct.ip_mreq = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
@INADDR_ANY = global i32 0
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
@.str = [11 x i8] c"setsockopt\00"
define i32 @sock_add_multicast_membership(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ip_mreq
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @inet_addr(i8* %8)
%10 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %6, i32 0, i32 1
%11 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %10, i32 0, i32 0
store i32 %9, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %6, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* %4
%17 = load i32, i32* @IPPROTO_IP
%18 = load i32, i32* @IP_ADD_MEMBERSHIP
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, %struct.ip_mreq* %6, i32 8)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %25
24: ; preds = %2
store i32 0, i32* %3
br label %25
25: ; preds = %24, %22
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.ip_mreq = type { %struct.TYPE_4__, %struct.TYPE_3__ }
%struct.TYPE_4__ = type { i32 }
%struct.TYPE_3__ = type { i32 }
@INADDR_ANY = global i32 0
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
@.str = [11 x i8] c"setsockopt\00"
define i32 @sock_add_multicast_membership(i32 %0, i8* %1) {
%3 = alloca %struct.ip_mreq
%4 = bitcast %struct.ip_mreq* %3 to i8*
%5 = tail call i32 @inet_addr(i8* %1)
%6 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %3, i64 0, i32 1, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* @INADDR_ANY
%8 = tail call i32 @htonl(i32 %7)
%9 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %3, i64 0, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @IPPROTO_IP
%11 = load i32, i32* @IP_ADD_MEMBERSHIP
%12 = call i32 @setsockopt(i32 %0, i32 %10, i32 %11, %struct.ip_mreq* %3, i32 8)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %2, %14
%17 = phi i32 [ -1, %14 ], [ 0, %2 ]
ret i32 %17
}