#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 */
int /*<<< orphan*/ memset(char *, char, int);
scalar_t__ re_match(char const *, char *);
int sscanf(char const *, char *, char *);
char *strchr(char const *, char);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char const *, char);
int validate_ipv6(char const *);
int validate_ipv6net(char const *);
int validate_ipv6_addr_param(const char *str) {
  if (!str)
    return 0;
  char value[87];
  char ipv6_1[43];
  char ipv6_2[43];
  memset(ipv6_1, '\0', 43);
  memset(ipv6_2, '\0', 43);
  if (sscanf(str, "!%87s", value) == 1)
    str = value;
  if (re_match(str, "^[^-]+-[^-]+$")) {
    char *dash = strrchr(str, '-');
    strncpy(ipv6_1, str, dash - str);
    strncpy(ipv6_2, dash + 1, strchr(str, '\0') - dash + 1);
    if (validate_ipv6(ipv6_1))
      return validate_ipv6(ipv6_2);
    else
      return 0;
  }
  if (strchr(str, '/') != NULL)
    return validate_ipv6net(str);
  else
    return validate_ipv6(str);
}


x86 assembly (clang, -O0):
<validate_ipv6_addr_param>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <validate_ipv6_addr_param+0x26>
movl   $0x0,-0x4(%rbp)
jmp    12f8 <validate_ipv6_addr_param+0x178>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x2b,%edx
call   1090 <memset@plt>
lea    -0xd0(%rbp),%rdi
xor    %esi,%esi
mov    $0x2b,%edx
call   1090 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x70(%rbp),%rdx
lea    0xe25(%rip),%rsi
call   10a0 <sscanf@plt>
cmp    $0x1,%eax
jne    11f1 <validate_ipv6_addr_param+0x71>
lea    -0x70(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1030 <re_match@plt>
cmp    $0x0,%rax
je     12c3 <validate_ipv6_addr_param+0x143>
mov    -0x10(%rbp),%rdi
mov    $0x2d,%esi
call   1070 <strrchr@plt>
mov    %rax,-0xd8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0xd8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
lea    -0xd0(%rbp),%rax
mov    %rax,-0xe8(%rbp)
mov    -0xd8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xe0(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <strchr@plt>
mov    -0xe8(%rbp),%rdi
mov    -0xe0(%rbp),%rsi
mov    -0xd8(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
lea    -0xa0(%rbp),%rdi
call   1050 <validate_ipv6@plt>
cmp    $0x0,%eax
je     12b7 <validate_ipv6_addr_param+0x137>
lea    -0xd0(%rbp),%rdi
call   1050 <validate_ipv6@plt>
mov    %eax,-0x4(%rbp)
jmp    12f8 <validate_ipv6_addr_param+0x178>
movl   $0x0,-0x4(%rbp)
jmp    12f8 <validate_ipv6_addr_param+0x178>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     12ec <validate_ipv6_addr_param+0x16c>
mov    -0x10(%rbp),%rdi
call   1080 <validate_ipv6net@plt>
mov    %eax,-0x4(%rbp)
jmp    12f8 <validate_ipv6_addr_param+0x178>
mov    -0x10(%rbp),%rdi
call   1050 <validate_ipv6@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_ipv6_addr_param>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xc0,%rsp
test   %rdi,%rdi
je     1259 <validate_ipv6_addr_param+0xc9>
mov    %rdi,%rbx
lea    0x30(%rsp),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x2b,%edx
call   10a0 <memset@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x2b,%edx
call   10a0 <memset@plt>
lea    0xe2b(%rip),%rsi
lea    0x60(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rdx
call   10b0 <sscanf@plt>
cmp    $0x1,%eax
cmove  %rbp,%rbx
lea    0xe13(%rip),%rsi
mov    %rbx,%rdi
call   1030 <re_match@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     125e <validate_ipv6_addr_param+0xce>
mov    $0x2d,%esi
call   1080 <strrchr@plt>
mov    %rax,%r12
mov    %r12d,%edx
sub    %ebx,%edx
lea    0x30(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <strncpy@plt>
mov    %r12,%rbp
add    $0x1,%rbp
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbx,1),%edx
sub    %r12d,%edx
add    $0x1,%edx
mov    %rsp,%rdi
mov    %rbp,%rsi
call   1040 <strncpy@plt>
mov    %r15,%rdi
call   1050 <validate_ipv6@plt>
test   %eax,%eax
je     127f <validate_ipv6_addr_param+0xef>
mov    %rsp,%rdi
jmp    1277 <validate_ipv6_addr_param+0xe7>
xor    %r14d,%r14d
jmp    127f <validate_ipv6_addr_param+0xef>
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     1277 <validate_ipv6_addr_param+0xe7>
call   1090 <validate_ipv6net@plt>
jmp    127c <validate_ipv6_addr_param+0xec>
call   1050 <validate_ipv6@plt>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0xc0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"!%87s\00"
@.str.1 = [14 x i8] c"^[^-]+-[^-]+$\00"
define i32 @validate_ipv6_addr_param(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [87 x i8]
%5 = alloca [43 x i8]
%6 = alloca [43 x i8]
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 0, i32* %2
br label %67
11: ; preds = %1
%12 = getelementptr inbounds [43 x i8], [43 x i8]* %5, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i8 signext 0, i32 43)
%14 = getelementptr inbounds [43 x i8], [43 x i8]* %6, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i8 signext 0, i32 43)
%16 = load i8*, i8** %3
%17 = getelementptr inbounds [87 x i8], [87 x i8]* %4, i64 0, i64 0
%18 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = getelementptr inbounds [87 x i8], [87 x i8]* %4, i64 0, i64 0
store i8* %21, i8** %3
br label %22
22: ; preds = %20, %11
%23 = load i8*, i8** %3
%24 = call i64 @re_match(i8* %23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %57
26: ; preds = %22
%27 = load i8*, i8** %3
%28 = call i8* @strrchr(i8* %27, i8 signext 45)
store i8* %28, i8** %7
%29 = getelementptr inbounds [43 x i8], [43 x i8]* %5, i64 0, i64 0
%30 = load i8*, i8** %3
%31 = load i8*, i8** %7
%32 = load i8*, i8** %3
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = trunc i64 %35 to i32
%37 = call i32 @strncpy(i8* %29, i8* %30, i32 %36)
%38 = getelementptr inbounds [43 x i8], [43 x i8]* %6, i64 0, i64 0
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8*, i8** %3
%42 = call i8* @strchr(i8* %41, i8 signext 0)
%43 = load i8*, i8** %7
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
%47 = add nsw i64 %46, 1
%48 = trunc i64 %47 to i32
%49 = call i32 @strncpy(i8* %38, i8* %40, i32 %48)
%50 = getelementptr inbounds [43 x i8], [43 x i8]* %5, i64 0, i64 0
%51 = call i32 @validate_ipv6(i8* %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %26
%54 = getelementptr inbounds [43 x i8], [43 x i8]* %6, i64 0, i64 0
%55 = call i32 @validate_ipv6(i8* %54)
store i32 %55, i32* %2
br label %67
56: ; preds = %26
store i32 0, i32* %2
br label %67
57: ; preds = %22
%58 = load i8*, i8** %3
%59 = call i8* @strchr(i8* %58, i8 signext 47)
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = load i8*, i8** %3
%63 = call i32 @validate_ipv6net(i8* %62)
store i32 %63, i32* %2
br label %67
64: ; preds = %57
%65 = load i8*, i8** %3
%66 = call i32 @validate_ipv6(i8* %65)
store i32 %66, i32* %2
br label %67
67: ; preds = %64, %61, %56, %53, %10
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
@.str = [6 x i8] c"!%87s\00"
@.str.1 = [14 x i8] c"^[^-]+-[^-]+$\00"
define i32 @validate_ipv6_addr_param(i8* %0) {
%2 = alloca [87 x i8]
%3 = alloca [43 x i8]
%4 = alloca [43 x i8]
%5 = icmp eq i8* %0, null
br i1 %5, label %45, label %6
6: ; preds = %1
%7 = getelementptr inbounds [87 x i8], [87 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [43 x i8], [43 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [43 x i8], [43 x i8]* %4, i64 0, i64 0
%10 = call i32 @memset(i8* %8, i8 signext 0, i32 43)
%11 = call i32 @memset(i8* %9, i8 signext 0, i32 43)
%12 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %7)
%13 = icmp eq i32 %12, 1
%14 = select i1 %13, i8* %7, i8* %0
%15 = call i64 @re_match(i8* %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %36, label %17
17: ; preds = %6
%18 = call i8* @strrchr(i8* dereferenceable(1) %14, i8 signext 45)
%19 = ptrtoint i8* %18 to i64
%20 = ptrtoint i8* %14 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = call i32 @strncpy(i8* %8, i8* %14, i32 %22)
%24 = getelementptr inbounds i8, i8* %18, i64 1
%25 = call i64 @strlen(i8* dereferenceable(1) %14)
%26 = getelementptr i8, i8* %14, i64 %25
%27 = ptrtoint i8* %26 to i64
%28 = sub i64 %27, %19
%29 = trunc i64 %28 to i32
%30 = add i32 %29, 1
%31 = call i32 @strncpy(i8* %9, i8* %24, i32 %30)
%32 = call i32 @validate_ipv6(i8* %8)
%33 = icmp eq i32 %32, 0
br i1 %33, label %43, label %34
34: ; preds = %17
%35 = call i32 @validate_ipv6(i8* %9)
br label %43
36: ; preds = %6
%37 = call i8* @strchr(i8* dereferenceable(1) %14, i8 signext 47)
%38 = icmp eq i8* %37, null
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @validate_ipv6net(i8* %14)
br label %43
41: ; preds = %36
%42 = call i32 @validate_ipv6(i8* %14)
br label %43
43: ; preds = %34, %17, %41, %39
%44 = phi i32 [ %40, %39 ], [ %42, %41 ], [ %35, %34 ], [ 0, %17 ]
br label %45
45: ; preds = %1, %43
%46 = phi i32 [ %44, %43 ], [ 0, %1 ]
ret i32 %46
}