#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 */
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *very_ugly_ipv6_code(char *inip, int bitmask, int format_as_chainname) {
  int colon = 0, n, h;
  char *ip, *outip, *outptr, *fmt;
  if (inip) {
    (ip = (char *)malloc(strlen(inip) + 1), *ip = 0);
    strcpy(ip, inip);
  };
  if (ip && *ip && bitmask >= 0 && bitmask <= 64) {
    (outip = (char *)malloc(strlen(ip) + 10), *outip = 0);
  } else {
    return "undefined";
  }
  outptr = outip;
  while (ip && *ip) {
    if (*ip == ':') {
      if (colon < (bitmask / 16 - 1)) {
        if (format_as_chainname) {
          *outptr = '_';
        } else {
          *outptr = ':';
        }
        outptr++;
        colon++;
      } else {
        char *cutcolon = strchr(ip + 1, ':');
        if (cutcolon) {
          *cutcolon = '\0';
        }
        if (format_as_chainname) {
          fmt = "_%x_%d";
        } else {
          fmt = ":%x";
        }
        if (bitmask % 16) {
          sscanf(ip + 1, "%x", &h);
          n = h - h % (1 << (16 - bitmask % 16));
        } else {
          n = 0;
        }
        sprintf(outptr, fmt, n, bitmask);
        if (!format_as_chainname) {
          strcat(outip, "::");
        }
        return outip;
      }
    } else {
      *outptr = *ip;
      outptr++;
    }
    ip++;
  }
  *outptr = '\0';
  return outip;
}


