#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_2__ TYPE_1__;

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

/* Variables and functions */
scalar_t__ IN_MULTICAST(int /*<<< orphan*/);
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_ADD_MEMBERSHIP;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ inet_ntoa(TYPE_1__);
int /*<<< orphan*/ log_error(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ log_info(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ntohl(void *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct ip_mreq *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int udpsocket_addmc(int sock, in_addr_t laddr, in_addr_t raddr) {
  struct ip_mreq mreq;
  if (IN_MULTICAST(ntohl(raddr))) {
    mreq.imr_multiaddr.s_addr = raddr;
    mreq.imr_interface.s_addr = laddr;
    log_info("IP_ADD_MEMBERSHIP on %s (local %s)",
             inet_ntoa(mreq.imr_multiaddr), inet_ntoa(mreq.imr_interface));
    if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) <
        0) {
      log_error("failed to add membership to %s (%s)",
                inet_ntoa(mreq.imr_multiaddr), strerror(errno));
      return -1;
    }
    return 0;
  }
  return -2;
}


x86 assembly (clang, -O0):
<udpsocket_addmc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <ntohl@plt>
mov    %eax,%edi
call   1070 <IN_MULTICAST@plt>
cmp    $0x0,%rax
je     1244 <udpsocket_addmc+0xd4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <inet_ntoa@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <inet_ntoa@plt>
mov    -0x2c(%rbp),%esi
mov    %eax,%edx
lea    0xe32(%rip),%rdi
call   1030 <log_info@plt>
mov    -0x8(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    1238 <udpsocket_addmc+0xc8>
mov    -0x28(%rbp),%rdi
call   1050 <inet_ntoa@plt>
mov    %eax,-0x30(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xdfc(%rip),%rdi
call   1060 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <udpsocket_addmc+0xdb>
movl   $0x0,-0x4(%rbp)
jmp    124b <udpsocket_addmc+0xdb>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udpsocket_addmc>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rdx,%rdi
call   1080 <ntohl@plt>
mov    %eax,%edi
call   1070 <IN_MULTICAST@plt>
test   %rax,%rax
je     1221 <udpsocket_addmc+0xb1>
mov    %rbp,(%rsp)
mov    %rbx,0x8(%rsp)
mov    %rbp,%rdi
call   1050 <inet_ntoa@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <inet_ntoa@plt>
lea    0xe45(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <log_info@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %r14d,%edi
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1226 <udpsocket_addmc+0xb6>
mov    (%rsp),%rdi
call   1050 <inet_ntoa@plt>
mov    %eax,%ebx
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe12(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1060 <log_error@plt>
mov    $0xffffffff,%eax
jmp    1226 <udpsocket_addmc+0xb6>
mov    $0xfffffffe,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ip_mreq = type { %struct.TYPE_2__, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i8* }
@.str = [35 x i8] c"IP_ADD_MEMBERSHIP on %s (local %s)\00"
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
@.str.1 = [36 x i8] c"failed to add membership to %s (%s)\00"
@errno = global i32 0
define i32 @udpsocket_addmc(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.ip_mreq
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %7
%10 = call i32 @ntohl(i8* %9)
%11 = call i64 @IN_MULTICAST(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %43
13: ; preds = %3
%14 = load i8*, i8** %7
%15 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %8, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i8* %14, i8** %16
%17 = load i8*, i8** %6
%18 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %8, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i8* %17, i8** %19
%20 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %8, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @inet_ntoa(i8* %22)
%24 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %8, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @inet_ntoa(i8* %26)
%28 = call i32 @log_info(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %27)
%29 = load i32, i32* %5
%30 = load i32, i32* @IPPROTO_IP
%31 = load i32, i32* @IP_ADD_MEMBERSHIP
%32 = call i64 @setsockopt(i32 %29, i32 %30, i32 %31, %struct.ip_mreq* %8, i32 16)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %13
%35 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %8, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i32 @inet_ntoa(i8* %37)
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 @log_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %40)
store i32 -1, i32* %4
br label %44
42: ; preds = %13
store i32 0, i32* %4
br label %44
43: ; preds = %3
store i32 -2, i32* %4
br label %44
44: ; preds = %43, %42, %34
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
%struct.ip_mreq = type { %struct.TYPE_2__, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i8* }
@.str = [35 x i8] c"IP_ADD_MEMBERSHIP on %s (local %s)\00"
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
@.str.1 = [36 x i8] c"failed to add membership to %s (%s)\00"
@errno = global i32 0
define i32 @udpsocket_addmc(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.ip_mreq
%5 = bitcast %struct.ip_mreq* %4 to i8*
%6 = tail call i32 @ntohl(i8* %2)
%7 = tail call i64 @IN_MULTICAST(i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %3
%10 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %4, i64 0, i32 0, i32 0
store i8* %2, i8** %10
%11 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %4, i64 0, i32 1, i32 0
store i8* %1, i8** %11
%12 = tail call i32 @inet_ntoa(i8* %2)
%13 = tail call i32 @inet_ntoa(i8* %1)
%14 = tail call i32 @log_info(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = load i32, i32* @IPPROTO_IP
%16 = load i32, i32* @IP_ADD_MEMBERSHIP
%17 = call i64 @setsockopt(i32 %0, i32 %15, i32 %16, %struct.ip_mreq* %4, i32 16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %9
%20 = load i8*, i8** %10
%21 = call i32 @inet_ntoa(i8* %20)
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 @log_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %23)
br label %25
25: ; preds = %3, %9, %19
%26 = phi i32 [ -1, %19 ], [ 0, %9 ], [ -2, %3 ]
ret i32 %26
}