#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;

/* 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 *, size_t);
int ntohs(int /*<<< orphan*/);
int local(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 (port)
      *port = 0;
    if (ip)
      ip[0] = '?', ip[1] = '\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, 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, ip_len);
    if (port)
      *port = ntohs(s->sin6_port);
  }
  return 0;
}


x86 assembly (clang, -O0):
<local>:
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 <local+0x72>
cmpq   $0x0,-0x20(%rbp)
je     117c <local+0x4c>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     1196 <local+0x66>
mov    -0x10(%rbp),%rax
movb   $0x3f,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,0x1(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <local+0x137>
mov    -0x28(%rbp),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    120b <local+0xdb>
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e8 <local+0xb8>
mov    0x2e18(%rip),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     1206 <local+0xd6>
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1260 <local+0x130>
lea    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     123d <local+0x10d>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rdi
mov    -0x40(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     125b <local+0x12b>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1260 <local+0x130>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<local>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
movl   $0x8,0x14(%rsp)
lea    0x8(%rsp),%rsi
lea    0x14(%rsp),%rdx
call   1050 <getsockname@plt>
cmp    $0xffffffff,%eax
je     1177 <local+0x47>
mov    0x8(%rsp),%rdi
mov    0x2e7d(%rip),%rax
cmp    (%rax),%rdi
jne    1194 <local+0x64>
test   %rbx,%rbx
jne    11a3 <local+0x73>
jmp    11b3 <local+0x83>
test   %r14,%r14
je     1183 <local+0x53>
movl   $0x0,(%r14)
mov    $0xffffffff,%ebp
test   %rbx,%rbx
je     11c6 <local+0x96>
movw   $0x3f,(%rbx)
jmp    11c6 <local+0x96>
test   %rbx,%rbx
je     11b3 <local+0x83>
mov    0x2e40(%rip),%rax
mov    (%rax),%rdi
lea    0xc(%rsp),%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <inet_ntop@plt>
xor    %ebp,%ebp
test   %r14,%r14
je     11c6 <local+0x96>
mov    0x8(%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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 @local(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 i32*, i32** %9
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i32*, i32** %9
store i32 0, i32* %22
br label %23
23: ; preds = %21, %18
%24 = load i8*, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i64 0
store i8 63, i8* %28
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 1
store i8 0, i8* %30
br label %31
31: ; preds = %26, %23
store i32 -1, i32* %5
br label %82
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 %59
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 %49
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 = call i32 @inet_ntop(i64 %42, i8* %45, i8* %46, i64 %47)
br label %49
49: ; preds = %41, %37
%50 = load i32*, i32** %9
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %58
52: ; preds = %49
%53 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @ntohs(i32 %55)
%57 = load i32*, i32** %9
store i32 %56, i32* %57
br label %58
58: ; preds = %52, %49
br label %81
59: ; preds = %32
%60 = bitcast %struct.sockaddr_storage* %10 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %60, %struct.sockaddr_in6** %13
%61 = load i8*, i8** %7
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %71
63: ; preds = %59
%64 = load i64, i64* @AF_INET6
%65 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%66 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %65, i32 0, i32 1
%67 = bitcast i32* %66 to i8*
%68 = load i8*, i8** %7
%69 = load i64, i64* %8
%70 = call i32 @inet_ntop(i64 %64, i8* %67, i8* %68, i64 %69)
br label %71
71: ; preds = %63, %59
%72 = load i32*, i32** %9
%73 = icmp ne i32* %72, null
br i1 %73, label %74, label %80
74: ; preds = %71
%75 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%76 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %75, i32 0, i32 0
%77 = load i32, i32* %76
%78 = call i32 @ntohs(i32 %77)
%79 = load i32*, i32** %9
store i32 %78, i32* %79
br label %80
80: ; preds = %74, %71
br label %81
81: ; preds = %80, %58
store i32 0, i32* %5
br label %82
82: ; preds = %81, %31
%83 = load i32, i32* %5
ret i32 %83
}

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 @local(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 %19
12: ; preds = %4
%13 = icmp eq i32* %3, null
br i1 %13, label %15, label %14
14: ; preds = %12
store i32 0, i32* %3
br label %15
15: ; preds = %14, %12
%16 = icmp eq i8* %1, null
br i1 %16, label %50, label %17
17: ; preds = %15
store i8 63, i8* %1
%18 = getelementptr inbounds i8, i8* %1, i64 1
store i8 0, i8* %18
br label %50
19: ; preds = %4
%20 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %5, i64 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @AF_INET
%23 = icmp eq i64 %21, %22
%24 = icmp eq i8* %1, null
br i1 %23, label %25, label %37
25: ; preds = %19
br i1 %24, label %31, label %26
26: ; preds = %25
%27 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i64 0, i32 1
%29 = bitcast i32* %28 to i8*
%30 = call i32 @inet_ntop(i64 %21, i8* %29, i8* %1, i64 %2)
br label %31
31: ; preds = %26, %25
%32 = icmp eq i32* %3, null
br i1 %32, label %50, label %33
33: ; preds = %31
%34 = bitcast %struct.sockaddr_storage* %5 to i32*
%35 = load i32, i32* %34
%36 = call i32 @ntohs(i32 %35)
store i32 %36, i32* %3
br label %50
37: ; preds = %19
br i1 %24, label %44, label %38
38: ; preds = %37
%39 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in6*
%40 = load i64, i64* @AF_INET6
%41 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %39, i64 0, i32 1
%42 = bitcast i32* %41 to i8*
%43 = call i32 @inet_ntop(i64 %40, i8* %42, i8* %1, i64 %2)
br label %44
44: ; preds = %38, %37
%45 = icmp eq i32* %3, null
br i1 %45, label %50, label %46
46: ; preds = %44
%47 = bitcast %struct.sockaddr_storage* %5 to i32*
%48 = load i32, i32* %47
%49 = call i32 @ntohs(i32 %48)
store i32 %49, i32* %3
br label %50
50: ; preds = %33, %31, %46, %44, %15, %17
%51 = phi i32 [ -1, %17 ], [ -1, %15 ], [ 0, %44 ], [ 0, %46 ], [ 0, %31 ], [ 0, %33 ]
ret i32 %51
}