#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 in6_addr {
  scalar_t__ *s6_addr;
};

/* Variables and functions */
char *safe_strdup(char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
char *expand_ipv6(struct in6_addr *ip6) {
  char buf[128];
  char *p;
  unsigned i;
  p = buf;
  for (i = 0; i < 16; i++) {
    sprintf(p, "%.2x", (unsigned)ip6->s6_addr[i]);
    p += 2;
    if (i % 2 != 0 && i != 15) {
      *p = ':';
      p++;
    }
  }
  *p = 0;
  return safe_strdup(buf);
}


x86 assembly (clang, -O0):
<expand_ipv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x98(%rbp)
movl   $0x0,-0x9c(%rbp)
cmpl   $0x10,-0x9c(%rbp)
jae    11e0 <expand_ipv6+0xc0>
mov    -0x98(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x9c(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
mov    %eax,%edx
lea    0xe8b(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x98(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x98(%rbp)
mov    -0x9c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11c7 <expand_ipv6+0xa7>
cmpl   $0xf,-0x9c(%rbp)
je     11c7 <expand_ipv6+0xa7>
mov    -0x98(%rbp),%rax
movb   $0x3a,(%rax)
mov    -0x98(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x98(%rbp)
jmp    11cc <expand_ipv6+0xac>
mov    -0x9c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c(%rbp)
jmp    1147 <expand_ipv6+0x27>
mov    -0x98(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x90(%rbp),%rdi
call   1030 <safe_strdup@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_ipv6>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%r15
mov    %rsp,%rax
xor    %ebx,%ebx
lea    0xec3(%rip),%r14
jmp    114a <expand_ipv6+0x2a>
nop
add    $0x1,%rbx
cmp    $0x10,%rbx
je     1182 <expand_ipv6+0x62>
mov    %rax,%r12
mov    (%r15),%rax
mov    (%rax,%rbx,8),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <sprintf@plt>
lea    0x2(%r12),%rax
cmp    $0xf,%rbx
je     1140 <expand_ipv6+0x20>
mov    %ebx,%ecx
and    $0x1,%ecx
test   %rcx,%rcx
je     1140 <expand_ipv6+0x20>
movb   $0x3a,0x2(%r12)
add    $0x3,%r12
mov    %r12,%rax
jmp    1140 <expand_ipv6+0x20>
movb   $0x0,(%rax)
mov    %rsp,%rdi
call   1030 <safe_strdup@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.in6_addr = type { i64* }
@.str = [5 x i8] c"%.2x\00"
define i8* @expand_ipv6(%struct.in6_addr* %0) {
%2 = alloca %struct.in6_addr*
%3 = alloca [128 x i8]
%4 = alloca i8*
%5 = alloca i32
store %struct.in6_addr* %0, %struct.in6_addr** %2
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i8* %6, i8** %4
store i32 0, i32* %5
br label %7
7: ; preds = %34, %1
%8 = load i32, i32* %5
%9 = icmp ult i32 %8, 16
br i1 %9, label %10, label %37
10: ; preds = %7
%11 = load i8*, i8** %4
%12 = load %struct.in6_addr*, %struct.in6_addr** %2
%13 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %12, i32 0, i32 0
%14 = load i64*, i64** %13
%15 = load i32, i32* %5
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i64, i64* %14, i64 %16
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
%20 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 2
store i8* %22, i8** %4
%23 = load i32, i32* %5
%24 = urem i32 %23, 2
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %10
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, 15
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8*, i8** %4
store i8 58, i8* %30
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
br label %33
33: ; preds = %29, %26, %10
br label %34
34: ; preds = %33
%35 = load i32, i32* %5
%36 = add i32 %35, 1
store i32 %36, i32* %5
br label %7
37: ; preds = %7
%38 = load i8*, i8** %4
store i8 0, i8* %38
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%40 = call i8* @safe_strdup(i8* %39)
ret i8* %40
}

LLVM IR (-O2):
%struct.in6_addr = type { i64* }
@.str = [5 x i8] c"%.2x\00"
define i8* @expand_ipv6(%struct.in6_addr* nocapture %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %0, i64 0, i32 0
br label %5
5: ; preds = %1, %20
%6 = phi i64 [ 0, %1 ], [ %22, %20 ]
%7 = phi i8* [ %3, %1 ], [ %21, %20 ]
%8 = load i64*, i64** %4
%9 = getelementptr inbounds i64, i64* %8, i64 %6
%10 = load i64, i64* %9
%11 = trunc i64 %10 to i32
%12 = call i32 @sprintf(i8* dereferenceable(1) %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds i8, i8* %7, i64 2
%14 = and i64 %6, 1
%15 = icmp ne i64 %14, 0
%16 = icmp ne i64 %6, 15
%17 = and i1 %16, %15
br i1 %17, label %18, label %20
18: ; preds = %5
store i8 58, i8* %13
%19 = getelementptr inbounds i8, i8* %7, i64 3
br label %20
20: ; preds = %5, %18
%21 = phi i8* [ %19, %18 ], [ %13, %5 ]
%22 = add nuw nsw i64 %6, 1
%23 = icmp eq i64 %22, 16
br i1 %23, label %24, label %5
24: ; preds = %20
store i8 0, i8* %21
%25 = call i8* @safe_strdup(i8* %3)
ret i8* %25
}