#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;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int);
int ntohs(int /*<<< orphan*/);
int ez_net_socket_name(int fd, char *ip, size_t ip_len, int *port) {
  struct sockaddr_storage sa;
  socklen_t salen = sizeof(sa);
  if (getsockname(fd, (struct sockaddr *)&sa, &salen) == -1) {
    if (ip) {
      ip[0] = '?';
      ip[1] = '\0';
    }
    if (port)
      *port = 0;
    return -1;
  }
  if (sa.ss_family == AF_INET) {
    struct sockaddr_in *s = (struct sockaddr_in *)&sa;
    if (ip)
      inet_ntop(AF_INET, (void *)&(s->sin_addr), ip, (socklen_t)ip_len);
    if (port)
      *port = ntohs(s->sin_port);
  } else {
    struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
    if (ip)
      inet_ntop(AF_INET6, (void *)&(s->sin6_addr), ip, (socklen_t)ip_len);
    if (port)
      *port = ntohs(s->sin6_port);
  }
  return 0;
}


x86 assembly (clang, -O0):
<ez_net_socket_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    11a2 <ez_net_socket_name+0x72>
cmpq   $0x0,-0x10(%rbp)
je     1181 <ez_net_socket_name+0x51>
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
cmpq   $0x0,-0x20(%rbp)
je     1196 <ez_net_socket_name+0x66>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <ez_net_socket_name+0x13b>
mov    -0x28(%rbp),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    120d <ez_net_socket_name+0xdd>
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ea <ez_net_socket_name+0xba>
mov    0x2e18(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     1208 <ez_net_socket_name+0xd8>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1264 <ez_net_socket_name+0x134>
lea    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1241 <ez_net_socket_name+0x111>
mov    0x2db9(%rip),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     125f <ez_net_socket_name+0x12f>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1264 <ez_net_socket_name+0x134>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ez_net_socket_name>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
movl   $0x8,0xc(%rsp)
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
call   1050 <getsockname@plt>
cmp    $0xffffffff,%eax
je     118a <ez_net_socket_name+0x5a>
mov    (%rsp),%rdi
mov    0x2e81(%rip),%rax
cmp    (%rax),%rdi
jne    11a2 <ez_net_socket_name+0x72>
test   %rbx,%rbx
je     1181 <ez_net_socket_name+0x51>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <inet_ntop@plt>
test   %r14,%r14
jne    11c6 <ez_net_socket_name+0x96>
xor    %eax,%eax
jmp    11d5 <ez_net_socket_name+0xa5>
test   %rbx,%rbx
je     1194 <ez_net_socket_name+0x64>
movw   $0x3f,(%rbx)
xor    %ecx,%ecx
mov    $0xffffffff,%eax
test   %r14,%r14
jne    11d2 <ez_net_socket_name+0xa2>
jmp    11d5 <ez_net_socket_name+0xa5>
test   %rbx,%rbx
je     11c1 <ez_net_socket_name+0x91>
mov    0x2e32(%rip),%rax
mov    (%rax),%rdi
lea    0x4(%rsp),%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <inet_ntop@plt>
test   %r14,%r14
je     1186 <ez_net_socket_name+0x56>
mov    (%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
xor    %eax,%eax
mov    %ecx,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @ez_net_socket_name(i32 %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca %struct.sockaddr_storage
%11 = alloca i32
%12 = alloca %struct.sockaddr_in*
%13 = alloca %struct.sockaddr_in6*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i32 8, i32* %11
%14 = load i32, i32* %6
%15 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr*
%16 = call i32 @getsockname(i32 %14, %struct.sockaddr* %15, i32* %11)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %32
18: ; preds = %4
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 63, i8* %23
%24 = load i8*, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i64 1
store i8 0, i8* %25
br label %26
26: ; preds = %21, %18
%27 = load i32*, i32** %9
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i32*, i32** %9
store i32 0, i32* %30
br label %31
31: ; preds = %29, %26
store i32 -1, i32* %5
br label %84
32: ; preds = %4
%33 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %10, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load i64, i64* @AF_INET
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %60
37: ; preds = %32
%38 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in*
store %struct.sockaddr_in* %38, %struct.sockaddr_in** %12
%39 = load i8*, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %50
41: ; preds = %37
%42 = load i64, i64* @AF_INET
%43 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %43, i32 0, i32 1
%45 = bitcast i32* %44 to i8*
%46 = load i8*, i8** %7
%47 = load i64, i64* %8
%48 = trunc i64 %47 to i32
%49 = call i32 @inet_ntop(i64 %42, i8* %45, i8* %46, i32 %48)
br label %50
50: ; preds = %41, %37
%51 = load i32*, i32** %9
%52 = icmp ne i32* %51, null
br i1 %52, label %53, label %59
53: ; preds = %50
%54 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @ntohs(i32 %56)
%58 = load i32*, i32** %9
store i32 %57, i32* %58
br label %59
59: ; preds = %53, %50
br label %83
60: ; preds = %32
%61 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %61, %struct.sockaddr_in6** %13
%62 = load i8*, i8** %7
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %73
64: ; preds = %60
%65 = load i64, i64* @AF_INET6
%66 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%67 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %66, i32 0, i32 1
%68 = bitcast i32* %67 to i8*
%69 = load i8*, i8** %7
%70 = load i64, i64* %8
%71 = trunc i64 %70 to i32
%72 = call i32 @inet_ntop(i64 %65, i8* %68, i8* %69, i32 %71)
br label %73
73: ; preds = %64, %60
%74 = load i32*, i32** %9
%75 = icmp ne i32* %74, null
br i1 %75, label %76, label %82
76: ; preds = %73
%77 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%78 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %77, i32 0, i32 0
%79 = load i32, i32* %78
%80 = call i32 @ntohs(i32 %79)
%81 = load i32*, i32** %9
store i32 %80, i32* %81
br label %82
82: ; preds = %76, %73
br label %83
83: ; preds = %82, %59
store i32 0, i32* %5
br label %84
84: ; preds = %83, %31
%85 = load i32, i32* %5
ret i32 %85
}

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