#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*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ inet_ntop(int const, int /*<<< orphan*/ *, char *,
                             int /*<<< orphan*/);
void get_ip_str(const struct sockaddr *sa, char *addr) {
  switch (sa->sa_family) {
  case AF_INET:
    inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), addr,
              INET6_ADDRSTRLEN);
    break;
  case AF_INET6:
    inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), addr,
              INET6_ADDRSTRLEN);
    break;
  }
}


x86 assembly (clang, -O0):
<get_ip_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
sub    $0x80,%eax
je     116c <get_ip_str+0x5c>
jmp    1139 <get_ip_str+0x29>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
jne    1187 <get_ip_str+0x77>
jmp    114c <get_ip_str+0x3c>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2e8d(%rip),%rax
mov    (%rax),%ecx
mov    $0x81,%edi
call   1030 <inet_ntop@plt>
jmp    1187 <get_ip_str+0x77>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    $0x80,%edi
call   1030 <inet_ntop@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_str>:
mov    %rsi,%rdx
mov    %rdi,%rsi
mov    (%rdi),%edi
cmp    $0x81,%edi
je     112d <get_ip_str+0x1d>
cmp    $0x80,%edi
jne    113b <get_ip_str+0x2b>
mov    $0x80,%edi
mov    0x2eb4(%rip),%rax
mov    (%rax),%ecx
jmp    1030 <inet_ntop@plt>
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
define void @get_ip_str(%struct.sockaddr* %0, i8* %1) {
%3 = alloca %struct.sockaddr*
%4 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %3
store i8* %1, i8** %4
%5 = load %struct.sockaddr*, %struct.sockaddr** %3
%6 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %5, i32 0, i32 0
%7 = load i32, i32* %6
switch i32 %7, label %22 [
i32 129, label %8
i32 128, label %15
]
8: ; preds = %2
%9 = load %struct.sockaddr*, %struct.sockaddr** %3
%10 = bitcast %struct.sockaddr* %9 to %struct.sockaddr_in*
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%12 = load i8*, i8** %4
%13 = load i32, i32* @INET6_ADDRSTRLEN
%14 = call i32 @inet_ntop(i32 129, i32* %11, i8* %12, i32 %13)
br label %22
15: ; preds = %2
%16 = load %struct.sockaddr*, %struct.sockaddr** %3
%17 = bitcast %struct.sockaddr* %16 to %struct.sockaddr_in6*
%18 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %17, i32 0, i32 0
%19 = load i8*, i8** %4
%20 = load i32, i32* @INET6_ADDRSTRLEN
%21 = call i32 @inet_ntop(i32 128, i32* %18, i8* %19, i32 %20)
br label %22
22: ; preds = %2, %15, %8
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
define void @get_ip_str(%struct.sockaddr* %0, i8* %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i32, i32* %3
switch i32 %4, label %10 [
i32 129, label %6
i32 128, label %5
]
5: ; preds = %2
br label %6
6: ; preds = %2, %5
%7 = phi i32 [ 128, %5 ], [ %4, %2 ]
%8 = load i32, i32* @INET6_ADDRSTRLEN
%9 = tail call i32 @inet_ntop(i32 %7, i32* %3, i8* %1, i32 %8)
br label %10
10: ; preds = %6, %2
ret void
}