#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 */
typedef int uint64_t;
typedef int uint32_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int PF_INET;
int PF_INET6;
int lookup_host(char const *, int *, int *);
int ntohl(int);
int parse_ipv4(char const *, int *, int *);
int parse_ipv6(char const *, int *, int *);
int /*<<< orphan*/ *strchr(char const *, char);
scalar_t__ strpbrk(char const *, char *);
int parse_ip(int *af, const char *src, uint64_t *dst, int *bytes, int lookup,
             uint32_t *num_ip) {
  char *alpha = "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY";
  uint32_t v4addr;
  int ret;
  if (strchr(src, ':') != NULL) {
    *af = PF_INET6;
  } else if (strpbrk(src, alpha)) {
    *af = 0;
    if (lookup == 0)
      return -1;
    else
      return lookup_host(src, dst, num_ip);
  } else
    *af = PF_INET;
  *num_ip = 1;
  switch (*af) {
  case AF_INET:
    ret = (parse_ipv4(src, &v4addr, bytes));
    dst[0] = 0;
    dst[1] = ntohl(v4addr) & 0xffffffffLL;
    return ret;
    break;
  case AF_INET6:
    ret = (parse_ipv6(src, dst, bytes));
    dst[0] = (((uint64_t)ntohl(dst[0])) << 32) + ntohl((dst[0]) >> 32);
    dst[1] = (((uint64_t)ntohl(dst[1])) << 32) + ntohl((dst[1]) >> 32);
    return ret;
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<parse_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9,-0x38(%rbp)
lea    0xe79(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     11b7 <parse_ip+0x57>
mov    0x2e3e(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    121b <parse_ip+0xbb>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <strpbrk@plt>
cmp    $0x0,%rax
je     1207 <parse_ip+0xa7>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x0,-0x2c(%rbp)
jne    11ee <parse_ip+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <parse_ip+0x1c7>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <lookup_host@plt>
mov    %eax,-0x4(%rbp)
jmp    1327 <parse_ip+0x1c7>
mov    0x2dca(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    121b <parse_ip+0xbb>
mov    -0x38(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x80,%eax
je     129a <parse_ip+0x13a>
jmp    123e <parse_ip+0xde>
mov    -0x4c(%rbp),%eax
sub    $0x81,%eax
jne    1320 <parse_ip+0x1c0>
jmp    1251 <parse_ip+0xf1>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    -0x44(%rbp),%rsi
call   1030 <parse_ipv4@plt>
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x44(%rbp),%edi
call   1080 <ntohl@plt>
cltq
movabs $0xffffffff,%rcx
and    %rcx,%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1327 <parse_ip+0x1c7>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <parse_ipv6@plt>
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1080 <ntohl@plt>
mov    $0x20,%ecx
shl    %cl,%eax
mov    %eax,-0x54(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    $0x20,%ecx
sar    %cl,%edi
call   1080 <ntohl@plt>
mov    -0x54(%rbp),%ecx
add    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1080 <ntohl@plt>
mov    $0x20,%ecx
shl    %cl,%eax
mov    %eax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
mov    $0x20,%ecx
sar    %cl,%edi
call   1080 <ntohl@plt>
mov    -0x50(%rbp),%ecx
add    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1327 <parse_ip+0x1c7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %r8d,%ebp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbx
mov    %rsi,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11e0 <parse_ip+0x80>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
movl   $0x1,(%r12)
mov    (%rbx),%eax
cmp    $0x80,%eax
je     121a <parse_ip+0xba>
xor    %ebx,%ebx
cmp    $0x81,%eax
jne    1253 <parse_ip+0xf3>
lea    0x4(%rsp),%rsi
mov    %r13,%rdi
mov    %r14,%rdx
call   1030 <parse_ipv4@plt>
mov    %eax,%ebx
movl   $0x0,(%r15)
mov    0x4(%rsp),%edi
call   1080 <ntohl@plt>
mov    %eax,0x4(%r15)
jmp    1253 <parse_ip+0xf3>
lea    0xe19(%rip),%rsi
mov    %r13,%rdi
call   1070 <strpbrk@plt>
test   %rax,%rax
je     1242 <parse_ip+0xe2>
movl   $0x0,(%rbx)
test   %ebp,%ebp
je     124e <parse_ip+0xee>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <lookup_host@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <parse_ipv6@plt>
mov    %eax,%ebx
mov    (%r15),%edi
call   1080 <ntohl@plt>
call   1080 <ntohl@plt>
mov    0x4(%r15),%edi
call   1080 <ntohl@plt>
jmp    1253 <parse_ip+0xf3>
mov    0x2d8f(%rip),%rax
jmp    1196 <parse_ip+0x36>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [53 x i8] c"abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY\00"
@PF_INET6 = global i32 0
@PF_INET = global i32 0
define i32 @parse_ip(i32* %0, i8* %1, i32* %2, i32* %3, i32 %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
store i32* %0, i32** %8
store i8* %1, i8** %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i32 %4, i32* %12
store i32* %5, i32** %13
store i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i8** %14
%17 = load i8*, i8** %9
%18 = call i32* @strchr(i8* %17, i8 signext 58)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %6
%21 = load i32, i32* @PF_INET6
%22 = load i32*, i32** %8
store i32 %21, i32* %22
br label %42
23: ; preds = %6
%24 = load i8*, i8** %9
%25 = load i8*, i8** %14
%26 = call i64 @strpbrk(i8* %24, i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %23
%29 = load i32*, i32** %8
store i32 0, i32* %29
%30 = load i32, i32* %12
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %7
br label %93
33: ; preds = %28
%34 = load i8*, i8** %9
%35 = load i32*, i32** %10
%36 = load i32*, i32** %13
%37 = call i32 @lookup_host(i8* %34, i32* %35, i32* %36)
store i32 %37, i32* %7
br label %93
38: ; preds = %23
%39 = load i32, i32* @PF_INET
%40 = load i32*, i32** %8
store i32 %39, i32* %40
br label %41
41: ; preds = %38
br label %42
42: ; preds = %41, %20
%43 = load i32*, i32** %13
store i32 1, i32* %43
%44 = load i32*, i32** %8
%45 = load i32, i32* %44
switch i32 %45, label %92 [
i32 129, label %46
i32 128, label %60
]
46: ; preds = %42
%47 = load i8*, i8** %9
%48 = load i32*, i32** %11
%49 = call i32 @parse_ipv4(i8* %47, i32* %15, i32* %48)
store i32 %49, i32* %16
%50 = load i32*, i32** %10
%51 = getelementptr inbounds i32, i32* %50, i64 0
store i32 0, i32* %51
%52 = load i32, i32* %15
%53 = call i32 @ntohl(i32 %52)
%54 = sext i32 %53 to i64
%55 = and i64 %54, 4294967295
%56 = trunc i64 %55 to i32
%57 = load i32*, i32** %10
%58 = getelementptr inbounds i32, i32* %57, i64 1
store i32 %56, i32* %58
%59 = load i32, i32* %16
store i32 %59, i32* %7
br label %93
60: ; preds = %42
%61 = load i8*, i8** %9
%62 = load i32*, i32** %10
%63 = load i32*, i32** %11
%64 = call i32 @parse_ipv6(i8* %61, i32* %62, i32* %63)
store i32 %64, i32* %16
%65 = load i32*, i32** %10
%66 = getelementptr inbounds i32, i32* %65, i64 0
%67 = load i32, i32* %66
%68 = call i32 @ntohl(i32 %67)
%69 = shl i32 %68, 32
%70 = load i32*, i32** %10
%71 = getelementptr inbounds i32, i32* %70, i64 0
%72 = load i32, i32* %71
%73 = ashr i32 %72, 32
%74 = call i32 @ntohl(i32 %73)
%75 = add nsw i32 %69, %74
%76 = load i32*, i32** %10
%77 = getelementptr inbounds i32, i32* %76, i64 0
store i32 %75, i32* %77
%78 = load i32*, i32** %10
%79 = getelementptr inbounds i32, i32* %78, i64 1
%80 = load i32, i32* %79
%81 = call i32 @ntohl(i32 %80)
%82 = shl i32 %81, 32
%83 = load i32*, i32** %10
%84 = getelementptr inbounds i32, i32* %83, i64 1
%85 = load i32, i32* %84
%86 = ashr i32 %85, 32
%87 = call i32 @ntohl(i32 %86)
%88 = add nsw i32 %82, %87
%89 = load i32*, i32** %10
%90 = getelementptr inbounds i32, i32* %89, i64 1
store i32 %88, i32* %90
%91 = load i32, i32* %16
store i32 %91, i32* %7
br label %93
92: ; preds = %42
store i32 0, i32* %7
br label %93
93: ; preds = %92, %60, %46, %33, %32
%94 = load i32, i32* %7
ret i32 %94
}

LLVM IR (-O2):
@.str = [53 x i8] c"abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY\00"
@PF_INET6 = global i32 0
@PF_INET = global i32 0
define i32 @parse_ip(i32* nocapture %0, i8* %1, i32* %2, i32* %3, i32 %4, i32* %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
%9 = tail call i32* @strchr(i8* %1, i8 signext 58)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %18
11: ; preds = %6
%12 = tail call i64 @strpbrk(i8* %1, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %11
store i32 0, i32* %0
%15 = icmp eq i32 %4, 0
br i1 %15, label %35, label %16
16: ; preds = %14
%17 = tail call i32 @lookup_host(i8* %1, i32* %2, i32* %5)
br label %35
18: ; preds = %11, %6
%19 = phi i32* [ @PF_INET6, %6 ], [ @PF_INET, %11 ]
%20 = load i32, i32* %19
store i32 %20, i32* %0
store i32 1, i32* %5
%21 = load i32, i32* %0
switch i32 %21, label %35 [
i32 129, label %22
i32 128, label %27
]
22: ; preds = %18
%23 = call i32 @parse_ipv4(i8* %1, i32* %7, i32* %3)
store i32 0, i32* %2
%24 = load i32, i32* %7
%25 = call i32 @ntohl(i32 %24)
%26 = getelementptr inbounds i32, i32* %2, i64 1
store i32 %25, i32* %26
br label %35
27: ; preds = %18
%28 = tail call i32 @parse_ipv6(i8* %1, i32* %2, i32* %3)
%29 = load i32, i32* %2
%30 = tail call i32 @ntohl(i32 %29)
%31 = tail call i32 @ntohl(i32 poison)
%32 = getelementptr inbounds i32, i32* %2, i64 1
%33 = load i32, i32* %32
%34 = tail call i32 @ntohl(i32 %33)
br label %35
35: ; preds = %18, %14, %27, %22, %16
%36 = phi i32 [ %28, %27 ], [ %23, %22 ], [ %17, %16 ], [ -1, %14 ], [ 0, %18 ]
ret i32 %36
}