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

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
unsigned long GetASNFromDNS(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, struct in6_addr *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncat(char *, char *, int);
unsigned long int GetASN6(const char *ip) {
  struct in6_addr saddr;
  char nibble_pair[5];
  char reverse_name[256 + 1];
  int i;
  memset(reverse_name, 0, 256 + 1);
  if (inet_pton(AF_INET6, ip, &saddr) <= 0) {
    fprintf(stderr, "Invalid IPv6 address %s\n", ip);
    return -1;
  }
  reverse_name[0] = 0;
  for (i = 7; i >= 0; i--) {
    if (i != 7)
      strncat(reverse_name, ".", 256);
    sprintf(nibble_pair, "%x.%x", saddr.s6_addr[i] & 0x0F,
            saddr.s6_addr[i] >> 4);
    strncat(reverse_name, nibble_pair, 256);
  }
  return GetASNFromDNS(reverse_name, "origin6.asn.cymru.com");
}


x86 assembly (clang, -O0):
<GetASN6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x101,%edx
call   1040 <memset@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1070 <inet_pton@plt>
cmp    $0x0,%rax
jg     11c8 <GetASN6+0x68>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4a(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    127d <GetASN6+0x11d>
movb   $0x0,-0x120(%rbp)
movl   $0x7,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jl     1266 <GetASN6+0x106>
cmpl   $0x7,-0x124(%rbp)
je     120b <GetASN6+0xab>
lea    -0x120(%rbp),%rdi
lea    0xe18(%rip),%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
lea    -0x1d(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x124(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
and    $0xf,%edx
mov    -0x18(%rbp),%rax
movslq -0x124(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
sar    $0x4,%ecx
lea    0xde3(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    -0x1d(%rbp),%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    -0x124(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x124(%rbp)
jmp    11d9 <GetASN6+0x79>
lea    -0x120(%rbp),%rdi
lea    0xdad(%rip),%rsi
call   1030 <GetASNFromDNS@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetASN6>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x101,%edx
call   1040 <memset@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rdx
mov    %rbx,%rsi
call   1070 <inet_pton@plt>
test   %rax,%rax
jle    138c <GetASN6+0x22c>
movb   $0x0,0x10(%rsp)
mov    (%rsp),%rax
mov    0x1c(%rax),%ecx
mov    %ecx,%edx
and    $0xf,%edx
sar    $0x4,%ecx
lea    0xe61(%rip),%r15
lea    0xb(%rsp),%r14
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
lea    0xe33(%rip),%r12
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    (%rsp),%rax
mov    0x18(%rax),%ecx
mov    %ecx,%edx
and    $0xf,%edx
sar    $0x4,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    (%rsp),%rax
mov    0x14(%rax),%ecx
mov    %ecx,%edx
and    $0xf,%edx
sar    $0x4,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    (%rsp),%rax
mov    0x10(%rax),%ecx
mov    %ecx,%edx
and    $0xf,%edx
sar    $0x4,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    (%rsp),%rax
mov    0xc(%rax),%ecx
mov    %ecx,%edx
and    $0xf,%edx
sar    $0x4,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    (%rsp),%rax
mov    0x8(%rax),%ecx
mov    %ecx,%edx
and    $0xf,%edx
sar    $0x4,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    (%rsp),%rax
mov    0x4(%rax),%ecx
mov    %ecx,%edx
and    $0xf,%edx
sar    $0x4,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
mov    (%rsp),%rax
mov    (%rax),%ecx
mov    %ecx,%edx
and    $0xf,%edx
sar    $0x4,%ecx
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <strncat@plt>
lea    0xc9f(%rip),%rsi
mov    %rbx,%rdi
call   1030 <GetASNFromDNS@plt>
jmp    13ab <GetASN6+0x24b>
mov    0x2c45(%rip),%rax
mov    (%rax),%edi
lea    0xc64(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffffffffffff,%rax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32* }
@AF_INET6 = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Invalid IPv6 address %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%x.%x\00"
@.str.3 = [22 x i8] c"origin6.asn.cymru.com\00"
define i64 @GetASN6(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.in6_addr
%5 = alloca [5 x i8]
%6 = alloca [257 x i8]
%7 = alloca i32
store i8* %0, i8** %3
%8 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 257)
%10 = load i32, i32* @AF_INET6
%11 = load i8*, i8** %3
%12 = call i64 @inet_pton(i32 %10, i8* %11, %struct.in6_addr* %4)
%13 = icmp sle i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %16)
store i64 -1, i64* %2
br label %55
18: ; preds = %1
%19 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
store i8 0, i8* %19
store i32 7, i32* %7
br label %20
20: ; preds = %49, %18
%21 = load i32, i32* %7
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %52
23: ; preds = %20
%24 = load i32, i32* %7
%25 = icmp ne i32 %24, 7
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%28 = call i32 @strncat(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 256)
br label %29
29: ; preds = %26, %23
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%31 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %4, i32 0, i32 0
%32 = load i32*, i32** %31
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = load i32, i32* %35
%37 = and i32 %36, 15
%38 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %4, i32 0, i32 0
%39 = load i32*, i32** %38
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = ashr i32 %43, 4
%45 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %44)
%46 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%47 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%48 = call i32 @strncat(i8* %46, i8* %47, i32 256)
br label %49
49: ; preds = %29
%50 = load i32, i32* %7
%51 = add nsw i32 %50, -1
store i32 %51, i32* %7
br label %20
52: ; preds = %20
%53 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%54 = call i64 @GetASNFromDNS(i8* %53, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i64 %54, i64* %2
br label %55
55: ; preds = %52, %14
%56 = load i64, i64* %2
ret i64 %56
}

LLVM IR (-O2):
%struct.in6_addr = type { i32* }
@AF_INET6 = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Invalid IPv6 address %s\0A\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [6 x i8] c"%x.%x\00"
@.str.3 = [22 x i8] c"origin6.asn.cymru.com\00"
define i64 @GetASN6(i8* %0) {
%2 = alloca %struct.in6_addr
%3 = alloca [5 x i8]
%4 = alloca [257 x i8]
%5 = bitcast %struct.in6_addr* %2 to i8*
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [257 x i8], [257 x i8]* %4, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 257)
%9 = load i32, i32* @AF_INET6
%10 = call i64 @inet_pton(i32 %9, i8* %0, %struct.in6_addr* %2)
%11 = icmp slt i64 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %80
15: ; preds = %1
store i8 0, i8* %7
%16 = getelementptr inbounds %struct.in6_addr, %struct.in6_addr* %2, i64 0, i32 0
%17 = load i32*, i32** %16
%18 = getelementptr inbounds i32, i32* %17, i64 7
%19 = load i32, i32* %18
%20 = and i32 %19, 15
%21 = ashr i32 %19, 4
%22 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
%23 = call i32 @strncat(i8* %7, i8* %6, i32 256)
%24 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 256)
%25 = load i32*, i32** %16
%26 = getelementptr inbounds i32, i32* %25, i64 6
%27 = load i32, i32* %26
%28 = and i32 %27, 15
%29 = ashr i32 %27, 4
%30 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %29)
%31 = call i32 @strncat(i8* %7, i8* %6, i32 256)
%32 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 256)
%33 = load i32*, i32** %16
%34 = getelementptr inbounds i32, i32* %33, i64 5
%35 = load i32, i32* %34
%36 = and i32 %35, 15
%37 = ashr i32 %35, 4
%38 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %37)
%39 = call i32 @strncat(i8* %7, i8* %6, i32 256)
%40 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 256)
%41 = load i32*, i32** %16
%42 = getelementptr inbounds i32, i32* %41, i64 4
%43 = load i32, i32* %42
%44 = and i32 %43, 15
%45 = ashr i32 %43, 4
%46 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %44, i32 %45)
%47 = call i32 @strncat(i8* %7, i8* %6, i32 256)
%48 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 256)
%49 = load i32*, i32** %16
%50 = getelementptr inbounds i32, i32* %49, i64 3
%51 = load i32, i32* %50
%52 = and i32 %51, 15
%53 = ashr i32 %51, 4
%54 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %52, i32 %53)
%55 = call i32 @strncat(i8* %7, i8* %6, i32 256)
%56 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 256)
%57 = load i32*, i32** %16
%58 = getelementptr inbounds i32, i32* %57, i64 2
%59 = load i32, i32* %58
%60 = and i32 %59, 15
%61 = ashr i32 %59, 4
%62 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %60, i32 %61)
%63 = call i32 @strncat(i8* %7, i8* %6, i32 256)
%64 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 256)
%65 = load i32*, i32** %16
%66 = getelementptr inbounds i32, i32* %65, i64 1
%67 = load i32, i32* %66
%68 = and i32 %67, 15
%69 = ashr i32 %67, 4
%70 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %68, i32 %69)
%71 = call i32 @strncat(i8* %7, i8* %6, i32 256)
%72 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 256)
%73 = load i32*, i32** %16
%74 = load i32, i32* %73
%75 = and i32 %74, 15
%76 = ashr i32 %74, 4
%77 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %75, i32 %76)
%78 = call i32 @strncat(i8* %7, i8* %6, i32 256)
%79 = call i64 @GetASNFromDNS(i8* %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %80
80: ; preds = %15, %12
%81 = phi i64 [ -1, %12 ], [ %79, %15 ]
ret i64 %81
}