#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 in_addr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
unsigned long GetASNFromDNS(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, struct in_addr *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char, unsigned char,
                            unsigned char, unsigned char);
int /*<<< orphan*/ stderr;
unsigned long int GetASN4(const char *ip) {
  struct in_addr saddr;
  unsigned char addr[4];
  char reverse_name[256 + 1];
  int i;
  memset(reverse_name, 0, 256 + 1);
  if (inet_pton(AF_INET, ip, &saddr) <= 0) {
    fprintf(stderr, "Invalid IPv4 address %s\n", ip);
    return -1;
  }
  for (i = 0; i < 4; i++)
    addr[i] = ((unsigned char *)&saddr)[3 - i];
  snprintf(reverse_name, 256, "%d.%d.%d.%d", addr[0], addr[1], addr[2],
           addr[3]);
  return GetASNFromDNS(reverse_name, "origin.asn.cymru.com");
}


x86 assembly (clang, -O0):
<GetASN4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x101,%edx
call   1050 <memset@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1070 <inet_pton@plt>
cmp    $0x0,%rax
jg     11b8 <GetASN4+0x68>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5a(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    124a <GetASN4+0xfa>
movl   $0x0,-0x124(%rbp)
cmpl   $0x4,-0x124(%rbp)
jge    11ff <GetASN4+0xaf>
mov    $0x3,%eax
sub    -0x124(%rbp),%eax
cltq
mov    -0x18(%rbp,%rax,1),%cl
movslq -0x124(%rbp),%rax
mov    %cl,-0x1c(%rbp,%rax,1)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    11c2 <GetASN4+0x72>
lea    -0x120(%rbp),%rdi
mov    -0x1c(%rbp),%cl
mov    -0x1b(%rbp),%r8b
mov    -0x1a(%rbp),%al
mov    $0x100,%esi
lea    0xdfd(%rip),%rdx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %al,%r9d
movzbl -0x19(%rbp),%eax
mov    %eax,(%rsp)
call   1040 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1030 <GetASNFromDNS@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetASN4>:
push   %rbx
sub    $0x120,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x101,%edx
call   1050 <memset@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1070 <inet_pton@plt>
test   %rax,%rax
jle    11ca <GetASN4+0x7a>
movzbl 0xb(%rsp),%ecx
movzbl 0xa(%rsp),%r8d
movzbl 0x9(%rsp),%r9d
movzbl 0x8(%rsp),%eax
mov    %eax,(%rsp)
lea    0xe72(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1030 <GetASNFromDNS@plt>
jmp    11e9 <GetASN4+0x99>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffffffffffff,%rax
add    $0x120,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Invalid IPv4 address %s\0A\00"
@.str.1 = [12 x i8] c"%d.%d.%d.%d\00"
@.str.2 = [21 x i8] c"origin.asn.cymru.com\00"
define i64 @GetASN4(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.in_addr
%5 = alloca [4 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_INET
%11 = load i8*, i8** %3
%12 = call i64 @inet_pton(i32 %10, i8* %11, %struct.in_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 %48
18: ; preds = %1
store i32 0, i32* %7
br label %19
19: ; preds = %32, %18
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 4
br i1 %21, label %22, label %35
22: ; preds = %19
%23 = bitcast %struct.in_addr* %4 to i8*
%24 = load i32, i32* %7
%25 = sub nsw i32 3, %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i8, i8* %27
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %30
store i8 %28, i8* %31
br label %32
32: ; preds = %22
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %19
35: ; preds = %19
%36 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%38 = load i8, i8* %37
%39 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
%40 = load i8, i8* %39
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
%42 = load i8, i8* %41
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
%44 = load i8, i8* %43
%45 = call i32 @snprintf(i8* %36, i32 256, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %38, i8 zeroext %40, i8 zeroext %42, i8 zeroext %44)
%46 = getelementptr inbounds [257 x i8], [257 x i8]* %6, i64 0, i64 0
%47 = call i64 @GetASNFromDNS(i8* %46, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i64 %47, i64* %2
br label %48
48: ; preds = %35, %14
%49 = load i64, i64* %2
ret i64 %49
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"Invalid IPv4 address %s\0A\00"
@.str.1 = [12 x i8] c"%d.%d.%d.%d\00"
@.str.2 = [21 x i8] c"origin.asn.cymru.com\00"
define i64 @GetASN4(i8* %0) {
%2 = alloca %struct.in_addr
%3 = alloca [257 x i8]
%4 = bitcast %struct.in_addr* %2 to i8*
%5 = getelementptr inbounds [257 x i8], [257 x i8]* %3, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 257)
%7 = load i32, i32* @AF_INET
%8 = call i64 @inet_pton(i32 %7, i8* %0, %struct.in_addr* %2)
%9 = icmp slt i64 %8, 1
br i1 %9, label %20, label %10
10: ; preds = %1
%11 = getelementptr inbounds i8, i8* %4, i64 3
%12 = load i8, i8* %11
%13 = getelementptr inbounds i8, i8* %4, i64 2
%14 = load i8, i8* %13
%15 = getelementptr inbounds i8, i8* %4, i64 1
%16 = load i8, i8* %15
%17 = load i8, i8* %4
%18 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %12, i8 zeroext %14, i8 zeroext %16, i8 zeroext %17)
%19 = call i64 @GetASNFromDNS(i8* %5, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %23
20: ; preds = %1
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %10, %20
%24 = phi i64 [ -1, %20 ], [ %19, %10 ]
ret i64 %24
}