#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 {
  void *sin6_port;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
void *htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int create_socket(struct sockaddr_in6 *source_addr, int src_port,
                  struct sockaddr_in6 *dest_addr, int dst_port) {
  int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
  if (sockfd == -1) {
    fprintf(stderr, "socket() failed : %s.\n", strerror(errno));
    return (-1);
  }
  if (source_addr != NULL) {
    socklen_t addrlen = sizeof(struct sockaddr_in6);
    if (src_port > 0)
      source_addr->sin6_port = htons(src_port);
    if (bind(sockfd, (struct sockaddr *)source_addr, addrlen) == -1) {
      fprintf(stderr, "bind() failed : %s.\n", strerror(errno));
      return (-1);
    }
  }
  if (dest_addr != NULL) {
    socklen_t addrlen = sizeof(struct sockaddr_in6);
    if (dst_port > 0)
      dest_addr->sin6_port = htons(dst_port);
    if (connect(sockfd, (struct sockaddr *)dest_addr, addrlen) == -1) {
      fprintf(stderr, "connect() failed : %s.\n", strerror(errno));
      return (-1);
    }
  }
  return (sockfd);
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11d4 <create_socket+0x74>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe3d(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e0 <create_socket+0x180>
cmpq   $0x0,-0x10(%rbp)
je     1257 <create_socket+0xf7>
movl   $0x8,-0x2c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1202 <create_socket+0xa2>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    1252 <create_socket+0xf2>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xdd6(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e0 <create_socket+0x180>
jmp    1257 <create_socket+0xf7>
cmpq   $0x0,-0x20(%rbp)
je     12da <create_socket+0x17a>
movl   $0x8,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1285 <create_socket+0x125>
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    12d5 <create_socket+0x175>
mov    0x2d2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xd68(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e0 <create_socket+0x180>
jmp    12da <create_socket+0x17a>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbp
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11f0 <create_socket+0x90>
mov    %eax,%ebx
test   %rbp,%rbp
je     11be <create_socket+0x5e>
test   %r12d,%r12d
jle    11aa <create_socket+0x4a>
mov    %r12d,%edi
call   1030 <htons@plt>
mov    %rax,0x0(%rbp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     11f9 <create_socket+0x99>
test   %r14,%r14
je     1229 <create_socket+0xc9>
test   %r15d,%r15d
jle    11d3 <create_socket+0x73>
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %rax,(%r14)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    1229 <create_socket+0xc9>
lea    0xe3e(%rip),%rbx
jmp    1200 <create_socket+0xa0>
lea    0xe09(%rip),%rbx
jmp    1200 <create_socket+0xa0>
lea    0xe17(%rip),%rbx
mov    0x2dc9(%rip),%rax
mov    (%rax),%ebp
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"socket() failed : %s.\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"bind() failed : %s.\0A\00"
@.str.2 = [24 x i8] c"connect() failed : %s.\0A\00"
define i32 @create_socket(%struct.sockaddr_in6* %0, i32 %1, %struct.sockaddr_in6* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_in6*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in6*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.sockaddr_in6* %0, %struct.sockaddr_in6** %6
store i32 %1, i32* %7
store %struct.sockaddr_in6* %2, %struct.sockaddr_in6** %8
store i32 %3, i32* %9
%13 = load i32, i32* @AF_INET6
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %5
br label %73
23: ; preds = %4
%24 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %6
%25 = icmp ne %struct.sockaddr_in6* %24, null
br i1 %25, label %26, label %47
26: ; preds = %23
store i32 8, i32* %11
%27 = load i32, i32* %7
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* %7
%31 = call i8* @htons(i32 %30)
%32 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %6
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %32, i32 0, i32 0
store i8* %31, i8** %33
br label %34
34: ; preds = %29, %26
%35 = load i32, i32* %10
%36 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %6
%37 = bitcast %struct.sockaddr_in6* %36 to %struct.sockaddr*
%38 = load i32, i32* %11
%39 = call i32 @bind(i32 %35, %struct.sockaddr* %37, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %44)
store i32 -1, i32* %5
br label %73
46: ; preds = %34
br label %47
47: ; preds = %46, %23
%48 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %8
%49 = icmp ne %struct.sockaddr_in6* %48, null
br i1 %49, label %50, label %71
50: ; preds = %47
store i32 8, i32* %12
%51 = load i32, i32* %9
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i32, i32* %9
%55 = call i8* @htons(i32 %54)
%56 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %8
%57 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %56, i32 0, i32 0
store i8* %55, i8** %57
br label %58
58: ; preds = %53, %50
%59 = load i32, i32* %10
%60 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %8
%61 = bitcast %struct.sockaddr_in6* %60 to %struct.sockaddr*
%62 = load i32, i32* %12
%63 = call i32 @connect(i32 %59, %struct.sockaddr* %61, i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %70
65: ; preds = %58
%66 = load i32, i32* @stderr
%67 = load i32, i32* @errno
%68 = call i8* @strerror(i32 %67)
%69 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %68)
store i32 -1, i32* %5
br label %73
70: ; preds = %58
br label %71
71: ; preds = %70, %47
%72 = load i32, i32* %10
store i32 %72, i32* %5
br label %73
73: ; preds = %71, %65, %41, %18
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"socket() failed : %s.\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"bind() failed : %s.\0A\00"
@.str.2 = [24 x i8] c"connect() failed : %s.\0A\00"
define i32 @create_socket(%struct.sockaddr_in6* %0, i32 %1, %struct.sockaddr_in6* %2, i32 %3) {
%5 = load i32, i32* @AF_INET6
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %31, label %9
9: ; preds = %4
%10 = icmp eq %struct.sockaddr_in6* %0, null
br i1 %10, label %20, label %11
11: ; preds = %9
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = tail call i8* @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %0, i64 0, i32 0
store i8* %14, i8** %15
br label %16
16: ; preds = %13, %11
%17 = bitcast %struct.sockaddr_in6* %0 to %struct.sockaddr*
%18 = tail call i32 @bind(i32 %7, %struct.sockaddr* %17, i32 8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %31, label %20
20: ; preds = %16, %9
%21 = icmp eq %struct.sockaddr_in6* %2, null
br i1 %21, label %37, label %22
22: ; preds = %20
%23 = icmp sgt i32 %3, 0
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = tail call i8* @htons(i32 %3)
%26 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i8* %25, i8** %26
br label %27
27: ; preds = %24, %22
%28 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%29 = tail call i32 @connect(i32 %7, %struct.sockaddr* %28, i32 8)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %27, %16, %4
%32 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %16 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), %27 ]
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 @fprintf(i32 %33, i8* %32, i8* %35)
br label %37
37: ; preds = %31, %20, %27
%38 = phi i32 [ %7, %27 ], [ %7, %20 ], [ -1, %31 ]
ret i32 %38
}