#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 in6_addr {
  int dummy;
};
struct sockaddr_in6 {
  struct in6_addr sin6_addr;
  int /*<<< orphan*/ sin6_port;
  scalar_t__ sin6_scope_id;
  scalar_t__ sin6_flowinfo;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
struct in6_addr IN6ADDR_LOOPBACK_INIT;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error_exit(char *);
int /*<<< orphan*/ htons(int);
void sock_6(int *sock, int port) {
  struct sockaddr_in6 sa;
  struct in6_addr any6 = IN6ADDR_LOOPBACK_INIT;
  sa.sin6_family = AF_INET6;
  sa.sin6_flowinfo = 0;
  sa.sin6_scope_id = 0;
  sa.sin6_port = htons(port);
  sa.sin6_addr = any6;
  int succ = bind(*sock, (struct sockaddr *)&sa, sizeof(sa));
  if (succ < 0) {
    close(*sock);
    error_exit("bind of server socket for version 6");
  }
}


x86 assembly (clang, -O0):
<sock_6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1060 <bind@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11c0 <sock_6+0x80>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
lea    0xe45(%rip),%rdi
call   1050 <error_exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_6>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    0x2e98(%rip),%rax
mov    (%rax),%ebp
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %esi,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
mov    $0x20,%edx
call   1060 <bind@plt>
test   %eax,%eax
jns    119e <sock_6+0x5e>
mov    (%rbx),%edi
call   1040 <close@plt>
lea    0xe67(%rip),%rdi
call   1050 <error_exit@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
%struct.sockaddr_in6 = type { %struct.in6_addr, i32, i64, i64, i32 }
%struct.sockaddr = type { i32 }
@IN6ADDR_LOOPBACK_INIT = global %struct.in6_addr zeroinitializer
@AF_INET6 = global i32 0
@.str = [36 x i8] c"bind of server socket for version 6\00"
define void @sock_6(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in6
%6 = alloca %struct.in6_addr
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = bitcast %struct.in6_addr* %6 to i8*
%9 = load i32, i32* @AF_INET6
%10 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 4
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 3
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 2
store i64 0, i64* %12
%13 = load i32, i32* %4
%14 = call i32 @htons(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
%17 = bitcast %struct.in6_addr* %16 to i8*
%18 = bitcast %struct.in6_addr* %6 to i8*
%19 = load i32*, i32** %3
%20 = load i32, i32* %19
%21 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 32)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %2
%26 = load i32*, i32** %3
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = call i32 @error_exit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %25, %2
ret void
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
%struct.sockaddr_in6 = type { %struct.in6_addr, i32, i64, i64, i32 }
%struct.sockaddr = type { i32 }
@IN6ADDR_LOOPBACK_INIT = global %struct.in6_addr zeroinitializer
@AF_INET6 = global i32 0
@.str = [36 x i8] c"bind of server socket for version 6\00"
define void @sock_6(i32* nocapture %0, i32 %1) {
%3 = alloca %struct.sockaddr_in6
%4 = bitcast %struct.sockaddr_in6* %3 to i8*
%5 = load i32, i32* getelementptr inbounds (%struct.in6_addr, %struct.in6_addr* @IN6ADDR_LOOPBACK_INIT, i64 0, i32 0)
%6 = load i32, i32* @AF_INET6
%7 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 4
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
%9 = bitcast i64* %8 to i8*
%10 = tail call i32 @htons(i32 %1)
%11 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0, i32 0
store i32 %5, i32* %12
%13 = load i32, i32* %0
%14 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%15 = call i32 @bind(i32 %13, %struct.sockaddr* %14, i32 32)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* %0
%19 = call i32 @close(i32 %18)
%20 = call i32 @error_exit(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %17, %2
ret void
}