#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 *,
                             unsigned int);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
char *get_ip_str(const struct sockaddr *sa, char *s, unsigned int 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);
    return NULL;
  }
  return s;
}


x86 assembly (clang, -O0):
<get_ip_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
sub    $0x80,%eax
je     1179 <get_ip_str+0x59>
jmp    114c <get_ip_str+0x2c>
mov    -0x20(%rbp),%eax
sub    $0x81,%eax
jne    1193 <get_ip_str+0x73>
jmp    115f <get_ip_str+0x3f>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x81,%edi
call   1030 <inet_ntop@plt>
jmp    11b3 <get_ip_str+0x93>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x80,%edi
call   1030 <inet_ntop@plt>
jmp    11b3 <get_ip_str+0x93>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe5f(%rip),%rsi
call   1040 <strncpy@plt>
movq   $0x0,-0x8(%rbp)
jmp    11bb <get_ip_str+0x9b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_str>:
push   %rbx
mov    %edx,%ecx
mov    %rsi,%rbx
mov    %rdi,%rsi
mov    (%rdi),%eax
cmp    $0x80,%eax
je     1140 <get_ip_str+0x20>
cmp    $0x81,%eax
jne    114f <get_ip_str+0x2f>
mov    $0x81,%edi
jmp    1145 <get_ip_str+0x25>
mov    $0x80,%edi
mov    %rbx,%rdx
call   1030 <inet_ntop@plt>
jmp    1162 <get_ip_str+0x42>
lea    0xeaa(%rip),%rsi
mov    %rbx,%rdi
mov    %ecx,%edx
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, i32 %2) {
%4 = alloca i8*
%5 = alloca %struct.sockaddr*
%6 = alloca i8*
%7 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8* %1, i8** %6
store i32 %2, i32* %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 i32, i32* %7
%17 = call i32 @inet_ntop(i32 129, i32* %14, i8* %15, i32 %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 i32, i32* %7
%24 = call i32 @inet_ntop(i32 128, i32* %21, i8* %22, i32 %23)
br label %29
25: ; preds = %3
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = call i32 @strncpy(i8* %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %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, i32 %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, i32 %2)
br label %12
8: ; preds = %3
%9 = tail call i32 @inet_ntop(i32 128, i32* %4, i8* %1, i32 %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), i32 %2)
br label %12
12: ; preds = %6, %8, %10
%13 = phi i8* [ null, %10 ], [ %1, %8 ], [ %1, %6 ]
ret i8* %13
}