#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__ {
  int /*<<< orphan*/ 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 int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
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*/ strerror(int /*<<< orphan*/);
int udpsocket_open(int /*<<< orphan*/, int /*<<< orphan*/, unsigned short);
int udpsocket_wopen(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;
  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;
  }
  return sock;
}


x86 assembly (clang, -O0):
<udpsocket_wopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dx,%ax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %ax,-0xe(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
movzwl -0xe(%rbp),%edx
call   1060 <udpsocket_open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
movzwl -0xe(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    11f6 <udpsocket_wopen+0x96>
mov    -0x20(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe1b(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <udpsocket_wopen+0x9c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udpsocket_wopen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %edi,%ebx
call   1060 <udpsocket_open@plt>
mov    %eax,%r14d
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    11d2 <udpsocket_wopen+0x72>
mov    (%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%ebx
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3d(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <log_error@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%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_wopen(i32 %0, i32 %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i16
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i16 %2, i16* %7
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = load i16, i16* %7
%13 = call i32 @udpsocket_open(i32 %10, i32 %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 i32, i32* %5
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i16, i16* %7
%20 = call i32 @htons(i16 zeroext %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* %8
%23 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%24 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %3
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @inet_ntoa(i32 %29)
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @log_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %32)
store i32 -1, i32* %4
br label %36
34: ; preds = %3
%35 = load i32, i32* %8
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %26
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%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_wopen(i32 %0, i32 %1, i16 zeroext %2) {
%4 = alloca %struct.sockaddr_in
%5 = tail call i32 @udpsocket_open(i32 %0, i32 %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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %0, i32* %9
%10 = tail call i32 @htons(i16 zeroext %2)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %10, i32* %11
%12 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%13 = call i64 @bind(i32 %5, %struct.sockaddr* %12, i32 12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = load i32, i32* %9
%17 = call i32 @inet_ntoa(i32 %16)
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @log_error(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %19)
br label %21
21: ; preds = %3, %15
%22 = phi i32 [ -1, %15 ], [ %5, %3 ]
ret i32 %22
}