#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 sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;
typedef void *in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ IN_MULTICAST(int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_ntoa(TYPE_1__);
int /*<<< orphan*/ log_error(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ntohl(void *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ udpsocket_addmc(int, void *, void *);
int udpsocket_open(void *, void *, unsigned short);
int udpsocket_ropen(in_addr_t laddr, in_addr_t raddr, unsigned short port) {
  int sock = udpsocket_open(laddr, raddr, port);
  struct sockaddr_in sin;
  sin.sin_family = AF_INET;
  if (IN_MULTICAST(ntohl(raddr))) {
    sin.sin_addr.s_addr = raddr;
  } else {
    sin.sin_addr.s_addr = laddr;
  }
  sin.sin_port = htons(port);
  if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    log_error("could not bind socket to ip `%s' (%s)", inet_ntoa(sin.sin_addr),
              strerror(errno));
    return -1;
  }
  udpsocket_addmc(sock, laddr, raddr);
  return sock;
}


x86 assembly (clang, -O0):
<udpsocket_ropen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dx,%ax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %ax,-0x1a(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movzwl -0x1a(%rbp),%edx
call   1070 <udpsocket_open@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <ntohl@plt>
mov    %eax,%edi
call   1080 <IN_MULTICAST@plt>
cmp    $0x0,%rax
je     11ee <udpsocket_ropen+0x5e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11f6 <udpsocket_ropen+0x66>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movzwl -0x1a(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1254 <udpsocket_ropen+0xc4>
mov    -0x30(%rbp),%rdi
call   1040 <inet_ntoa@plt>
mov    %eax,-0x34(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xdbd(%rip),%rdi
call   1050 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <udpsocket_ropen+0xda>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <udpsocket_addmc@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udpsocket_ropen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
call   1070 <udpsocket_open@plt>
mov    %eax,%r15d
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rbx,%rdi
call   10a0 <ntohl@plt>
mov    %eax,%edi
call   1080 <IN_MULTICAST@plt>
test   %rax,%rax
mov    %rbx,%rax
cmove  %r14,%rax
mov    %rax,0x8(%rsp)
mov    %ebp,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1207 <udpsocket_ropen+0x77>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <udpsocket_addmc@plt>
jmp    1237 <udpsocket_ropen+0xa7>
mov    0x8(%rsp),%rdi
call   1040 <inet_ntoa@plt>
mov    %eax,%ebx
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdd8(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <log_error@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [38 x i8] c"could not bind socket to ip `%s' (%s)\00"
@errno = global i32 0
define i32 @udpsocket_ropen(i8* %0, i8* %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i16
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i8* %1, i8** %6
store i16 %2, i16* %7
%10 = load i8*, i8** %5
%11 = load i8*, i8** %6
%12 = load i16, i16* %7
%13 = call i32 @udpsocket_open(i8* %10, i8* %11, i16 zeroext %12)
store i32 %13, i32* %8
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i8*, i8** %6
%17 = call i32 @ntohl(i8* %16)
%18 = call i64 @IN_MULTICAST(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i8* %21, i8** %23
br label %28
24: ; preds = %3
%25 = load i8*, i8** %5
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i8* %25, i8** %27
br label %28
28: ; preds = %24, %20
%29 = load i16, i16* %7
%30 = call i32 @htons(i16 zeroext %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* %8
%33 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 16)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %28
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @inet_ntoa(i8* %39)
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 @log_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %40, i32 %42)
store i32 -1, i32* %4
br label %50
44: ; preds = %28
%45 = load i32, i32* %8
%46 = load i8*, i8** %5
%47 = load i8*, i8** %6
%48 = call i32 @udpsocket_addmc(i32 %45, i8* %46, i8* %47)
%49 = load i32, i32* %8
store i32 %49, i32* %4
br label %50
50: ; preds = %44, %36
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [38 x i8] c"could not bind socket to ip `%s' (%s)\00"
@errno = global i32 0
define i32 @udpsocket_ropen(i8* %0, i8* %1, i16 zeroext %2) {
%4 = alloca %struct.sockaddr_in
%5 = tail call i32 @udpsocket_open(i8* %0, i8* %1, i16 zeroext %2)
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @ntohl(i8* %1)
%10 = tail call i64 @IN_MULTICAST(i32 %9)
%11 = icmp eq i64 %10, 0
%12 = select i1 %11, i8* %0, i8* %1
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i8* %12, i8** %13
%14 = tail call i32 @htons(i16 zeroext %2)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%17 = call i64 @bind(i32 %5, %struct.sockaddr* %16, i32 16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %3
%20 = load i8*, i8** %13
%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 ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %23)
br label %27
25: ; preds = %3
%26 = call i32 @udpsocket_addmc(i32 %5, i8* %0, i8* %1)
br label %27
27: ; preds = %25, %19
%28 = phi i32 [ -1, %19 ], [ %5, %25 ]
ret i32 %28
}