#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 */

/* Type definitions */
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ in6addr_any;
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int coap_net_udp_bind(int port) {
  int netd;
  struct sockaddr_in6 addr;
  if ((netd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin6_family = AF_INET6;
  addr.sin6_port = htons(port);
  addr.sin6_addr = in6addr_any;
  if (bind(netd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    return -1;
  }
  return netd;
}


x86 assembly (clang, -O0):
<coap_net_udp_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1183 <coap_net_udp_bind+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <coap_net_udp_bind+0xa3>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11dd <coap_net_udp_bind+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <coap_net_udp_bind+0xa3>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coap_net_udp_bind>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e85(%rip),%r15
mov    (%r15),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11c2 <coap_net_udp_bind+0x82>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    11c7 <coap_net_udp_bind+0x87>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@in6addr_any = global i32 0
define i32 @coap_net_udp_bind(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in6
store i32 %0, i32* %3
%6 = load i32, i32* @AF_INET6
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @IPPROTO_UDP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %28
12: ; preds = %1
%13 = call i32 @memset(%struct.sockaddr_in6* %5, i32 0, i32 12)
%14 = load i32, i32* @AF_INET6
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* %3
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @in6addr_any
%20 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %4
%22 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%23 = call i64 @bind(i32 %21, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %12
store i32 -1, i32* %2
br label %28
26: ; preds = %12
%27 = load i32, i32* %4
store i32 %27, i32* %2
br label %28
28: ; preds = %26, %25, %11
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@in6addr_any = global i32 0
define i32 @coap_net_udp_bind(i32 %0) {
%2 = alloca %struct.sockaddr_in6
%3 = bitcast %struct.sockaddr_in6* %2 to i8*
%4 = load i32, i32* @AF_INET6
%5 = load i32, i32* @SOCK_DGRAM
%6 = load i32, i32* @IPPROTO_UDP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %1
%10 = call i32 @memset(%struct.sockaddr_in6* %2, i32 0, i32 12)
%11 = load i32, i32* @AF_INET6
%12 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 %0)
%14 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @in6addr_any
%16 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%18 = call i64 @bind(i32 %7, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
%20 = select i1 %19, i32 -1, i32 %7
br label %21
21: ; preds = %9, %1
%22 = phi i32 [ -1, %1 ], [ %20, %9 ]
ret i32 %22
}