#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;
int /*<<< orphan*/ *inet_ntop(scalar_t__, void const *, char *, size_t);
char *socket_str_address(char *buffer, size_t n,
                         const struct sockaddr *address) {
  const void *addr;
  if (address->sa_family == AF_INET)
    addr = &((const struct sockaddr_in *)address)->sin_addr;
  else
    addr = &((const struct sockaddr_in6 *)address)->sin6_addr;
  if (inet_ntop(address->sa_family, addr, buffer, n) == NULL)
    return (NULL);
  return (buffer);
}


x86 assembly (clang, -O0):
<socket_str_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2eb6(%rip),%rcx
cmp    (%rcx),%rax
jne    1148 <socket_str_address+0x38>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1150 <socket_str_address+0x40>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
jne    117f <socket_str_address+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    1187 <socket_str_address+0x77>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_str_address>:
push   %rbx
mov    %rsi,%rcx
mov    %rdi,%rbx
mov    (%rdx),%rdi
mov    %rdx,%rsi
mov    %rbx,%rdx
call   1030 <inet_ntop@plt>
test   %rax,%rax
cmovne %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
define i8* @socket_str_address(i8* %0, i64 %1, %struct.sockaddr* %2) {
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%5 = bitcast %struct.sockaddr* %2 to i8*
%6 = load i64, i64* %4
%7 = tail call i32* @inet_ntop(i64 %6, i8* %5, i8* %0, i64 %1)
%8 = icmp eq i32* %7, null
%9 = select i1 %8, i8* null, i8* %0
ret i8* %9
}