#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);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen) {
  switch (sa->sa_family) {
  case AF_INET:
    inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen);
    break;
  case AF_INET6:
    inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), s, maxlen);
    break;
  default:
    strncpy(s, "Unknown AF", maxlen);
    break;
  }
  return s;
}


x86 assembly (clang, -O0):
<get_ip_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     117b <get_ip_str+0x5b>
jmp    114d <get_ip_str+0x2d>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
jne    1196 <get_ip_str+0x76>
jmp    1160 <get_ip_str+0x40>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x81,%edi
call   1030 <inet_ntop@plt>
jmp    11aa <get_ip_str+0x8a>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x80,%edi
call   1030 <inet_ntop@plt>
jmp    11aa <get_ip_str+0x8a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe5b(%rip),%rsi
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%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    1162 <get_ip_str+0x42>
lea    0xea9(%rip),%rsi
mov    %rbx,%rdi
mov    %rcx,%rdx
call   1040 <strncpy@plt>
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 %struct.sockaddr*
%5 = alloca i8*
%6 = alloca i64
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%7 = load %struct.sockaddr*, %struct.sockaddr** %4
%8 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %7, i32 0, i32 0
%9 = load i32, i32* %8
switch i32 %9, label %24 [
i32 129, label %10
i32 128, label %17
]
10: ; preds = %3
%11 = load %struct.sockaddr*, %struct.sockaddr** %4
%12 = bitcast %struct.sockaddr* %11 to %struct.sockaddr_in*
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%14 = load i8*, i8** %5
%15 = load i64, i64* %6
%16 = call i32 @inet_ntop(i32 129, i32* %13, i8* %14, i64 %15)
br label %28
17: ; preds = %3
%18 = load %struct.sockaddr*, %struct.sockaddr** %4
%19 = bitcast %struct.sockaddr* %18 to %struct.sockaddr_in6*
%20 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %19, i32 0, i32 0
%21 = load i8*, i8** %5
%22 = load i64, i64* %6
%23 = call i32 @inet_ntop(i32 128, i32* %20, i8* %21, i64 %22)
br label %28
24: ; preds = %3
%25 = load i8*, i8** %5
%26 = load i64, i64* %6
%27 = call i32 @strncpy(i8* %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64 %26)
br label %28
28: ; preds = %24, %17, %10
%29 = load i8*, i8** %5
ret i8* %29
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"Unknown AF\00"
define i8* @get_ip_str(%struct.sockaddr* %0, i8* returned %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 = %10, %8, %6
ret i8* %1
}