#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_port;
  int /*<<< orphan*/ sin6_family;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ in6addr_any;
int /*<<< orphan*/ set_dualstack_mode(int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int get_fd(int type, int port) {
  int fd;
  struct sockaddr_in6 addr;
  if ((fd = socket(AF_INET6, type, 0)) == -1) {
    return -1;
  }
  set_dualstack_mode(fd);
  addr.sin6_addr = in6addr_any;
  addr.sin6_family = AF_INET6;
  addr.sin6_port = htons(port);
  if ((bind(fd, (struct sockaddr *)&addr, sizeof(addr))) == -1) {
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<get_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1179 <get_fd+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <get_fd+0x90>
mov    -0x10(%rbp),%edi
call   1030 <set_dualstack_mode@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    11ca <get_fd+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <get_fd+0x90>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%esi
mov    0x2e8d(%rip),%r14
mov    (%r14),%edi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11a0 <get_fd+0x60>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <set_dualstack_mode@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%r14),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    11a5 <get_fd+0x65>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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