#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*/ member_1;
  int /*<<< orphan*/ member_0;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close_with_errno(int);
scalar_t__ getsockname(int, struct sockaddr *, int *);
scalar_t__ listen(int, int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_attach_socket(int *port) {
  int s = socket(AF_INET6, SOCK_STREAM, 0);
  if (s != -1) {
    struct sockaddr_in6 addr = {AF_INET6, 0};
    socklen_t addrlen = sizeof(addr);
    if (bind(s, (struct sockaddr *)&addr, addrlen) == 0 && listen(s, 0) == 0 &&
        getsockname(s, (struct sockaddr *)&addr, &addrlen) == 0) {
      *port = ntohs(addr.sin6_port);
      return s;
    }
  } else if ((s = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
    struct sockaddr_in addr = {AF_INET, 0};
    socklen_t addrlen = sizeof(addr);
    if (bind(s, (struct sockaddr *)&addr, addrlen) == 0 && listen(s, 0) == 0 &&
        getsockname(s, (struct sockaddr *)&addr, &addrlen) == 0) {
      *port = ntohs(addr.sin_port);
      return s;
    }
  }
  close_with_errno(s);
  return -1;
}


x86 assembly (clang, -O0):
<create_attach_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     121a <create_attach_socket+0xba>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xc,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jne    1215 <create_attach_socket+0xb5>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1030 <listen@plt>
cmp    $0x0,%rax
jne    1215 <create_attach_socket+0xb5>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1070 <getsockname@plt>
cmp    $0x0,%rax
jne    1215 <create_attach_socket+0xb5>
mov    -0x20(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12db <create_attach_socket+0x17b>
jmp    12cc <create_attach_socket+0x16c>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     12c7 <create_attach_socket+0x167>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0xc,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jne    12c2 <create_attach_socket+0x162>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1030 <listen@plt>
cmp    $0x0,%rax
jne    12c2 <create_attach_socket+0x162>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1070 <getsockname@plt>
cmp    $0x0,%rax
jne    12c2 <create_attach_socket+0x162>
mov    -0x30(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12db <create_attach_socket+0x17b>
jmp    12c7 <create_attach_socket+0x167>
jmp    12cc <create_attach_socket+0x16c>
mov    -0x14(%rbp),%edi
call   1040 <close_with_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_attach_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e6c(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e53(%rip),%r15
mov    (%r15),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
jne    11a4 <create_attach_socket+0x44>
mov    0x2e55(%rip),%rbx
mov    (%rbx),%edi
mov    (%r15),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     1205 <create_attach_socket+0xa5>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
movq   $0x0,0xc(%rsp)
movl   $0xc,0x4(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
jne    120a <create_attach_socket+0xaa>
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <listen@plt>
test   %rax,%rax
jne    120a <create_attach_socket+0xaa>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1070 <getsockname@plt>
test   %rax,%rax
jne    120a <create_attach_socket+0xaa>
mov    0x8(%rsp),%edi
call   1050 <ntohs@plt>
mov    %eax,(%r14)
jmp    1216 <create_attach_socket+0xb6>
mov    $0xffffffff,%ebp
mov    %ebp,%edi
call   1040 <close_with_errno@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%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_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @create_attach_socket(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in6
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i32* %0, i32** %3
%9 = load i32, i32* @AF_INET6
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %40
14: ; preds = %1
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
%16 = load i32, i32* @AF_INET6
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 1
store i32 0, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 2
store i32 0, i32* %18
store i32 12, i32* %6
%19 = load i32, i32* %4
%20 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%21 = load i32, i32* %6
%22 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %39
24: ; preds = %14
%25 = load i32, i32* %4
%26 = call i64 @listen(i32 %25, i32 0)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %39
28: ; preds = %24
%29 = load i32, i32* %4
%30 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%31 = call i64 @getsockname(i32 %29, %struct.sockaddr* %30, i32* %6)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @ntohs(i32 %35)
%37 = load i32*, i32** %3
store i32 %36, i32* %37
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %75
39: ; preds = %28, %24, %14
br label %72
40: ; preds = %1
%41 = load i32, i32* @AF_INET
%42 = load i32, i32* @SOCK_STREAM
%43 = call i32 @socket(i32 %41, i32 %42, i32 0)
store i32 %43, i32* %4
%44 = icmp ne i32 %43, -1
br i1 %44, label %45, label %71
45: ; preds = %40
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%47 = load i32, i32* @AF_INET
store i32 %47, i32* %46
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 0, i32* %48
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 0, i32* %49
store i32 12, i32* %8
%50 = load i32, i32* %4
%51 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%52 = load i32, i32* %8
%53 = call i64 @bind(i32 %50, %struct.sockaddr* %51, i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %70
55: ; preds = %45
%56 = load i32, i32* %4
%57 = call i64 @listen(i32 %56, i32 0)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %70
59: ; preds = %55
%60 = load i32, i32* %4
%61 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%62 = call i64 @getsockname(i32 %60, %struct.sockaddr* %61, i32* %8)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %70
64: ; preds = %59
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 @ntohs(i32 %66)
%68 = load i32*, i32** %3
store i32 %67, i32* %68
%69 = load i32, i32* %4
store i32 %69, i32* %2
br label %75
70: ; preds = %59, %55, %45
br label %71
71: ; preds = %70, %40
br label %72
72: ; preds = %71, %39
%73 = load i32, i32* %4
%74 = call i32 @close_with_errno(i32 %73)
store i32 -1, i32* %2
br label %75
75: ; preds = %72, %64, %33
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @create_attach_socket(i32* nocapture writeonly %0) {
%2 = alloca %struct.sockaddr_in6
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = load i32, i32* @AF_INET6
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %30, label %10
10: ; preds = %1
%11 = bitcast %struct.sockaddr_in6* %2 to i8*
%12 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
%13 = load i32, i32* @AF_INET6
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 2
store i32 0, i32* %15
%16 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%17 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%18 = call i64 @bind(i32 %8, %struct.sockaddr* %17, i32 12)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %10
%21 = call i64 @listen(i32 %8, i32 0)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = call i64 @getsockname(i32 %8, %struct.sockaddr* %17, i32* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* %12
%28 = call i32 @ntohs(i32 %27)
store i32 %28, i32* %0
br label %58
29: ; preds = %10, %20, %23
br label %55
30: ; preds = %1
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = tail call i32 @socket(i32 %31, i32 %32, i32 0)
%34 = icmp eq i32 %33, -1
br i1 %34, label %55, label %35
35: ; preds = %30
%36 = bitcast %struct.sockaddr_in* %4 to i8*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%38 = load i32, i32* @AF_INET
store i32 %38, i32* %37
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 0, i32* %39
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 0, i32* %40
%41 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%42 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%43 = call i64 @bind(i32 %33, %struct.sockaddr* %42, i32 12)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %54
45: ; preds = %35
%46 = call i64 @listen(i32 %33, i32 0)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = call i64 @getsockname(i32 %33, %struct.sockaddr* %42, i32* %5)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* %37
%53 = call i32 @ntohs(i32 %52)
store i32 %53, i32* %0
br label %58
54: ; preds = %35, %45, %48
br label %55
55: ; preds = %54, %29, %30
%56 = phi i32 [ %8, %29 ], [ %33, %54 ], [ -1, %30 ]
%57 = call i32 @close_with_errno(i32 %56)
br label %58
58: ; preds = %51, %26, %55
%59 = phi i32 [ -1, %55 ], [ %8, %26 ], [ %33, %51 ]
ret i32 %59
}