#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__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct TYPE_4__ {
  void *s_addr;
};
struct ip_mreq {
  TYPE_2__ imr_interface;
  int /*<<< orphan*/ imr_multiaddr;
};
typedef int /*<<< orphan*/ mreq;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_ADD_MEMBERSHIP;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct ip_mreq *, int);
int /*<<< orphan*/ close(int);
void *htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned int);
scalar_t__ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct ip_mreq *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int BindMulticastSocket(const char *ip, unsigned port,
                        struct sockaddr_in *claddr) {
  struct ip_mreq mreq;
  int fd;
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    puts("Socket error");
    return -1;
  }
  claddr->sin_family = AF_INET;
  claddr->sin_port = htons(port);
  claddr->sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(fd, (struct sockaddr *)claddr, sizeof(struct sockaddr_in)) < 0) {
    puts("Fail binding server.");
    close(fd);
    return -1;
  }
  bzero(&mreq, sizeof(mreq));
  if (inet_aton(ip, &mreq.imr_multiaddr) < 0) {
    puts("Fail ip aton");
    return -1;
  }
  mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
    puts("Set sockopt error.");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<BindMulticastSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11e0 <BindMulticastSocket+0x50>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <BindMulticastSocket+0x163>
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1254 <BindMulticastSocket+0xc4>
lea    0xdd2(%rip),%rdi
call   1030 <puts@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <BindMulticastSocket+0x163>
lea    -0x30(%rbp),%rdi
mov    $0x10,%esi
call   10a0 <bzero@plt>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
add    $0x8,%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%rax
jge    1295 <BindMulticastSocket+0x105>
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <BindMulticastSocket+0x163>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    0x2d2e(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    12ed <BindMulticastSocket+0x15d>
lea    0xd53(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <BindMulticastSocket+0x163>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BindMulticastSocket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e34(%rip),%r12
mov    (%r12),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     125d <BindMulticastSocket+0xcd>
mov    %eax,%ebp
mov    (%r12),%eax
mov    %eax,0xc(%rbx)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rbx)
mov    0x2ded(%rip),%r15
mov    (%r15),%edi
call   1060 <htonl@plt>
mov    %rax,(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1266 <BindMulticastSocket+0xd6>
mov    %rsp,%rdi
mov    $0x10,%esi
call   10a0 <bzero@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1080 <inet_aton@plt>
test   %rax,%rax
js     127b <BindMulticastSocket+0xeb>
mov    (%r15),%edi
call   1060 <htonl@plt>
mov    %rax,(%rsp)
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jns    128c <BindMulticastSocket+0xfc>
lea    0xdd4(%rip),%rdi
jmp    1282 <BindMulticastSocket+0xf2>
lea    0xd9c(%rip),%rdi
jmp    1282 <BindMulticastSocket+0xf2>
lea    0xda0(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1287 <BindMulticastSocket+0xf7>
lea    0xda0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i8* }
%struct.ip_mreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"Socket error\00"
@INADDR_ANY = global i32 0
@.str.1 = [21 x i8] c"Fail binding server.\00"
@.str.2 = [13 x i8] c"Fail ip aton\00"
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
@.str.3 = [19 x i8] c"Set sockopt error.\00"
define i32 @BindMulticastSocket(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.ip_mreq
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %9
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %60
16: ; preds = %3
%17 = load i32, i32* @AF_INET
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 2
store i32 %17, i32* %19
%20 = load i32, i32* %6
%21 = call i32 @htons(i32 %20)
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 1
store i32 %21, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = call i8* @htonl(i32 %24)
%26 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %26, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %27, i32 0, i32 0
store i8* %25, i8** %28
%29 = load i32, i32* %9
%30 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%31 = bitcast %struct.sockaddr_in* %30 to %struct.sockaddr*
%32 = call i64 @bind(i32 %29, %struct.sockaddr* %31, i32 16)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %16
%35 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* %9
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %4
br label %60
38: ; preds = %16
%39 = call i32 @bzero(%struct.ip_mreq* %8, i32 16)
%40 = load i8*, i8** %5
%41 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %8, i32 0, i32 1
%42 = call i64 @inet_aton(i8* %40, i32* %41)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %60
46: ; preds = %38
%47 = load i32, i32* @INADDR_ANY
%48 = call i8* @htonl(i32 %47)
%49 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %8, i32 0, i32 0
%50 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %49, i32 0, i32 0
store i8* %48, i8** %50
%51 = load i32, i32* %9
%52 = load i32, i32* @IPPROTO_IP
%53 = load i32, i32* @IP_ADD_MEMBERSHIP
%54 = call i64 @setsockopt(i32 %51, i32 %52, i32 %53, %struct.ip_mreq* %8, i32 16)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %46
%57 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %60
58: ; preds = %46
%59 = load i32, i32* %9
store i32 %59, i32* %4
br label %60
60: ; preds = %58, %56, %44, %34, %14
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_3__, i32, i32 }
%struct.TYPE_3__ = type { i8* }
%struct.ip_mreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"Socket error\00"
@INADDR_ANY = global i32 0
@.str.1 = [21 x i8] c"Fail binding server.\00"
@.str.2 = [13 x i8] c"Fail ip aton\00"
@IPPROTO_IP = global i32 0
@IP_ADD_MEMBERSHIP = global i32 0
@.str.3 = [19 x i8] c"Set sockopt error.\00"
define i32 @BindMulticastSocket(i8* %0, i32 %1, %struct.sockaddr_in* %2) {
%4 = alloca %struct.ip_mreq
%5 = bitcast %struct.ip_mreq* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %43
12: ; preds = %3
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = tail call i8* @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i8* %18, i8** %19
%20 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%21 = tail call i64 @bind(i32 %8, %struct.sockaddr* %20, i32 16)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @close(i32 %8)
br label %43
26: ; preds = %12
%27 = call i32 @bzero(%struct.ip_mreq* %4, i32 16)
%28 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %4, i64 0, i32 1
%29 = call i64 @inet_aton(i8* %0, i32* %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %43
33: ; preds = %26
%34 = load i32, i32* @INADDR_ANY
%35 = call i8* @htonl(i32 %34)
%36 = getelementptr inbounds %struct.ip_mreq, %struct.ip_mreq* %4, i64 0, i32 0, i32 0
store i8* %35, i8** %36
%37 = load i32, i32* @IPPROTO_IP
%38 = load i32, i32* @IP_ADD_MEMBERSHIP
%39 = call i64 @setsockopt(i32 %8, i32 %37, i32 %38, %struct.ip_mreq* %4, i32 16)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %33, %41, %31, %23, %10
%44 = phi i32 [ -1, %10 ], [ -1, %23 ], [ -1, %31 ], [ -1, %41 ], [ %8, %33 ]
ret i32 %44
}