#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 {
  int sa_family;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ inet_ntop(int const, int /*<<< orphan*/ *, char *,
                             size_t const);
int /*<<< orphan*/ strncpy(char *, char *, size_t const);
char *get_ip_str(const struct sockaddr *sa, char *dst, const size_t maxlen) {
  switch (sa->sa_family) {
  case AF_INET:
    inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), dst, maxlen);
    break;
  case AF_INET6:
    inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), dst, maxlen);
    break;
  default:
    strncpy(dst, "Unknown AF", maxlen);
    return NULL;
  }
  return dst;
}


x86 assembly (clang, -O0):
<get_ip_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     117b <get_ip_str+0x5b>
jmp    114d <get_ip_str+0x2d>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
jne    1196 <get_ip_str+0x76>
jmp    1160 <get_ip_str+0x40>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x81,%edi
call   1030 <inet_ntop@plt>
jmp    11b7 <get_ip_str+0x97>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x80,%edi
call   1030 <inet_ntop@plt>
jmp    11b7 <get_ip_str+0x97>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe5b(%rip),%rsi
call   1040 <strncpy@plt>
movq   $0x0,-0x8(%rbp)
jmp    11bf <get_ip_str+0x9f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_str>:
push   %rbx
mov    %rdx,%rcx
mov    %rsi,%rbx
mov    %rdi,%rsi
mov    (%rdi),%eax
cmp    $0x80,%eax
je     1141 <get_ip_str+0x21>
cmp    $0x81,%eax
jne    1150 <get_ip_str+0x30>
mov    $0x81,%edi
jmp    1146 <get_ip_str+0x26>
mov    $0x80,%edi
mov    %rbx,%rdx
call   1030 <inet_ntop@plt>
jmp    1164 <get_ip_str+0x44>
lea    0xea9(%rip),%rsi
mov    %rbx,%rdi
mov    %rcx,%rdx
call   1040 <strncpy@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [11 x i8] c"Unknown AF\00"
define i8* @get_ip_str(%struct.sockaddr* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.sockaddr*
%6 = alloca i8*
%7 = alloca i64
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%8 = load %struct.sockaddr*, %struct.sockaddr** %5
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %8, i32 0, i32 0
%10 = load i32, i32* %9
switch i32 %10, label %25 [
i32 129, label %11
i32 128, label %18
]
11: ; preds = %3
%12 = load %struct.sockaddr*, %struct.sockaddr** %5
%13 = bitcast %struct.sockaddr* %12 to %struct.sockaddr_in*
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i32 @inet_ntop(i32 129, i32* %14, i8* %15, i64 %16)
br label %29
18: ; preds = %3
%19 = load %struct.sockaddr*, %struct.sockaddr** %5
%20 = bitcast %struct.sockaddr* %19 to %struct.sockaddr_in6*
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %20, i32 0, i32 0
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = call i32 @inet_ntop(i32 128, i32* %21, i8* %22, i64 %23)
br label %29
25: ; preds = %3
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = call i32 @strncpy(i8* %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %27)
store i8* null, i8** %4
br label %31
29: ; preds = %18, %11
%30 = load i8*, i8** %6
store i8* %30, i8** %4
br label %31
31: ; preds = %29, %25
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"Unknown AF\00"
define i8* @get_ip_str(%struct.sockaddr* %0, i8* %1, i64 %2) {
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%5 = load i32, i32* %4
switch i32 %5, label %10 [
i32 129, label %6
i32 128, label %8
]
6: ; preds = %3
%7 = tail call i32 @inet_ntop(i32 129, i32* %4, i8* %1, i64 %2)
br label %12
8: ; preds = %3
%9 = tail call i32 @inet_ntop(i32 128, i32* %4, i8* %1, i64 %2)
br label %12
10: ; preds = %3
%11 = tail call i32 @strncpy(i8* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %2)
br label %12
12: ; preds = %6, %8, %10
%13 = phi i8* [ null, %10 ], [ %1, %8 ], [ %1, %6 ]
ret i8* %13
}