#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_storage {
  scalar_t__ ss_family;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int evutil_socket_t;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ evutil_make_socket_nonblocking(int);
int /*<<< orphan*/ evutil_parse_sockaddr_port(char *, struct sockaddr *, int *);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
short ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
evutil_socket_t create_socket(char *addr, short *port) {
  evutil_socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1) {
    printf("socket() failed\n");
    return -1;
  }
  evutil_make_socket_nonblocking(sock);
  struct sockaddr_storage ss;
  int len = sizeof(struct sockaddr_storage);
  evutil_parse_sockaddr_port(addr, (struct sockaddr *)&ss, &len);
  if (bind(sock, (struct sockaddr *)&ss, len)) {
    printf("bind() failed: %s\n", strerror(errno));
    return -1;
  }
  getsockname(sock, (struct sockaddr *)&ss, &len);
  if (ss.ss_family == AF_INET) {
    *port = ntohs(((struct sockaddr_in *)&ss)->sin_port);
  } else if (ss.ss_family == AF_INET6) {
    *port = ntohs(((struct sockaddr_in6 *)&ss)->sin6_port);
  } else {
    printf("Address type not recognized\n");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d1 <create_socket+0x51>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <create_socket+0x140>
mov    -0x1c(%rbp),%edi
call   1080 <evutil_make_socket_nonblocking@plt>
movl   $0x8,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1030 <evutil_parse_sockaddr_port@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
je     1235 <create_socket+0xb5>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <create_socket+0x140>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <getsockname@plt>
mov    -0x28(%rbp),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
jne    1270 <create_socket+0xf0>
mov    -0x28(%rbp),%edi
call   1050 <ntohs@plt>
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %cx,(%rax)
jmp    12ba <create_socket+0x13a>
mov    -0x28(%rbp),%rax
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%rax
jne    129b <create_socket+0x11b>
mov    -0x28(%rbp),%edi
call   1050 <ntohs@plt>
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %cx,(%rax)
jmp    12b5 <create_socket+0x135>
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <create_socket+0x140>
jmp    12ba <create_socket+0x13a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e3e(%rip),%r13
mov    0x0(%r13),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1222 <create_socket+0x92>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <evutil_make_socket_nonblocking@plt>
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%r12
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <evutil_parse_sockaddr_port@plt>
mov    0x4(%rsp),%edx
mov    %ebx,%edi
mov    %r12,%rsi
call   1070 <bind@plt>
test   %rax,%rax
je     122b <create_socket+0x9b>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdef(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    126f <create_socket+0xdf>
lea    0xe06(%rip),%rdi
jmp    125a <create_socket+0xca>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1080 <getsockname@plt>
mov    0x8(%rsp),%rdi
cmp    0x0(%r13),%rdi
je     1266 <create_socket+0xd6>
mov    0x2d92(%rip),%rax
cmp    (%rax),%rdi
je     1266 <create_socket+0xd6>
lea    0xdb9(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
jmp    126f <create_socket+0xdf>
call   1060 <ntohs@plt>
mov    %ax,(%r14)
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i64 0
@SOCK_STREAM = global i32 0
@.str = [17 x i8] c"socket() failed\0A\00"
@.str.1 = [19 x i8] c"bind() failed: %s\0A\00"
@errno = global i32 0
@AF_INET6 = global i64 0
@.str.2 = [29 x i8] c"Address type not recognized\0A\00"
define i32 @create_socket(i8* %0, i16* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16*
%6 = alloca i32
%7 = alloca %struct.sockaddr_storage
%8 = alloca i32
store i8* %0, i8** %4
store i16* %1, i16** %5
%9 = load i64, i64* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i64 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32 @evutil_make_socket_nonblocking(i32 %17)
store i32 8, i32* %8
%19 = load i8*, i8** %4
%20 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%21 = call i32 @evutil_parse_sockaddr_port(i8* %19, %struct.sockaddr* %20, i32* %8)
%22 = load i32, i32* %6
%23 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%24 = load i32, i32* %8
%25 = call i64 @bind(i32 %22, %struct.sockaddr* %23, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %16
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i32 -1, i32* %3
br label %61
31: ; preds = %16
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%34 = call i32 @getsockname(i32 %32, %struct.sockaddr* %33, i32* %8)
%35 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %7, i32 0, i32 0
%36 = load i64, i64* %35
%37 = load i64, i64* @AF_INET
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %45
39: ; preds = %31
%40 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in*
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call signext i16 @ntohs(i32 %42)
%44 = load i16*, i16** %5
store i16 %43, i16* %44
br label %59
45: ; preds = %31
%46 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %7, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i64, i64* @AF_INET6
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %56
50: ; preds = %45
%51 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in6*
%52 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call signext i16 @ntohs(i32 %53)
%55 = load i16*, i16** %5
store i16 %54, i16* %55
br label %58
56: ; preds = %45
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
58: ; preds = %50
br label %59
59: ; preds = %58, %39
%60 = load i32, i32* %6
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %56, %27, %14
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
@SOCK_STREAM = global i32 0
@.str.1 = [19 x i8] c"bind() failed: %s\0A\00"
@errno = global i32 0
@AF_INET6 = global i64 0
@str = [28 x i8] c"Address type not recognized\00"
@str.3 = [16 x i8] c"socket() failed\00"
define i32 @create_socket(i8* %0, i16* nocapture writeonly %1) {
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = load i64, i64* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i64 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.3, i64 0, i64 0))
br label %40
11: ; preds = %2
%12 = tail call i32 @evutil_make_socket_nonblocking(i32 %7)
%13 = bitcast %struct.sockaddr_storage* %3 to i8*
%14 = bitcast i32* %4 to i8*
store i32 8, i32* %4
%15 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%16 = call i32 @evutil_parse_sockaddr_port(i8* %0, %struct.sockaddr* %15, i32* %4)
%17 = load i32, i32* %4
%18 = call i64 @bind(i32 %7, %struct.sockaddr* %15, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %11
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %38
24: ; preds = %11
%25 = call i32 @getsockname(i32 %7, %struct.sockaddr* %15, i32* %4)
%26 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @AF_INET
%29 = icmp eq i64 %27, %28
%30 = load i64, i64* @AF_INET6
%31 = icmp eq i64 %27, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %35, label %33
33: ; preds = %24
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %38
35: ; preds = %24
%36 = trunc i64 %27 to i32
%37 = call signext i16 @ntohs(i32 %36)
store i16 %37, i16* %1
br label %38
38: ; preds = %35, %33, %20
%39 = phi i32 [ -1, %20 ], [ %7, %35 ], [ -1, %33 ]
br label %40
40: ; preds = %38, %9
%41 = phi i32 [ -1, %9 ], [ %39, %38 ]
ret i32 %41
}