#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__ IN6_IS_ADDR_LINKLOCAL(struct in6_addr *);
scalar_t__ IN6_IS_ADDR_MULTICAST(struct in6_addr *);
scalar_t__ IN6_IS_ADDR_UNSPECIFIED(struct in6_addr *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, struct in6_addr *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
unsigned int strtoul(char *, char **, int);
int valid_ipv6(char *str) {
  unsigned int prefix_len;
  struct in6_addr addr;
  char *slash, *endp;
  slash = strchr(str, '/');
  if (!slash) {
    fprintf(stderr, "Missing network prefix\n");
    return 0;
  }
  *slash++ = 0;
  prefix_len = strtoul(slash, &endp, 10);
  if (*slash == '\0' || *endp != '\0')
    fprintf(stderr, "Non-digit in prefix length\n");
  else if (prefix_len <= 1 || prefix_len > 128)
    fprintf(stderr, "Invalid prefix len %d for IPv6\n", prefix_len);
  else if (inet_pton(AF_INET6, str, &addr) <= 0)
    fprintf(stderr, "Invalid IPv6 address\n");
  else if (IN6_IS_ADDR_LINKLOCAL(&addr))
    fprintf(stderr, "Can not assign an address reserved for IPv6 link local\n");
  else if (IN6_IS_ADDR_MULTICAST(&addr))
    fprintf(stderr, "Can not assign an address reserved for IPv6 multicast\n");
  else if (IN6_IS_ADDR_UNSPECIFIED(&addr))
    fprintf(stderr,
            "Can not assign IPv6 reserved for IPv6 unspecified address\n");
  else
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<valid_ipv6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bc <valid_ipv6+0x4c>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134a <valid_ipv6+0x1da>
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   1080 <strtoul@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1203 <valid_ipv6+0x93>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     121f <valid_ipv6+0xaf>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1343 <valid_ipv6+0x1d3>
cmpl   $0x1,-0x14(%rbp)
jbe    1236 <valid_ipv6+0xc6>
cmpl   $0x80,-0x14(%rbp)
jbe    1255 <valid_ipv6+0xe5>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    133e <valid_ipv6+0x1ce>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x0,%rax
jg     1291 <valid_ipv6+0x121>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1339 <valid_ipv6+0x1c9>
lea    -0x18(%rbp),%rdi
call   1070 <IN6_IS_ADDR_LINKLOCAL@plt>
cmp    $0x0,%rax
je     12c0 <valid_ipv6+0x150>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1334 <valid_ipv6+0x1c4>
lea    -0x18(%rbp),%rdi
call   1060 <IN6_IS_ADDR_MULTICAST@plt>
cmp    $0x0,%rax
je     12ef <valid_ipv6+0x17f>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    132f <valid_ipv6+0x1bf>
lea    -0x18(%rbp),%rdi
call   1090 <IN6_IS_ADDR_UNSPECIFIED@plt>
cmp    $0x0,%rax
je     131e <valid_ipv6+0x1ae>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    132a <valid_ipv6+0x1ba>
movl   $0x1,-0x4(%rbp)
jmp    134a <valid_ipv6+0x1da>
jmp    132f <valid_ipv6+0x1bf>
jmp    1334 <valid_ipv6+0x1c4>
jmp    1339 <valid_ipv6+0x1c9>
jmp    133e <valid_ipv6+0x1ce>
jmp    1343 <valid_ipv6+0x1d3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_ipv6>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    $0x2f,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11c7 <valid_ipv6+0x57>
mov    %rax,%rbx
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   1080 <strtoul@plt>
cmpb   $0x0,0x1(%rbx)
je     11b5 <valid_ipv6+0x45>
mov    0x10(%rsp),%rcx
cmpb   $0x0,(%rcx)
je     11ea <valid_ipv6+0x7a>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
jmp    11d7 <valid_ipv6+0x67>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    -0x81(%rax),%ecx
cmp    $0xffffff80,%ecx
ja     1212 <valid_ipv6+0xa2>
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%edi
lea    0xe2f(%rip),%rsi
xor    %ebx,%ebx
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11e0 <valid_ipv6+0x70>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rdx
mov    %r14,%rsi
call   1050 <inet_pton@plt>
test   %rax,%rax
jle    124e <valid_ipv6+0xde>
lea    0x8(%rsp),%rdi
call   1070 <IN6_IS_ADDR_LINKLOCAL@plt>
test   %rax,%rax
je     1263 <valid_ipv6+0xf3>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
jmp    11d7 <valid_ipv6+0x67>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
jmp    11d7 <valid_ipv6+0x67>
lea    0x8(%rsp),%rdi
call   1060 <IN6_IS_ADDR_MULTICAST@plt>
test   %rax,%rax
je     1287 <valid_ipv6+0x117>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
jmp    11d7 <valid_ipv6+0x67>
lea    0x8(%rsp),%rdi
call   1090 <IN6_IS_ADDR_UNSPECIFIED@plt>
test   %rax,%rax
je     12ab <valid_ipv6+0x13b>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    0xe33(%rip),%rsi
jmp    11d7 <valid_ipv6+0x67>
mov    $0x1,%ebx
jmp    11e0 <valid_ipv6+0x70>

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Missing network prefix\0A\00"
@.str.1 = [28 x i8] c"Non-digit in prefix length\0A\00"
@.str.2 = [32 x i8] c"Invalid prefix len %d for IPv6\0A\00"
@AF_INET6 = global i32 0
@.str.3 = [22 x i8] c"Invalid IPv6 address\0A\00"
@.str.4 = [56 x i8] c"Can not assign an address reserved for IPv6 link local\0A\00"
@.str.5 = [55 x i8] c"Can not assign an address reserved for IPv6 multicast\0A\00"
@.str.6 = [59 x i8] c"Can not assign IPv6 reserved for IPv6 unspecified address\0A\00"
define i32 @valid_ipv6(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 = call i8* @strchr(i8* %8, i8 signext 47)
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %75
15: ; preds = %1
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %6
store i8 0, i8* %16
%18 = load i8*, i8** %6
%19 = call i32 @strtoul(i8* %18, i8** %7, i32 10)
store i32 %19, i32* %4
%20 = load i8*, i8** %6
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %15
%25 = load i8*, i8** %7
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24, %15
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %74
32: ; preds = %24
%33 = load i32, i32* %4
%34 = icmp ule i32 %33, 1
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = load i32, i32* %4
%37 = icmp ugt i32 %36, 128
br i1 %37, label %38, label %42
38: ; preds = %35, %32
%39 = load i32, i32* @stderr
%40 = load i32, i32* %4
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %40)
br label %73
42: ; preds = %35
%43 = load i32, i32* @AF_INET6
%44 = load i8*, i8** %3
%45 = call i64 @inet_pton(i32 %43, i8* %44, %struct.in6_addr* %5)
%46 = icmp sle i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %72
50: ; preds = %42
%51 = call i64 @IN6_IS_ADDR_LINKLOCAL(%struct.in6_addr* %5)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %71
56: ; preds = %50
%57 = call i64 @IN6_IS_ADDR_MULTICAST(%struct.in6_addr* %5)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0))
br label %70
62: ; preds = %56
%63 = call i64 @IN6_IS_ADDR_UNSPECIFIED(%struct.in6_addr* %5)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i32, i32* @stderr
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.6, i64 0, i64 0))
br label %69
68: ; preds = %62
store i32 1, i32* %2
br label %75
69: ; preds = %65
br label %70
70: ; preds = %69, %59
br label %71
71: ; preds = %70, %53
br label %72
72: ; preds = %71, %47
br label %73
73: ; preds = %72, %38
br label %74
74: ; preds = %73, %29
store i32 0, i32* %2
br label %75
75: ; preds = %74, %68, %12
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Missing network prefix\0A\00"
@.str.1 = [28 x i8] c"Non-digit in prefix length\0A\00"
@.str.2 = [32 x i8] c"Invalid prefix len %d for IPv6\0A\00"
@AF_INET6 = global i32 0
@.str.3 = [22 x i8] c"Invalid IPv6 address\0A\00"
@.str.4 = [56 x i8] c"Can not assign an address reserved for IPv6 link local\0A\00"
@.str.5 = [55 x i8] c"Can not assign an address reserved for IPv6 multicast\0A\00"
@.str.6 = [59 x i8] c"Can not assign IPv6 reserved for IPv6 unspecified address\0A\00"
define i32 @valid_ipv6(i8* %0) {
%2 = alloca %struct.in6_addr
%3 = alloca i8*
%4 = bitcast %struct.in6_addr* %2 to i8*
%5 = bitcast i8** %3 to i8*
%6 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %54
11: ; preds = %1
%12 = getelementptr inbounds i8, i8* %6, i64 1
store i8 0, i8* %6
%13 = call i32 @strtoul(i8* %12, i8** %3, i32 10)
%14 = load i8, i8* %12
%15 = icmp eq i8 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16, %11
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %54
23: ; preds = %16
%24 = add i32 %13, -129
%25 = icmp ult i32 %24, -127
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %54
29: ; preds = %23
%30 = load i32, i32* @AF_INET6
%31 = call i64 @inet_pton(i32 %30, i8* %0, %struct.in6_addr* %2)
%32 = icmp slt i64 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %54
36: ; preds = %29
%37 = call i64 @IN6_IS_ADDR_LINKLOCAL(%struct.in6_addr* %2)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %54
42: ; preds = %36
%43 = call i64 @IN6_IS_ADDR_MULTICAST(%struct.in6_addr* %2)
%44 = icmp eq i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0))
br label %54
48: ; preds = %42
%49 = call i64 @IN6_IS_ADDR_UNSPECIFIED(%struct.in6_addr* %2)
%50 = icmp eq i64 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.6, i64 0, i64 0))
br label %54
54: ; preds = %20, %33, %45, %51, %39, %26, %48, %8
%55 = phi i32 [ 0, %8 ], [ 1, %48 ], [ 0, %26 ], [ 0, %39 ], [ 0, %51 ], [ 0, %45 ], [ 0, %33 ], [ 0, %20 ]
ret i32 %55
}