#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 dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
char const *pretty_sockaddr_ipv4(struct sockaddr_in *, char *);
char const *pretty_sockaddr_ipv6(struct sockaddr_in6 *, char *);
int /*<<< orphan*/ sprintf(char *, char *, scalar_t__);
const char *pretty_sockaddr(struct sockaddr *sa, char *buf) {
  if (sa->sa_family == AF_INET) {
    return pretty_sockaddr_ipv4((struct sockaddr_in *)sa, buf);
  } else if (sa->sa_family == AF_INET6) {
    return pretty_sockaddr_ipv6((struct sockaddr_in6 *)sa, buf);
  } else {
    sprintf(buf, "<UNKNOWN-AF-%d>", sa->sa_family);
    return 0;
  }
}


x86 assembly (clang, -O0):
<pretty_sockaddr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e9a(%rip),%rcx
cmp    (%rcx),%rax
jne    116d <pretty_sockaddr+0x3d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <pretty_sockaddr_ipv4@plt>
mov    %rax,-0x8(%rbp)
jmp    11b9 <pretty_sockaddr+0x89>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e65(%rip),%rcx
cmp    (%rcx),%rax
jne    119a <pretty_sockaddr+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <pretty_sockaddr_ipv6@plt>
mov    %rax,-0x8(%rbp)
jmp    11b9 <pretty_sockaddr+0x89>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe54(%rip),%rsi
call   1050 <sprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pretty_sockaddr>:
push   %rax
mov    (%rdi),%rdx
mov    0x2ead(%rip),%rax
cmp    (%rax),%rdx
jne    1146 <pretty_sockaddr+0x16>
pop    %rax
jmp    1040 <pretty_sockaddr_ipv4@plt>
mov    0x2e93(%rip),%rax
cmp    (%rax),%rdx
jne    1158 <pretty_sockaddr+0x28>
pop    %rax
jmp    1030 <pretty_sockaddr_ipv6@plt>
lea    0xea1(%rip),%rax
mov    %rsi,%rdi
mov    %rax,%rsi
call   1050 <sprintf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [16 x i8] c"<UNKNOWN-AF-%d>\00"
define i8* @pretty_sockaddr(%struct.sockaddr* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.sockaddr*
%5 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i8* %1, i8** %5
%6 = load %struct.sockaddr*, %struct.sockaddr** %4
%7 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @AF_INET
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load %struct.sockaddr*, %struct.sockaddr** %4
%13 = bitcast %struct.sockaddr* %12 to %struct.sockaddr_in*
%14 = load i8*, i8** %5
%15 = call i8* @pretty_sockaddr_ipv4(%struct.sockaddr_in* %13, i8* %14)
store i8* %15, i8** %3
br label %33
16: ; preds = %2
%17 = load %struct.sockaddr*, %struct.sockaddr** %4
%18 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @AF_INET6
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load %struct.sockaddr*, %struct.sockaddr** %4
%24 = bitcast %struct.sockaddr* %23 to %struct.sockaddr_in6*
%25 = load i8*, i8** %5
%26 = call i8* @pretty_sockaddr_ipv6(%struct.sockaddr_in6* %24, i8* %25)
store i8* %26, i8** %3
br label %33
27: ; preds = %16
%28 = load i8*, i8** %5
%29 = load %struct.sockaddr*, %struct.sockaddr** %4
%30 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %31)
store i8* null, i8** %3
br label %33
33: ; preds = %27, %22, %11
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [16 x i8] c"<UNKNOWN-AF-%d>\00"
define i8* @pretty_sockaddr(%struct.sockaddr* %0, i8* %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = load i64, i64* @AF_INET
%6 = icmp eq i64 %4, %5
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = bitcast %struct.sockaddr* %0 to %struct.sockaddr_in*
%9 = tail call i8* @pretty_sockaddr_ipv4(%struct.sockaddr_in* %8, i8* %1)
br label %18
10: ; preds = %2
%11 = load i64, i64* @AF_INET6
%12 = icmp eq i64 %4, %11
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = bitcast %struct.sockaddr* %0 to %struct.sockaddr_in6*
%15 = tail call i8* @pretty_sockaddr_ipv6(%struct.sockaddr_in6* %14, i8* %1)
br label %18
16: ; preds = %10
%17 = tail call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %4)
br label %18
18: ; preds = %16, %13, %7
%19 = phi i8* [ %9, %7 ], [ %15, %13 ], [ null, %16 ]
ret i8* %19
}