x86 assembly (clang, -O0):
<very_ugly_ipv6_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c1 <very_ugly_ipv6_code+0x51>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
cmpq   $0x0,-0x30(%rbp)
je     1215 <very_ugly_ipv6_code+0xa5>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1215 <very_ugly_ipv6_code+0xa5>
cmpl   $0x0,-0x14(%rbp)
jl     1215 <very_ugly_ipv6_code+0xa5>
cmpl   $0x40,-0x14(%rbp)
jg     1215 <very_ugly_ipv6_code+0xa5>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0xa,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
jmp    1225 <very_ugly_ipv6_code+0xb5>
lea    0xde4(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13f6 <very_ugly_ipv6_code+0x286>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x51(%rbp)
je     124d <very_ugly_ipv6_code+0xdd>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x51(%rbp)
mov    -0x51(%rbp),%al
test   $0x1,%al
jne    125d <very_ugly_ipv6_code+0xed>
jmp    13e7 <very_ugly_ipv6_code+0x277>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    13be <very_ugly_ipv6_code+0x24e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12c5 <very_ugly_ipv6_code+0x155>
cmpl   $0x0,-0x18(%rbp)
je     12a4 <very_ugly_ipv6_code+0x134>
mov    -0x40(%rbp),%rax
movb   $0x5f,(%rax)
jmp    12ab <very_ugly_ipv6_code+0x13b>
mov    -0x40(%rbp),%rax
movb   $0x3a,(%rax)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    13b9 <very_ugly_ipv6_code+0x249>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12ed <very_ugly_ipv6_code+0x17d>
mov    -0x50(%rbp),%rax
movb   $0x0,(%rax)
cmpl   $0x0,-0x18(%rbp)
je     1307 <very_ugly_ipv6_code+0x197>
lea    0xd0c(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1312 <very_ugly_ipv6_code+0x1a2>
lea    0xd03(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1378 <very_ugly_ipv6_code+0x208>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
lea    0xcdd(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1070 <sscanf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    -0x60(%rbp),%eax
mov    $0x10,%ecx
sub    %edx,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
cltd
idiv   %ecx
mov    -0x5c(%rbp),%eax
sub    %edx,%eax
mov    %eax,-0x20(%rbp)
jmp    137f <very_ugly_ipv6_code+0x20f>
movl   $0x0,-0x20(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1090 <sprintf@plt>
cmpl   $0x0,-0x18(%rbp)
jne    13ac <very_ugly_ipv6_code+0x23c>
mov    -0x38(%rbp),%rdi
lea    0xc6e(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    13f6 <very_ugly_ipv6_code+0x286>
jmp    13d6 <very_ugly_ipv6_code+0x266>
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x40(%rbp),%rax
mov    %cl,(%rax)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    122d <very_ugly_ipv6_code+0xbd>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<very_ugly_ipv6_code>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xe56(%rip),%r15
cmp    $0x40,%r12d
ja     12c4 <very_ugly_ipv6_code+0x154>
cmpb   $0x0,0x0(%rbp)
je     12c4 <very_ugly_ipv6_code+0x154>
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0xa(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    %r12d,%eax
shr    $0x4,%eax
and    $0xf,%eax
add    $0xffffffff,%eax
test   %r14d,%r14d
mov    $0x3a,%ecx
mov    $0x5f,%esi
cmove  %ecx,%esi
add    $0x1,%rbp
xor    %edx,%edx
mov    %r15,%rbx
jmp    1212 <very_ugly_ipv6_code+0xa2>
nopl   0x0(%rax)
cmp    %eax,%edx
jge    122b <very_ugly_ipv6_code+0xbb>
mov    %sil,(%rbx)
add    $0x1,%edx
add    $0x1,%rbx
add    $0x1,%rbp
movzbl -0x1(%rbp),%ecx
cmp    $0x3a,%cl
je     1200 <very_ugly_ipv6_code+0x90>
test   %cl,%cl
je     1223 <very_ugly_ipv6_code+0xb3>
mov    %cl,(%rbx)
jmp    120a <very_ugly_ipv6_code+0x9a>
movb   $0x0,(%rbx)
jmp    12c4 <very_ugly_ipv6_code+0x154>
mov    %rbp,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1240 <very_ugly_ipv6_code+0xd0>
movb   $0x0,(%rax)
mov    %r14d,(%rsp)
test   %r14d,%r14d
lea    0xdc3(%rip),%rax
lea    0xdb5(%rip),%r13
cmove  %rax,%r13
lea    0xf(%r12),%eax
test   %r12d,%r12d
cmovns %r12d,%eax
and    $0xfffffff0,%eax
mov    %r12d,%r14d
sub    %eax,%r14d
je     129d <very_ugly_ipv6_code+0x12d>
lea    0xd9b(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbp,%rdi
call   1070 <sscanf@plt>
mov    $0x10,%cl
sub    %r14b,%cl
mov    $0x1,%esi
shl    %cl,%esi
mov    0x4(%rsp),%ecx
mov    %ecx,%eax
cltd
idiv   %esi
sub    %edx,%ecx
jmp    129f <very_ugly_ipv6_code+0x12f>
xor    %ecx,%ecx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %ecx,%edx
mov    %r12d,%ecx
call   1090 <sprintf@plt>
cmpl   $0x0,(%rsp)
jne    12c4 <very_ugly_ipv6_code+0x154>
lea    0xd59(%rip),%rsi
mov    %r15,%rdi
call   1080 <strcat@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"undefined\00"
@.str.1 = [7 x i8] c"_%x_%d\00"
@.str.2 = [4 x i8] c":%x\00"
@.str.3 = [3 x i8] c"%x\00"
@.str.4 = [3 x i8] c"::\00"
define i8* @very_ugly_ipv6_code(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %28
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %11
%24 = load i8*, i8** %11
store i8 0, i8* %24
%25 = load i8*, i8** %11
%26 = load i8*, i8** %5
%27 = call i32 @strcpy(i8* %25, i8* %26)
br label %28
28: ; preds = %18, %3
%29 = load i8*, i8** %11
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %49
31: ; preds = %28
%32 = load i8*, i8** %11
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %49
36: ; preds = %31
%37 = load i32, i32* %6
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %49
39: ; preds = %36
%40 = load i32, i32* %6
%41 = icmp sle i32 %40, 64
br i1 %41, label %42, label %49
42: ; preds = %39
%43 = load i8*, i8** %11
%44 = call i64 @strlen(i8* %43)
%45 = add nsw i64 %44, 10
%46 = call i64 @malloc(i64 %45)
%47 = inttoptr i64 %46 to i8*
store i8* %47, i8** %12
%48 = load i8*, i8** %12
store i8 0, i8* %48
br label %50
49: ; preds = %39, %36, %31, %28
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %4
br label %141
50: ; preds = %42
%51 = load i8*, i8** %12
store i8* %51, i8** %13
br label %52
52: ; preds = %135, %50
%53 = load i8*, i8** %11
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i8*, i8** %11
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %58, 0
br label %60
60: ; preds = %55, %52
%61 = phi i1 [ false, %52 ], [ %59, %55 ]
br i1 %61, label %62, label %138
62: ; preds = %60
%63 = load i8*, i8** %11
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 58
br i1 %66, label %67, label %129
67: ; preds = %62
%68 = load i32, i32* %8
%69 = load i32, i32* %6
%70 = sdiv i32 %69, 16
%71 = sub nsw i32 %70, 1
%72 = icmp slt i32 %68, %71
br i1 %72, label %73, label %85
73: ; preds = %67
%74 = load i32, i32* %7
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = load i8*, i8** %13
store i8 95, i8* %77
br label %80
78: ; preds = %73
%79 = load i8*, i8** %13
store i8 58, i8* %79
br label %80
80: ; preds = %78, %76
%81 = load i8*, i8** %13
%82 = getelementptr inbounds i8, i8* %81, i32 1
store i8* %82, i8** %13
%83 = load i32, i32* %8
%84 = add nsw i32 %83, 1
store i32 %84, i32* %8
br label %128
85: ; preds = %67
%86 = load i8*, i8** %11
%87 = getelementptr inbounds i8, i8* %86, i64 1
%88 = call i8* @strchr(i8* %87, i8 signext 58)
store i8* %88, i8** %15
%89 = load i8*, i8** %15
%90 = icmp ne i8* %89, null
br i1 %90, label %91, label %93
91: ; preds = %85
%92 = load i8*, i8** %15
store i8 0, i8* %92
br label %93
93: ; preds = %91, %85
%94 = load i32, i32* %7
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %97
96: ; preds = %93
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %14
br label %98
97: ; preds = %93
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %14
br label %98
98: ; preds = %97, %96
%99 = load i32, i32* %6
%100 = srem i32 %99, 16
%101 = icmp ne i32 %100, 0
br i1 %101, label %102, label %114
102: ; preds = %98
%103 = load i8*, i8** %11
%104 = getelementptr inbounds i8, i8* %103, i64 1
%105 = call i32 @sscanf(i8* %104, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %10)
%106 = load i32, i32* %10
%107 = load i32, i32* %10
%108 = load i32, i32* %6
%109 = srem i32 %108, 16
%110 = sub nsw i32 16, %109
%111 = shl i32 1, %110
%112 = srem i32 %107, %111
%113 = sub nsw i32 %106, %112
store i32 %113, i32* %9
br label %115
114: ; preds = %98
store i32 0, i32* %9
br label %115
115: ; preds = %114, %102
%116 = load i8*, i8** %13
%117 = load i8*, i8** %14
%118 = load i32, i32* %9
%119 = load i32, i32* %6
%120 = call i32 @sprintf(i8* %116, i8* %117, i32 %118, i32 %119)
%121 = load i32, i32* %7
%122 = icmp ne i32 %121, 0
br i1 %122, label %126, label %123
123: ; preds = %115
%124 = load i8*, i8** %12
%125 = call i32 @strcat(i8* %124, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %126
126: ; preds = %123, %115
%127 = load i8*, i8** %12
store i8* %127, i8** %4
br label %141
128: ; preds = %80
br label %135
129: ; preds = %62
%130 = load i8*, i8** %11
%131 = load i8, i8* %130
%132 = load i8*, i8** %13
store i8 %131, i8* %132
%133 = load i8*, i8** %13
%134 = getelementptr inbounds i8, i8* %133, i32 1
store i8* %134, i8** %13
br label %135
135: ; preds = %129, %128
%136 = load i8*, i8** %11
%137 = getelementptr inbounds i8, i8* %136, i32 1
store i8* %137, i8** %11
br label %52
138: ; preds = %60
%139 = load i8*, i8** %13
store i8 0, i8* %139
%140 = load i8*, i8** %12
store i8* %140, i8** %4
br label %141
141: ; preds = %138, %126, %49
%142 = load i8*, i8** %4
ret i8* %142
}

LLVM IR (-O2):
@.str = [10 x i8] c"undefined\00"
@.str.1 = [7 x i8] c"_%x_%d\00"
@.str.2 = [4 x i8] c":%x\00"
@.str.3 = [3 x i8] c"%x\00"
@.str.4 = [3 x i8] c"::\00"
define i8* @very_ugly_ipv6_code(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp ne i8* %0, null
tail call void @llvm.assume(i1 %6)
%7 = tail call i64 @strlen(i8* %0)
%8 = add nsw i64 %7, 1
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
store i8 0, i8* %10
%11 = tail call i32 @strcpy(i8* %10, i8* %0)
%12 = load i8, i8* %10
%13 = icmp ne i8 %12, 0
%14 = icmp ult i32 %1, 65
%15 = and i1 %14, %13
br i1 %15, label %16, label %62
16: ; preds = %3
%17 = tail call i64 @strlen(i8* %10)
%18 = add nsw i64 %17, 10
%19 = tail call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
store i8 0, i8* %20
%21 = lshr i32 %1, 4
%22 = and i32 %21, 15
%23 = add nsw i32 %22, -1
%24 = icmp eq i32 %2, 0
%25 = select i1 %24, i8 58, i8 95
br label %26
26: ; preds = %57, %16
%27 = phi i8* [ %10, %16 ], [ %60, %57 ]
%28 = phi i32 [ 0, %16 ], [ %58, %57 ]
%29 = phi i8* [ %20, %16 ], [ %59, %57 ]
%30 = load i8, i8* %27
switch i8 %30, label %56 [
i8 0, label %61
i8 58, label %31
]
31: ; preds = %26
%32 = icmp slt i32 %28, %23
br i1 %32, label %33, label %35
33: ; preds = %31
store i8 %25, i8* %29
%34 = add nsw i32 %28, 1
br label %57
35: ; preds = %31
%36 = getelementptr inbounds i8, i8* %27, i64 1
%37 = tail call i8* @strchr(i8* %36, i8 signext 58)
%38 = icmp eq i8* %37, null
br i1 %38, label %40, label %39
39: ; preds = %35
store i8 0, i8* %37
br label %40
40: ; preds = %39, %35
%41 = select i1 %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%42 = srem i32 %1, 16
%43 = icmp eq i32 %42, 0
br i1 %43, label %51, label %44
44: ; preds = %40
%45 = call i32 @sscanf(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%46 = load i32, i32* %4
%47 = sub nsw i32 16, %42
%48 = shl nuw i32 1, %47
%49 = srem i32 %46, %48
%50 = sub nsw i32 %46, %49
br label %51
51: ; preds = %40, %44
%52 = phi i32 [ %50, %44 ], [ 0, %40 ]
%53 = call i32 @sprintf(i8* dereferenceable(1) %29, i8* dereferenceable(1) %41, i32 %52, i32 %1)
br i1 %24, label %54, label %62
54: ; preds = %51
%55 = call i32 @strcat(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %62
56: ; preds = %26
store i8 %30, i8* %29
br label %57
57: ; preds = %56, %33
%58 = phi i32 [ %34, %33 ], [ %28, %56 ]
%59 = getelementptr inbounds i8, i8* %29, i64 1
%60 = getelementptr inbounds i8, i8* %27, i64 1
br label %26
61: ; preds = %26
store i8 0, i8* %29
br label %62
62: ; preds = %51, %54, %3, %61
%63 = phi i8* [ %20, %61 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %3 ], [ %20, %54 ], [ %20, %51 ]
ret i8* %63
}