#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_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  short sa_family;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
short const AF_INET;
short const AF_INET6;
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(short const, int /*<<< orphan*/ *, char *, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
const char *print_sockaddr(void *address, socklen_t addrlen) {
  static char buffer[512];
  const short family = ((struct sockaddr *)address)->sa_family;
  if (family == AF_INET && addrlen >= sizeof(struct sockaddr_in)) {
    struct sockaddr_in *address_ = address;
    snprintf(buffer, 512, "%s:%d", inet_ntoa(address_->sin_addr),
             ntohs(address_->sin_port));
  } else if (family == AF_INET6 && addrlen >= sizeof(struct sockaddr_in6)) {
    struct sockaddr_in6 *address_ = address;
    char buf[50];
    inet_ntop(AF_INET6, &address_->sin6_addr, buf, sizeof(buf));
    snprintf(buffer, 512, "[%s]:%d", buf, ntohs(address_->sin6_port));
  } else
    snprintf(buffer, 512, "<unknown destination, sa_family=%d>", family);
  return buffer;
}


x86 assembly (clang, -O0):
<print_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%ax
mov    %ax,-0xe(%rbp)
movswl -0xe(%rbp),%eax
mov    0x2e83(%rip),%rcx
movswl (%rcx),%ecx
cmp    %ecx,%eax
jne    11c7 <print_sockaddr+0x87>
movslq -0xc(%rbp),%rax
cmp    $0x8,%rax
jb     11c7 <print_sockaddr+0x87>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x68(%rbp),%rcx
mov    %eax,%r8d
lea    0x2ea1(%rip),%rdi
mov    $0x200,%esi
lea    0xe49(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1272 <print_sockaddr+0x132>
movswl -0xe(%rbp),%eax
mov    0x2e0e(%rip),%rcx
movswl (%rcx),%ecx
cmp    %ecx,%eax
jne    124f <print_sockaddr+0x10f>
movslq -0xc(%rbp),%rax
cmp    $0x8,%rax
jb     124f <print_sockaddr+0x10f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%ax
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
lea    -0x60(%rbp),%rdx
mov    $0x32,%ecx
movswl %ax,%edi
call   1030 <inet_ntop@plt>
lea    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1060 <ntohs@plt>
mov    -0x70(%rbp),%rcx
mov    %eax,%r8d
lea    0x2e19(%rip),%rdi
mov    $0x200,%esi
lea    0xdc7(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    126d <print_sockaddr+0x12d>
movswl -0xe(%rbp),%ecx
lea    0x2df6(%rip),%rdi
mov    $0x200,%esi
lea    0xdac(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    1272 <print_sockaddr+0x132>
lea    0x2dd7(%rip),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_sockaddr>:
push   %r14
push   %rbx
push   %rax
movzwl (%rdi),%eax
cmp    $0x8,%esi
jb     1178 <print_sockaddr+0x48>
test   %ax,%ax
jne    1178 <print_sockaddr+0x48>
mov    %rdi,%rbx
mov    0x4(%rdi),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r14
mov    (%rbx),%edi
call   1050 <ntohs@plt>
lea    0x2ef3(%rip),%rdi
lea    0xea0(%rip),%rdx
mov    $0x200,%esi
mov    %r14,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
jmp    1195 <print_sockaddr+0x65>
movswl %ax,%ecx
lea    0x2ece(%rip),%rdi
lea    0xe81(%rip),%rdx
mov    $0x200,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0x2eb4(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type { i16 }
@print_sockaddr.buffer = internal global [512 x i8] zeroinitializer
@AF_INET = constant i16 0
@.str = [6 x i8] c"%s:%d\00"
@AF_INET6 = constant i16 0
@.str.1 = [8 x i8] c"[%s]:%d\00"
@.str.2 = [36 x i8] c"<unknown destination, sa_family=%d>\00"
define i8* @print_sockaddr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i16
%6 = alloca %struct.sockaddr_in*
%7 = alloca %struct.sockaddr_in6*
%8 = alloca [50 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = bitcast i8* %9 to %struct.sockaddr*
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %10, i32 0, i32 0
%12 = load i16, i16* %11
store i16 %12, i16* %5
%13 = load i16, i16* %5
%14 = sext i16 %13 to i32
%15 = load i16, i16* @AF_INET
%16 = sext i16 %15 to i32
%17 = icmp eq i32 %14, %16
br i1 %17, label %18, label %34
18: ; preds = %2
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = icmp uge i64 %20, 8
br i1 %21, label %22, label %34
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = bitcast i8* %23 to %struct.sockaddr_in*
store %struct.sockaddr_in* %24, %struct.sockaddr_in** %6
%25 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i8* @inet_ntoa(i32 %27)
%29 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @ntohs(i32 %31)
%33 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @print_sockaddr.buffer, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %28, i32 %32)
br label %63
34: ; preds = %18, %2
%35 = load i16, i16* %5
%36 = sext i16 %35 to i32
%37 = load i16, i16* @AF_INET6
%38 = sext i16 %37 to i32
%39 = icmp eq i32 %36, %38
br i1 %39, label %40, label %58
40: ; preds = %34
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = icmp uge i64 %42, 8
br i1 %43, label %44, label %58
44: ; preds = %40
%45 = load i8*, i8** %3
%46 = bitcast i8* %45 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %46, %struct.sockaddr_in6** %7
%47 = load i16, i16* @AF_INET6
%48 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%49 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %48, i32 0, i32 1
%50 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%51 = call i32 @inet_ntop(i16 signext %47, i32* %49, i8* %50, i32 50)
%52 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%53 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %7
%54 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i32 @ntohs(i32 %55)
%57 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @print_sockaddr.buffer, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %52, i32 %56)
br label %62
58: ; preds = %40, %34
%59 = load i16, i16* %5
%60 = sext i16 %59 to i32
%61 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @print_sockaddr.buffer, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %58, %44
br label %63
63: ; preds = %62, %22
ret i8* getelementptr inbounds ([512 x i8], [512 x i8]* @print_sockaddr.buffer, i64 0, i64 0)
}

LLVM IR (-O2):
@print_sockaddr.buffer = internal global [512 x i8] zeroinitializer
@AF_INET = constant i16 0
@.str = [6 x i8] c"%s:%d\00"
@AF_INET6 = constant i16 0
@.str.2 = [36 x i8] c"<unknown destination, sa_family=%d>\00"
define i8* @print_sockaddr(i8* nocapture %0, i32 %1) {
%3 = bitcast i8* %0 to i16*
%4 = load i16, i16* %3
%5 = icmp eq i16 %4, 0
%6 = icmp ugt i32 %1, 7
%7 = and i1 %6, %5
br i1 %7, label %8, label %17
8: ; preds = %2
%9 = getelementptr inbounds i8, i8* %0, i64 4
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %10
%12 = tail call i8* @inet_ntoa(i32 %11)
%13 = bitcast i8* %0 to i32*
%14 = load i32, i32* %13
%15 = tail call i32 @ntohs(i32 %14)
%16 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @print_sockaddr.buffer, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %15)
br label %20
17: ; preds = %2
%18 = sext i16 %4 to i32
%19 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @print_sockaddr.buffer, i64 0, i64 0), i32 512, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %8
ret i8* getelementptr inbounds ([512 x i8], [512 x i8]* @print_sockaddr.buffer, i64 0, i64 0)
}