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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int);
int ntohs(int /*<<< orphan*/);
void sockaddr_tostring(struct sockaddr_storage *sa, char *ip, int ip_len,
                       int *port) {
  struct sockaddr_in *s;
  struct sockaddr_in6 *s6;
  if (sa->ss_family == AF_INET) {
    s = (struct sockaddr_in *)sa;
    if (ip != NULL) {
      inet_ntop(AF_INET, (void *)&(s->sin_addr), ip, ip_len);
    }
    if (port != NULL) {
      *port = ntohs(s->sin_port);
    }
  } else {
    s6 = (struct sockaddr_in6 *)sa;
    if (ip != NULL) {
      inet_ntop(AF_INET6, (void *)&(s6->sin6_addr), ip, ip_len);
    }
    if (port != NULL) {
      *port = ntohs(s6->sin6_port);
    }
  }
}


x86 assembly (clang, -O0):
<sockaddr_tostring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2ea3(%rip),%rcx
cmp    (%rcx),%rax
jne    11a2 <sockaddr_tostring+0x82>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117f <sockaddr_tostring+0x5f>
mov    0x2e80(%rip),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     119d <sockaddr_tostring+0x7d>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11f6 <sockaddr_tostring+0xd6>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d3 <sockaddr_tostring+0xb3>
mov    0x2e24(%rip),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <inet_ntop@plt>
cmpq   $0x0,-0x20(%rbp)
je     11f1 <sockaddr_tostring+0xd1>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    11f6 <sockaddr_tostring+0xd6>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr_tostring>:
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%ecx
mov    %rsi,%rdx
mov    %rdi,%rbx
mov    (%rdi),%rdi
mov    0x2eaf(%rip),%rax
cmp    (%rax),%rdi
jne    1145 <sockaddr_tostring+0x25>
test   %rdx,%rdx
jne    1154 <sockaddr_tostring+0x34>
jmp    115d <sockaddr_tostring+0x3d>
test   %rdx,%rdx
je     115d <sockaddr_tostring+0x3d>
mov    0x2e8f(%rip),%rax
mov    (%rax),%rdi
lea    0x4(%rbx),%rsi
call   1030 <inet_ntop@plt>
test   %r14,%r14
je     116c <sockaddr_tostring+0x4c>
mov    (%rbx),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define void @sockaddr_tostring(%struct.sockaddr_storage* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca %struct.sockaddr_storage*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.sockaddr_in6*
store %struct.sockaddr_storage* %0, %struct.sockaddr_storage** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%11 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%12 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %11, i32 0, i32 0
%13 = load i64, i64* %12
%14 = load i64, i64* @AF_INET
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %39
16: ; preds = %4
%17 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%18 = bitcast %struct.sockaddr_storage* %17 to %struct.sockaddr_in*
store %struct.sockaddr_in* %18, %struct.sockaddr_in** %9
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %29
21: ; preds = %16
%22 = load i64, i64* @AF_INET
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 1
%25 = bitcast i32* %24 to i8*
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = call i32 @inet_ntop(i64 %22, i8* %25, i8* %26, i32 %27)
br label %29
29: ; preds = %21, %16
%30 = load i32*, i32** %8
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @ntohs(i32 %35)
%37 = load i32*, i32** %8
store i32 %36, i32* %37
br label %38
38: ; preds = %32, %29
br label %62
39: ; preds = %4
%40 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %5
%41 = bitcast %struct.sockaddr_storage* %40 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %41, %struct.sockaddr_in6** %10
%42 = load i8*, i8** %6
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %52
44: ; preds = %39
%45 = load i64, i64* @AF_INET6
%46 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %10
%47 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %46, i32 0, i32 1
%48 = bitcast i32* %47 to i8*
%49 = load i8*, i8** %6
%50 = load i32, i32* %7
%51 = call i32 @inet_ntop(i64 %45, i8* %48, i8* %49, i32 %50)
br label %52
52: ; preds = %44, %39
%53 = load i32*, i32** %8
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %61
55: ; preds = %52
%56 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %10
%57 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @ntohs(i32 %58)
%60 = load i32*, i32** %8
store i32 %59, i32* %60
br label %61
61: ; preds = %55, %52
br label %62
62: ; preds = %61, %38
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define void @sockaddr_tostring(%struct.sockaddr_storage* %0, i8* %1, i32 %2, i32* writeonly %3) {
%5 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %0, i64 0, i32 0
%6 = load i64, i64* %5
%7 = load i64, i64* @AF_INET
%8 = icmp eq i64 %6, %7
%9 = icmp eq i8* %1, null
br i1 %8, label %10, label %18
10: ; preds = %4
br i1 %9, label %16, label %11
11: ; preds = %10
%12 = bitcast %struct.sockaddr_storage* %0 to %struct.sockaddr_in*
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i64 0, i32 1
%14 = bitcast i32* %13 to i8*
%15 = tail call i32 @inet_ntop(i64 %6, i8* %14, i8* %1, i32 %2)
br label %16
16: ; preds = %11, %10
%17 = icmp eq i32* %3, null
br i1 %17, label %31, label %27
18: ; preds = %4
br i1 %9, label %25, label %19
19: ; preds = %18
%20 = bitcast %struct.sockaddr_storage* %0 to %struct.sockaddr_in6*
%21 = load i64, i64* @AF_INET6
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %20, i64 0, i32 1
%23 = bitcast i32* %22 to i8*
%24 = tail call i32 @inet_ntop(i64 %21, i8* %23, i8* %1, i32 %2)
br label %25
25: ; preds = %19, %18
%26 = icmp eq i32* %3, null
br i1 %26, label %31, label %27
27: ; preds = %25, %16
%28 = bitcast %struct.sockaddr_storage* %0 to i32*
%29 = load i32, i32* %28
%30 = tail call i32 @ntohs(i32 %29)
store i32 %30, i32* %3
br label %31
31: ; preds = %27, %25, %16
ret void
}