#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 sockaddr_storage {
  int dummy;
};
struct sockaddr_in6 {
  int sin6_family;
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_port;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int sin_family;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ EAFNOSUPPORT;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ INADDR_ANY;
int SOCK_DGRAM;
int SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ in6addr_any;
int socket(int, int, int /*<<< orphan*/);
int bound_socket(int domain, int type) {
  int sock;
  struct sockaddr_storage addr;
  struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
  struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
  socklen_t slen;
  switch (domain) {
  case AF_INET:
    addr4->sin_family = AF_INET;
    addr4->sin_port = 0;
    addr4->sin_addr.s_addr = INADDR_ANY;
    slen = sizeof(*addr4);
    break;
  case AF_INET6:
    addr6->sin6_family = AF_INET6;
    addr6->sin6_port = 0;
    addr6->sin6_addr = in6addr_any;
    slen = sizeof(*addr6);
    break;
  default:
    errno = EAFNOSUPPORT;
    return -1;
  }
  if ((type != SOCK_STREAM) && (type != SOCK_DGRAM)) {
    errno = EINVAL;
    return -1;
  }
  sock = socket(domain, type, 0);
  if (sock < 0)
    return -1;
  if (bind(sock, (struct sockaddr *)&addr, slen) < 0) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<bound_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0x80,%eax
je     11a4 <bound_socket+0x74>
jmp    1160 <bound_socket+0x30>
mov    -0x30(%rbp),%eax
sub    $0x81,%eax
jne    11d5 <bound_socket+0xa5>
jmp    1173 <bound_socket+0x43>
mov    -0x20(%rbp),%rax
movl   $0x81,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,0x8(%rax)
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0xc,-0x2c(%rbp)
jmp    11f3 <bound_socket+0xc3>
mov    -0x28(%rbp),%rax
movl   $0x80,(%rax)
mov    -0x28(%rbp),%rax
movl   $0x0,0x8(%rax)
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0xc,-0x2c(%rbp)
jmp    11f3 <bound_socket+0xc3>
mov    0x2dd4(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddb(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <bound_socket+0x15e>
mov    -0xc(%rbp),%eax
mov    0x2dcb(%rip),%rcx
cmp    (%rcx),%eax
je     1235 <bound_socket+0x105>
mov    -0xc(%rbp),%eax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%eax
je     1235 <bound_socket+0x105>
mov    0x2d9a(%rip),%rax
mov    (%rax),%ecx
mov    0x2d99(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <bound_socket+0x15e>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    125b <bound_socket+0x12b>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <bound_socket+0x15e>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
jge    1288 <bound_socket+0x158>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <bound_socket+0x15e>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bound_socket>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x81,%edi
je     114c <bound_socket+0x1c>
cmp    $0x80,%edi
jne    1178 <bound_socket+0x48>
mov    0x2ea6(%rip),%rax
jmp    1153 <bound_socket+0x23>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2e68(%rip),%rax
cmp    %esi,(%rax)
je     1198 <bound_socket+0x68>
mov    0x2e6d(%rip),%rax
cmp    %esi,(%rax)
je     1198 <bound_socket+0x68>
mov    0x2e42(%rip),%rax
jmp    117f <bound_socket+0x4f>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    0x2e38(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
xor    %edx,%edx
call   1050 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     118f <bound_socket+0x5f>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1040 <bind@plt>
test   %rax,%rax
js     11c2 <bound_socket+0x92>
mov    %ebp,%ebx
jmp    118f <bound_socket+0x5f>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    118f <bound_socket+0x5f>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@INADDR_ANY = global i32 0
@in6addr_any = global i32 0
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_DGRAM = global i32 0
@EINVAL = global i32 0
define i32 @bound_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage
%8 = alloca %struct.sockaddr_in*
%9 = alloca %struct.sockaddr_in6*
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in*
store %struct.sockaddr_in* %11, %struct.sockaddr_in** %8
%12 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %12, %struct.sockaddr_in6** %9
%13 = load i32, i32* %4
switch i32 %13, label %31 [
i32 129, label %14
i32 128, label %23
]
14: ; preds = %2
%15 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i32 129, i32* %16
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 2
store i32 0, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %19, i32* %22
store i32 12, i32* %10
br label %33
23: ; preds = %2
%24 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %9
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %24, i32 0, i32 0
store i32 128, i32* %25
%26 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %9
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %26, i32 0, i32 2
store i32 0, i32* %27
%28 = load i32, i32* @in6addr_any
%29 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %9
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %29, i32 0, i32 1
store i32 %28, i32* %30
store i32 12, i32* %10
br label %33
31: ; preds = %2
%32 = load i32, i32* @EAFNOSUPPORT
store i32 %32, i32* @errno
store i32 -1, i32* %3
br label %61
33: ; preds = %23, %14
%34 = load i32, i32* %5
%35 = load i32, i32* @SOCK_STREAM
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i32, i32* %5
%39 = load i32, i32* @SOCK_DGRAM
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = load i32, i32* @EINVAL
store i32 %42, i32* @errno
store i32 -1, i32* %3
br label %61
43: ; preds = %37, %33
%44 = load i32, i32* %4
%45 = load i32, i32* %5
%46 = call i32 @socket(i32 %44, i32 %45, i32 0)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %43
store i32 -1, i32* %3
br label %61
50: ; preds = %43
%51 = load i32, i32* %6
%52 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%53 = load i32, i32* %10
%54 = call i64 @bind(i32 %51, %struct.sockaddr* %52, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %3
br label %61
59: ; preds = %50
%60 = load i32, i32* %6
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %56, %49, %41, %31
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INADDR_ANY = global i32 0
@in6addr_any = global i32 0
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_DGRAM = global i32 0
@EINVAL = global i32 0
define i32 @bound_socket(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_storage
%4 = bitcast %struct.sockaddr_storage* %3 to i8*
switch i32 %0, label %6 [
i32 129, label %8
i32 128, label %5
]
5: ; preds = %2
br label %8
6: ; preds = %2
%7 = load i32, i32* @EAFNOSUPPORT
store i32 %7, i32* @errno
br label %28
8: ; preds = %2, %5
%9 = phi i32* [ @in6addr_any, %5 ], [ @INADDR_ANY, %2 ]
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 1, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = icmp eq i32 %12, %1
%14 = load i32, i32* @SOCK_DGRAM
%15 = icmp eq i32 %14, %1
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %19, label %17
17: ; preds = %8
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
br label %28
19: ; preds = %8
%20 = tail call i32 @socket(i32 %0, i32 %1, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %19
%23 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%24 = call i64 @bind(i32 %20, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @close(i32 %20)
br label %28
28: ; preds = %22, %19, %26, %17, %6
%29 = phi i32 [ -1, %6 ], [ -1, %17 ], [ -1, %26 ], [ -1, %19 ], [ %20, %22 ]
ret i32 %29
}