#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;
};
struct sockaddr_in6 {
  void *sin6_family;
  struct in_addr sin6_addr;
};
struct sockaddr_in {
  void *sin_family;
  struct in_addr sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef struct in_addr in6_addr;

/* Variables and functions */
void *AF_INET;
void *AF_INET6;
int INET6_ADDRSTRLEN;
int atoi(char *);
int /*<<< orphan*/ errno;
int inet_pton(void *, char *, struct in_addr *);
int /*<<< orphan*/ loge(char *, char const *, int /*<<< orphan*/,
                        int /*<<< orphan*/);
scalar_t__ strchr(char *, char);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
int ns_ecs_parse_subnet(struct sockaddr *addr, int *pmask, const char *str) {
  char buf[INET6_ADDRSTRLEN], *sp_pos;
  int mask = -1;
  int is_ipv6;
  strncpy(buf, str, INET6_ADDRSTRLEN);
  buf[INET6_ADDRSTRLEN - 1] = '\0';
  sp_pos = strrchr(buf, '/');
  if (sp_pos) {
    *sp_pos = 0;
    mask = atoi(sp_pos + 1);
  }
  is_ipv6 = buf[0] == '[' || strchr(buf, ':');
  if (is_ipv6) {
    struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
    struct in6_addr *ip = &(addr6->sin6_addr);
    if (inet_pton(AF_INET6, buf, ip) != 1) {
      loge("invalid addr %s. ns_ecs_parse_subnet() - inet_pton() error: "
           "errno=%d, %s\n",
           str, errno, strerror(errno));
      return -1;
    }
    addr6->sin6_family = AF_INET6;
    if (mask == -1)
      mask = 128;
  } else {
    struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
    struct in_addr *ip = &(addr4->sin_addr);
    if (inet_pton(AF_INET, buf, ip) != 1) {
      loge("invalid addr %s. ns_ecs_parse_subnet() - inet_pton() error: "
           "errno=%d, %s\n",
           str, errno, strerror(errno));
      return -1;
    }
    addr4->sin_family = AF_INET;
    if (mask == -1)
      mask = 32;
  }
  *pmask = mask;
  return 0;
}


x86 assembly (clang, -O0):
<ns_ecs_parse_subnet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x20(%rbp),%rsi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x70(%rbp),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
cltq
movb   $0x0,(%rdi,%rax,1)
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1212 <ns_ecs_parse_subnet+0xa2>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x70(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x5b,%ecx
mov    %al,-0x71(%rbp)
je     123f <ns_ecs_parse_subnet+0xcf>
mov    -0x70(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x71(%rbp)
mov    -0x71(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     12f9 <ns_ecs_parse_subnet+0x189>
mov    -0x70(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x50(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%rdi
mov    -0x50(%rbp),%rdx
call   1060 <inet_pton@plt>
cmp    $0x1,%eax
je     12d2 <ns_ecs_parse_subnet+0x162>
mov    -0x20(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x80(%rbp),%rsi
mov    -0x78(%rbp),%edx
mov    %eax,%ecx
lea    0xd46(%rip),%rdi
call   1070 <loge@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13c1 <ns_ecs_parse_subnet+0x251>
mov    0x2cff(%rip),%rax
mov    (%rax),%rcx
mov    -0x48(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12f4 <ns_ecs_parse_subnet+0x184>
movl   $0x80,-0x3c(%rbp)
jmp    13aa <ns_ecs_parse_subnet+0x23a>
mov    -0x70(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x68(%rbp)
mov    0x2cc8(%rip),%rax
mov    (%rax),%rdi
mov    -0x68(%rbp),%rdx
call   1060 <inet_pton@plt>
cmp    $0x1,%eax
je     1383 <ns_ecs_parse_subnet+0x213>
mov    -0x20(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    0x2c89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x90(%rbp),%rsi
mov    -0x84(%rbp),%edx
mov    %eax,%ecx
lea    0xc95(%rip),%rdi
call   1070 <loge@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13c1 <ns_ecs_parse_subnet+0x251>
mov    0x2c56(%rip),%rax
mov    (%rax),%rcx
mov    -0x60(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    13a5 <ns_ecs_parse_subnet+0x235>
movl   $0x20,-0x3c(%rbp)
jmp    13aa <ns_ecs_parse_subnet+0x23a>
mov    -0x3c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ns_ecs_parse_subnet>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,-0x30(%rbp)
mov    %rdi,%r12
mov    0x2e59(%rip),%r14
mov    (%r14),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strncpy@plt>
movslq (%r14),%rax
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11e4 <ns_ecs_parse_subnet+0x74>
movb   $0x0,(%rax)
add    $0x1,%rax
mov    %rax,%rdi
call   1080 <atoi@plt>
mov    %eax,%r13d
cmpb   $0x5b,(%rbx)
jne    11ef <ns_ecs_parse_subnet+0x7f>
jmp    1201 <ns_ecs_parse_subnet+0x91>
mov    $0xffffffff,%r13d
cmpb   $0x5b,(%rbx)
je     1201 <ns_ecs_parse_subnet+0x91>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     123b <ns_ecs_parse_subnet+0xcb>
lea    0x8(%r12),%rdx
mov    0x2dcb(%rip),%r14
mov    (%r14),%rdi
mov    %rbx,%rsi
call   1060 <inet_pton@plt>
mov    $0x80,%ecx
cmp    $0x1,%eax
jne    125c <ns_ecs_parse_subnet+0xec>
mov    (%r14),%rax
mov    %rax,(%r12)
cmp    $0xffffffff,%r13d
cmovne %r13d,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
jmp    1284 <ns_ecs_parse_subnet+0x114>
lea    0x8(%r12),%rdx
mov    0x2d99(%rip),%r14
mov    (%r14),%rdi
mov    %rbx,%rsi
call   1060 <inet_pton@plt>
mov    $0x20,%ecx
cmp    $0x1,%eax
je     1222 <ns_ecs_parse_subnet+0xb2>
mov    0x2d65(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xd8d(%rip),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
call   1070 <loge@plt>
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in6 = type { i8*, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.in6_addr = type opaque
%struct.sockaddr_in = type { i8*, %struct.in_addr }
@INET6_ADDRSTRLEN = global i32 0
@AF_INET6 = global i8* null
@.str = [74 x i8] c"invalid addr %s. ns_ecs_parse_subnet() - inet_pton() error: errno=%d, %s\0A\00"
@errno = global i32 0
@AF_INET = global i8* null
define i32 @ns_ecs_parse_subnet(%struct.sockaddr* %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in6*
%14 = alloca %struct.in6_addr*
%15 = alloca i32
%16 = alloca %struct.sockaddr_in*
%17 = alloca %struct.in_addr*
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
%18 = load i32, i32* @INET6_ADDRSTRLEN
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %8
%21 = alloca i8, i64 %19
store i64 %19, i64* %9
store i32 -1, i32* %11
%22 = load i8*, i8** %7
%23 = load i32, i32* @INET6_ADDRSTRLEN
%24 = call i32 @strncpy(i8* %21, i8* %22, i32 %23)
%25 = load i32, i32* @INET6_ADDRSTRLEN
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %21, i64 %27
store i8 0, i8* %28
%29 = call i8* @strrchr(i8* %21, i8 signext 47)
store i8* %29, i8** %10
%30 = load i8*, i8** %10
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %37
32: ; preds = %3
%33 = load i8*, i8** %10
store i8 0, i8* %33
%34 = load i8*, i8** %10
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = call i32 @atoi(i8* %35)
store i32 %36, i32* %11
br label %37
37: ; preds = %32, %3
%38 = getelementptr inbounds i8, i8* %21, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 91
br i1 %41, label %45, label %42
42: ; preds = %37
%43 = call i64 @strchr(i8* %21, i8 signext 58)
%44 = icmp ne i64 %43, 0
br label %45
45: ; preds = %42, %37
%46 = phi i1 [ true, %37 ], [ %44, %42 ]
%47 = zext i1 %46 to i32
store i32 %47, i32* %12
%48 = load i32, i32* %12
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %75
50: ; preds = %45
%51 = load %struct.sockaddr*, %struct.sockaddr** %5
%52 = bitcast %struct.sockaddr* %51 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %52, %struct.sockaddr_in6** %13
%53 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%54 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %53, i32 0, i32 1
%55 = bitcast %struct.in_addr* %54 to %struct.in6_addr*
store %struct.in6_addr* %55, %struct.in6_addr** %14
%56 = load i8*, i8** @AF_INET6
%57 = load %struct.in6_addr*, %struct.in6_addr** %14
%58 = bitcast %struct.in6_addr* %57 to %struct.in_addr*
%59 = call i32 @inet_pton(i8* %56, i8* %21, %struct.in_addr* %58)
%60 = icmp ne i32 %59, 1
br i1 %60, label %61, label %67
61: ; preds = %50
%62 = load i8*, i8** %7
%63 = load i32, i32* @errno
%64 = load i32, i32* @errno
%65 = call i32 @strerror(i32 %64)
%66 = call i32 @loge(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i8* %62, i32 %63, i32 %65)
store i32 -1, i32* %4
store i32 1, i32* %15
br label %101
67: ; preds = %50
%68 = load i8*, i8** @AF_INET6
%69 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %13
%70 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %69, i32 0, i32 0
store i8* %68, i8** %70
%71 = load i32, i32* %11
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %74
73: ; preds = %67
store i32 128, i32* %11
br label %74
74: ; preds = %73, %67
br label %98
75: ; preds = %45
%76 = load %struct.sockaddr*, %struct.sockaddr** %5
%77 = bitcast %struct.sockaddr* %76 to %struct.sockaddr_in*
store %struct.sockaddr_in* %77, %struct.sockaddr_in** %16
%78 = load %struct.sockaddr_in*, %struct.sockaddr_in** %16
%79 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %78, i32 0, i32 1
store %struct.in_addr* %79, %struct.in_addr** %17
%80 = load i8*, i8** @AF_INET
%81 = load %struct.in_addr*, %struct.in_addr** %17
%82 = call i32 @inet_pton(i8* %80, i8* %21, %struct.in_addr* %81)
%83 = icmp ne i32 %82, 1
br i1 %83, label %84, label %90
84: ; preds = %75
%85 = load i8*, i8** %7
%86 = load i32, i32* @errno
%87 = load i32, i32* @errno
%88 = call i32 @strerror(i32 %87)
%89 = call i32 @loge(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i8* %85, i32 %86, i32 %88)
store i32 -1, i32* %4
store i32 1, i32* %15
br label %101
90: ; preds = %75
%91 = load i8*, i8** @AF_INET
%92 = load %struct.sockaddr_in*, %struct.sockaddr_in** %16
%93 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %92, i32 0, i32 0
store i8* %91, i8** %93
%94 = load i32, i32* %11
%95 = icmp eq i32 %94, -1
br i1 %95, label %96, label %97
96: ; preds = %90
store i32 32, i32* %11
br label %97
97: ; preds = %96, %90
br label %98
98: ; preds = %97, %74
%99 = load i32, i32* %11
%100 = load i32*, i32** %6
store i32 %99, i32* %100
store i32 0, i32* %4
store i32 1, i32* %15
br label %101
101: ; preds = %98, %84, %61
%102 = load i8*, i8** %8
%103 = load i32, i32* %4
ret i32 %103
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.in_addr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@AF_INET6 = global i8* null
@.str = [74 x i8] c"invalid addr %s. ns_ecs_parse_subnet() - inet_pton() error: errno=%d, %s\0A\00"
@errno = global i32 0
@AF_INET = global i8* null
define i32 @ns_ecs_parse_subnet(%struct.sockaddr* %0, i32* nocapture writeonly %1, i8* %2) {
%4 = load i32, i32* @INET6_ADDRSTRLEN
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @strncpy(i8* %6, i8* %2, i32 %4)
%8 = load i32, i32* @INET6_ADDRSTRLEN
%9 = add nsw i32 %8, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %6, i64 %10
store i8 0, i8* %11
%12 = call i8* @strrchr(i8* %6, i8 signext 47)
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
store i8 0, i8* %12
%15 = getelementptr inbounds i8, i8* %12, i64 1
%16 = call i32 @atoi(i8* %15)
br label %17
17: ; preds = %14, %3
%18 = phi i32 [ %16, %14 ], [ -1, %3 ]
%19 = load i8, i8* %6
%20 = icmp eq i8 %19, 91
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = call i64 @strchr(i8* %6, i8 signext 58)
%23 = icmp eq i64 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %17, %21
%25 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 2
%26 = bitcast %struct.sockaddr* %25 to %struct.in_addr*
%27 = load i8*, i8** @AF_INET6
%28 = call i32 @inet_pton(i8* %27, i8* %6, %struct.in_addr* %26)
%29 = icmp eq i32 %28, 1
br i1 %29, label %44, label %30
30: ; preds = %24
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @loge(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %31, i32 %32)
br label %51
34: ; preds = %21
%35 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 2
%36 = bitcast %struct.sockaddr* %35 to %struct.in_addr*
%37 = load i8*, i8** @AF_INET
%38 = call i32 @inet_pton(i8* %37, i8* %6, %struct.in_addr* %36)
%39 = icmp eq i32 %38, 1
br i1 %39, label %44, label %40
40: ; preds = %34
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 @loge(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %41, i32 %42)
br label %51
44: ; preds = %34, %24
%45 = phi i8** [ @AF_INET6, %24 ], [ @AF_INET, %34 ]
%46 = phi i32 [ 128, %24 ], [ 32, %34 ]
%47 = load i8*, i8** %45
%48 = bitcast %struct.sockaddr* %0 to i8**
store i8* %47, i8** %48
%49 = icmp eq i32 %18, -1
%50 = select i1 %49, i32 %46, i32 %18
store i32 %50, i32* %1
br label %51
51: ; preds = %40, %30, %44
%52 = phi i32 [ 0, %44 ], [ -1, %30 ], [ -1, %40 ]
ret i32 %52
}