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

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, struct in6_addr *);
char *strchr(char const *, char);
unsigned int strtoul(char *, char **, int);
int validate_ipv6net(const char *str) {
  if (!str)
    return 0;
  unsigned int prefix_len;
  struct in6_addr addr;
  char *slash, *endp;
  slash = strchr(str, '/');
  if (!slash)
    return 0;
  *slash++ = 0;
  prefix_len = strtoul(slash, &endp, 10);
  if (*slash == '\0' || *endp != '\0')
    return 0;
  else if (prefix_len > 128)
    return 0;
  else if (inet_pton(AF_INET6, str, &addr) <= 0)
    return 0;
  else
    return 1;
}


x86 assembly (clang, -O0):
<validate_ipv6net>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1153 <validate_ipv6net+0x23>
movl   $0x0,-0x4(%rbp)
jmp    121b <validate_ipv6net+0xeb>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117c <validate_ipv6net+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    121b <validate_ipv6net+0xeb>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c3 <validate_ipv6net+0x93>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11cf <validate_ipv6net+0x9f>
movl   $0x0,-0x4(%rbp)
jmp    121b <validate_ipv6net+0xeb>
cmpl   $0x80,-0x14(%rbp)
jbe    11e8 <validate_ipv6net+0xb8>
movl   $0x0,-0x4(%rbp)
jmp    121b <validate_ipv6net+0xeb>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <inet_pton@plt>
cmp    $0x0,%rax
jg     1214 <validate_ipv6net+0xe4>
movl   $0x0,-0x4(%rbp)
jmp    121b <validate_ipv6net+0xeb>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<validate_ipv6net>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     11a4 <validate_ipv6net+0x74>
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1030 <strchr@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11a7 <validate_ipv6net+0x77>
mov    %rax,%rbp
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
mov    %rsp,%rsi
mov    $0xa,%edx
call   1050 <strtoul@plt>
cmpb   $0x0,0x1(%rbp)
je     11a7 <validate_ipv6net+0x77>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    11a7 <validate_ipv6net+0x77>
cmp    $0x80,%eax
ja     11a7 <validate_ipv6net+0x77>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <inet_pton@plt>
xor    %r14d,%r14d
test   %rax,%rax
setg   %r14b
jmp    11a7 <validate_ipv6net+0x77>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
@AF_INET6 = global i32 0
define i32 @validate_ipv6net(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.in6_addr
%6 = alloca 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 %43
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i8* @strchr(i8* %12, i8 signext 47)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %11
store i32 0, i32* %2
br label %43
17: ; preds = %11
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %6
store i8 0, i8* %18
%20 = load i8*, i8** %6
%21 = call i32 @strtoul(i8* %20, i8** %7, i32 10)
store i32 %21, i32* %4
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %17
%27 = load i8*, i8** %7
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26, %17
store i32 0, i32* %2
br label %43
32: ; preds = %26
%33 = load i32, i32* %4
%34 = icmp ugt i32 %33, 128
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 0, i32* %2
br label %43
36: ; preds = %32
%37 = load i32, i32* @AF_INET6
%38 = load i8*, i8** %3
%39 = call i64 @inet_pton(i32 %37, i8* %38, %struct.in6_addr* %5)
%40 = icmp sle i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 0, i32* %2
br label %43
42: ; preds = %36
store i32 1, i32* %2
br label %43
43: ; preds = %42, %41, %35, %31, %16, %10
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
@AF_INET6 = global i32 0
define i32 @validate_ipv6net(i8* %0) {
%2 = alloca %struct.in6_addr
%3 = alloca i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %28, label %5
5: ; preds = %1
%6 = bitcast %struct.in6_addr* %2 to i8*
%7 = bitcast i8** %3 to i8*
%8 = tail call i8* @strchr(i8* %0, i8 signext 47)
%9 = icmp eq i8* %8, null
br i1 %9, label %26, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %8, i64 1
store i8 0, i8* %8
%12 = call i32 @strtoul(i8* %11, i8** %3, i32 10)
%13 = load i8, i8* %11
%14 = icmp eq i8 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
%19 = icmp ugt i32 %12, 128
%20 = select i1 %18, i1 true, i1 %19
br i1 %20, label %26, label %21
21: ; preds = %15
%22 = load i32, i32* @AF_INET6
%23 = call i64 @inet_pton(i32 %22, i8* %0, %struct.in6_addr* %2)
%24 = icmp sgt i64 %23, 0
%25 = zext i1 %24 to i32
br label %26
26: ; preds = %21, %10, %15, %5
%27 = phi i32 [ 0, %5 ], [ 0, %15 ], [ 0, %10 ], [ %25, %21 ]
br label %28
28: ; preds = %1, %26
%29 = phi i32 [ %27, %26 ], [ 0, %1 ]
ret i32 %29
}