#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 {
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ INET_ADDRSTRLEN;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *inet_ntop(scalar_t__, int /*<<< orphan*/ *, char *,
                              int /*<<< orphan*/);
char *xmalloc(int /*<<< orphan*/);
char *xntop(const struct sockaddr *sa) {
  char *tmp = NULL;
  if (sa->sa_family == AF_INET) {
    struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    tmp = xmalloc(INET_ADDRSTRLEN);
    if (inet_ntop(sa->sa_family, &sin->sin_addr, tmp, INET_ADDRSTRLEN) ==
        NULL) {
      free(tmp);
      tmp = NULL;
    }
  } else if (sa->sa_family == AF_INET6) {
    struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
    tmp = xmalloc(INET6_ADDRSTRLEN);
    if (inet_ntop(sa->sa_family, &sin6->sin6_addr, tmp, INET6_ADDRSTRLEN) ==
        NULL) {
      free(tmp);
      tmp = NULL;
    }
  }
  return tmp;
}


x86 assembly (clang, -O0):
<xntop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e8e(%rip),%rcx
cmp    (%rcx),%rax
jne    11b2 <xntop+0x82>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2e3d(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    11ad <xntop+0x7d>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    1225 <xntop+0xf5>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <xntop+0xf0>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    121b <xntop+0xeb>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    1220 <xntop+0xf0>
jmp    1225 <xntop+0xf5>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xntop>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    (%rdi),%rax
mov    0x2e9e(%rip),%rcx
cmp    (%rcx),%rax
jne    1150 <xntop+0x20>
mov    0x2e7a(%rip),%r15
jmp    1163 <xntop+0x33>
mov    0x2e81(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <xntop+0x5c>
mov    0x2e85(%rip),%r15
mov    (%r15),%edi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    (%rbx),%rdi
mov    (%r15),%ecx
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <inet_ntop@plt>
test   %rax,%rax
jne    118f <xntop+0x5f>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@INET_ADDRSTRLEN = global i32 0
@AF_INET6 = global i64 0
@INET6_ADDRSTRLEN = global i32 0
define i8* @xntop(%struct.sockaddr* %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i64, i64* %2
%4 = load i64, i64* @AF_INET
%5 = icmp eq i64 %3, %4
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = load i32, i32* @INET_ADDRSTRLEN
%8 = tail call i8* @xmalloc(i32 %7)
%9 = load i64, i64* %2
%10 = bitcast %struct.sockaddr* %0 to i32*
%11 = load i32, i32* @INET_ADDRSTRLEN
%12 = tail call i32* @inet_ntop(i64 %9, i32* %10, i8* %8, i32 %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %25, label %28
14: ; preds = %1
%15 = load i64, i64* @AF_INET6
%16 = icmp eq i64 %3, %15
br i1 %16, label %17, label %28
17: ; preds = %14
%18 = load i32, i32* @INET6_ADDRSTRLEN
%19 = tail call i8* @xmalloc(i32 %18)
%20 = load i64, i64* %2
%21 = bitcast %struct.sockaddr* %0 to i32*
%22 = load i32, i32* @INET6_ADDRSTRLEN
%23 = tail call i32* @inet_ntop(i64 %20, i32* %21, i8* %19, i32 %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %17, %6
%26 = phi i8* [ %8, %6 ], [ %19, %17 ]
%27 = tail call i32 @free(i8* %26)
br label %28
28: ; preds = %25, %17, %6, %14
%29 = phi i8* [ null, %14 ], [ %8, %6 ], [ %19, %17 ], [ null, %25 ]
ret i8* %29
}