#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 */

/* Variables and functions */
int format_ip6_address(unsigned char const *, int, int, char *, int);
int /*<<< orphan*/ longest_zero_pairs(unsigned char const *, int *, int *);
int snprintf(char *, int, char *, int const, int const, int const, int const);
int format_ipaddress(char *buf, int len, const unsigned char address[16]) {
  int zero_start;
  int zero_length;
  int used;
  longest_zero_pairs(address, &zero_start, &zero_length);
  if (zero_start == 0 && zero_length == 12) {
    const int a1 = address[12 + 0];
    const int a2 = address[12 + 1];
    const int a3 = address[12 + 2];
    const int a4 = address[12 + 3];
    used = snprintf(buf, len, "::%d.%d.%d.%d", a1, a2, a3, a4);
  } else {
    used = format_ip6_address(address, zero_start, zero_length, buf, len);
  }
  return used;
}


x86 assembly (clang, -O0):
<format_ipaddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <longest_zero_pairs@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11c0 <format_ipaddress+0x90>
cmpl   $0xc,-0x20(%rbp)
jne    11c0 <format_ipaddress+0x90>
mov    -0x18(%rbp),%rax
movzbl 0xc(%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movzbl 0xd(%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
movzbl 0xe(%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
movzbl 0xf(%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x28(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x30(%rbp),%r9d
mov    -0x34(%rbp),%eax
lea    0xe50(%rip),%rdx
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
jmp    11da <format_ipaddress+0xaa>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
call   1050 <format_ip6_address@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_ipaddress>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rbp,%rdi
call   1030 <longest_zero_pairs@plt>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
test   %esi,%esi
jne    118d <format_ipaddress+0x5d>
cmp    $0xc,%edx
jne    118d <format_ipaddress+0x5d>
movzbl 0xc(%rbp),%ecx
movzbl 0xd(%rbp),%r8d
movzbl 0xe(%rbp),%r9d
movzbl 0xf(%rbp),%eax
mov    %eax,(%rsp)
lea    0xe80(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <snprintf@plt>
jmp    119b <format_ipaddress+0x6b>
mov    %rbp,%rdi
mov    %rbx,%rcx
mov    %r14d,%r8d
call   1050 <format_ip6_address@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"::%d.%d.%d.%d\00"
define i32 @format_ipaddress(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%14 = load i8*, i8** %6
%15 = call i32 @longest_zero_pairs(i8* %14, i32* %7, i32* %8)
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %45
18: ; preds = %3
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 12
br i1 %20, label %21, label %45
21: ; preds = %18
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 12
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
store i32 %25, i32* %10
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i64 13
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
store i32 %29, i32* %11
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i64 14
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
store i32 %33, i32* %12
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i64 15
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
store i32 %37, i32* %13
%38 = load i8*, i8** %4
%39 = load i32, i32* %5
%40 = load i32, i32* %10
%41 = load i32, i32* %11
%42 = load i32, i32* %12
%43 = load i32, i32* %13
%44 = call i32 @snprintf(i8* %38, i32 %39, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %40, i32 %41, i32 %42, i32 %43)
store i32 %44, i32* %9
br label %52
45: ; preds = %18, %3
%46 = load i8*, i8** %6
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = load i8*, i8** %4
%50 = load i32, i32* %5
%51 = call i32 @format_ip6_address(i8* %46, i32 %47, i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %9
br label %52
52: ; preds = %45, %21
%53 = load i32, i32* %9
ret i32 %53
}

LLVM IR (-O2):
@.str = [14 x i8] c"::%d.%d.%d.%d\00"
define i32 @format_ipaddress(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i32 @longest_zero_pairs(i8* %2, i32* %4, i32* %5)
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 12
%13 = select i1 %10, i1 %12, i1 false
br i1 %13, label %14, label %28
14: ; preds = %3
%15 = getelementptr inbounds i8, i8* %2, i64 12
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = getelementptr inbounds i8, i8* %2, i64 13
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = getelementptr inbounds i8, i8* %2, i64 14
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = getelementptr inbounds i8, i8* %2, i64 15
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %20, i32 %23, i32 %26)
br label %30
28: ; preds = %3
%29 = call i32 @format_ip6_address(i8* %2, i32 %9, i32 %11, i8* %0, i32 %1)
br label %30
30: ; preds = %28, %14
%31 = phi i32 [ %27, %14 ], [ %29, %28 ]
ret i32 %31
}