#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 */
typedef char in6_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
scalar_t__ IN6_IS_ADDR_V4MAPPED(char const *);
int /*<<< orphan*/ errno;
scalar_t__ inet_ntop(int /*<<< orphan*/, char const *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strerror(int /*<<< orphan*/);
char *format_address(const struct in6_addr *a, char *buf, int buflen) {
  char *ptr;
  if (IN6_IS_ADDR_V4MAPPED(a))
    ptr = (char *)inet_ntop(AF_INET, (char *)a + 12, buf, buflen);
  else
    ptr = (char *)inet_ntop(AF_INET6, a, buf, buflen);
  if (ptr == NULL)
    snprintf(buf, buflen, "%s", strerror(errno));
  buf[buflen - 1] = '\0';
  return ptr;
}


x86 assembly (clang, -O0):
<format_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <IN6_IS_ADDR_V4MAPPED@plt>
cmp    $0x0,%rax
je     118c <format_address+0x4c>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
add    $0xc,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x20(%rbp)
jmp    11a9 <format_address+0x69>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e6 <format_address+0xa6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    %rax,%rcx
lea    0xe1f(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_address>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <IN6_IS_ADDR_V4MAPPED@plt>
test   %rax,%rax
je     1165 <format_address+0x25>
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
add    $0xc,%rbx
jmp    116e <format_address+0x2e>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a5 <format_address+0x65>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe68(%rip),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%r14,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.in6_addr = type opaque
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@.str = [3 x i8] c"%s\00"
@errno = global i32 0
define i8* @format_address(%struct.in6_addr* %0, i8* %1, i32 %2) {
%4 = alloca %struct.in6_addr*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store %struct.in6_addr* %0, %struct.in6_addr** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load %struct.in6_addr*, %struct.in6_addr** %4
%9 = bitcast %struct.in6_addr* %8 to i8*
%10 = call i64 @IN6_IS_ADDR_V4MAPPED(i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %3
%13 = load i32, i32* @AF_INET
%14 = load %struct.in6_addr*, %struct.in6_addr** %4
%15 = bitcast %struct.in6_addr* %14 to i8*
%16 = getelementptr inbounds i8, i8* %15, i64 12
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = call i64 @inet_ntop(i32 %13, i8* %16, i8* %17, i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %7
br label %29
21: ; preds = %3
%22 = load i32, i32* @AF_INET6
%23 = load %struct.in6_addr*, %struct.in6_addr** %4
%24 = bitcast %struct.in6_addr* %23 to i8*
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = call i64 @inet_ntop(i32 %22, i8* %24, i8* %25, i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %7
br label %29
29: ; preds = %21, %12
%30 = load i8*, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @snprintf(i8* %33, i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %32, %29
%39 = load i8*, i8** %5
%40 = load i32, i32* %6
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %7
ret i8* %44
}

LLVM IR (-O2):
%struct.in6_addr = type opaque
@AF_INET = global i32 0
@AF_INET6 = global i32 0
@.str = [3 x i8] c"%s\00"
@errno = global i32 0
define i8* @format_address(%struct.in6_addr* %0, i8* %1, i32 %2) {
%4 = bitcast %struct.in6_addr* %0 to i8*
%5 = tail call i64 @IN6_IS_ADDR_V4MAPPED(i8* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds i8, i8* %4, i64 12
%10 = tail call i64 @inet_ntop(i32 %8, i8* %9, i8* %1, i32 %2)
br label %14
11: ; preds = %3
%12 = load i32, i32* @AF_INET6
%13 = tail call i64 @inet_ntop(i32 %12, i8* %4, i8* %1, i32 %2)
br label %14
14: ; preds = %11, %7
%15 = phi i64 [ %10, %7 ], [ %13, %11 ]
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %17, %14
%22 = inttoptr i64 %15 to i8*
%23 = add nsw i32 %2, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 0, i8* %25
ret i8* %22
}