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

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ assert(char const *);
int /*<<< orphan*/ free(char *);
int inet_pton(int /*<<< orphan*/, char *, struct sockaddr_in6 *);
scalar_t__ isdigit(char);
char *strchr(char *, char);
char *strdup(char const *);
scalar_t__ strlen(char *);
int host_is_ipv6_with_port(const char *host, char **addr, char **port) {
  char *str = NULL;
  int is_ipv6_with_port = 0;
  int rv = -1;
  assert(host);
  if (!(str = strdup(host)))
    goto cleanup;
  if (str[0] == '[') {
    char *addrptr = &str[1];
    char *tmp;
    if ((tmp = strchr(addrptr, ']'))) {
      *tmp = '\0';
      tmp++;
      if (*tmp == ':') {
        char *portptr;
        tmp++;
        portptr = tmp;
        while (isdigit(*tmp))
          tmp++;
        if (*tmp == '\0' && strlen(portptr)) {
          struct sockaddr_in6 saddr;
          if (inet_pton(AF_INET6, addrptr, &saddr) == 1) {
            is_ipv6_with_port = 1;
            if (addr) {
              if (!(*addr = strdup(addrptr)))
                goto cleanup;
            }
            if (port) {
              if (!(*port = strdup(portptr))) {
                free(*addr);
                goto cleanup;
              }
            }
          }
        }
      }
    }
  }
  rv = is_ipv6_with_port;
cleanup:
  free(str);
  return (rv);
}


x86 assembly (clang, -O0):
<host_is_ipv6_with_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11bf <host_is_ipv6_with_port+0x4f>
jmp    1327 <host_is_ipv6_with_port+0x1b7>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5b,%eax
jne    1321 <host_is_ipv6_with_port+0x1b1>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x5d,%esi
call   1060 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     131c <host_is_ipv6_with_port+0x1ac>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    1317 <host_is_ipv6_with_port+0x1a7>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
je     1255 <host_is_ipv6_with_port+0xe5>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    122e <host_is_ipv6_with_port+0xbe>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1312 <host_is_ipv6_with_port+0x1a2>
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
je     1312 <host_is_ipv6_with_port+0x1a2>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
lea    -0x48(%rbp),%rdx
call   1080 <inet_pton@plt>
cmp    $0x1,%eax
jne    130d <host_is_ipv6_with_port+0x19d>
movl   $0x1,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12cd <host_is_ipv6_with_port+0x15d>
mov    -0x30(%rbp),%rdi
call   1090 <strdup@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    12c8 <host_is_ipv6_with_port+0x158>
jmp    1327 <host_is_ipv6_with_port+0x1b7>
jmp    12cd <host_is_ipv6_with_port+0x15d>
cmpq   $0x0,-0x18(%rbp)
je     1308 <host_is_ipv6_with_port+0x198>
mov    -0x40(%rbp),%rdi
call   1090 <strdup@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1303 <host_is_ipv6_with_port+0x193>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
jmp    1327 <host_is_ipv6_with_port+0x1b7>
jmp    1308 <host_is_ipv6_with_port+0x198>
jmp    130d <host_is_ipv6_with_port+0x19d>
jmp    1312 <host_is_ipv6_with_port+0x1a2>
jmp    1317 <host_is_ipv6_with_port+0x1a7>
jmp    131c <host_is_ipv6_with_port+0x1ac>
jmp    1321 <host_is_ipv6_with_port+0x1b1>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_is_ipv6_with_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbx
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,%r12
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11fa <host_is_ipv6_with_port+0x8a>
xor    %ebp,%ebp
cmpb   $0x5b,(%r12)
jne    11fa <host_is_ipv6_with_port+0x8a>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x5d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11fa <host_is_ipv6_with_port+0x8a>
mov    %rax,%r14
movb   $0x0,(%rax)
cmpb   $0x3a,0x1(%rax)
jne    11fa <host_is_ipv6_with_port+0x8a>
mov    %r13,0x8(%rsp)
add    $0x2,%r14
mov    %r14,%r13
nopl   0x0(%rax,%rax,1)
movsbl 0x0(%r13),%edi
call   1040 <isdigit@plt>
add    $0x1,%r13
test   %rax,%rax
jne    11e0 <host_is_ipv6_with_port+0x70>
cmpb   $0x0,-0x1(%r13)
je     1213 <host_is_ipv6_with_port+0xa3>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     11fa <host_is_ipv6_with_port+0x8a>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rdx
mov    %rbx,%rsi
call   1080 <inet_pton@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
jne    11fa <host_is_ipv6_with_port+0x8a>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     1258 <host_is_ipv6_with_port+0xe8>
mov    %rbx,%rdi
call   1090 <strdup@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
je     127f <host_is_ipv6_with_port+0x10f>
mov    $0x1,%ebp
test   %r15,%r15
je     11fa <host_is_ipv6_with_port+0x8a>
mov    %r14,%rdi
call   1090 <strdup@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    11fa <host_is_ipv6_with_port+0x8a>
mov    0x8(%rsp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebp
jmp    11fa <host_is_ipv6_with_port+0x8a>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
@AF_INET6 = global i32 0
define i32 @host_is_ipv6_with_port(i8* %0, i8** %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.sockaddr_in6
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
store i8* null, i8** %7
store i32 0, i32* %8
store i32 -1, i32* %9
%14 = load i8*, i8** %4
%15 = call i32 @assert(i8* %14)
%16 = load i8*, i8** %4
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %7
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %3
br label %96
20: ; preds = %3
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 91
br i1 %25, label %26, label %94
26: ; preds = %20
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8* %28, i8** %10
%29 = load i8*, i8** %10
%30 = call i8* @strchr(i8* %29, i8 signext 93)
store i8* %30, i8** %11
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %93
32: ; preds = %26
%33 = load i8*, i8** %11
store i8 0, i8* %33
%34 = load i8*, i8** %11
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %11
%36 = load i8*, i8** %11
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 58
br i1 %39, label %40, label %92
40: ; preds = %32
%41 = load i8*, i8** %11
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %11
%43 = load i8*, i8** %11
store i8* %43, i8** %12
br label %44
44: ; preds = %49, %40
%45 = load i8*, i8** %11
%46 = load i8, i8* %45
%47 = call i64 @isdigit(i8 signext %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i8*, i8** %11
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %11
br label %44
52: ; preds = %44
%53 = load i8*, i8** %11
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %91
57: ; preds = %52
%58 = load i8*, i8** %12
%59 = call i64 @strlen(i8* %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %91
61: ; preds = %57
%62 = load i32, i32* @AF_INET6
%63 = load i8*, i8** %10
%64 = call i32 @inet_pton(i32 %62, i8* %63, %struct.sockaddr_in6* %13)
%65 = icmp eq i32 %64, 1
br i1 %65, label %66, label %90
66: ; preds = %61
store i32 1, i32* %8
%67 = load i8**, i8*** %5
%68 = icmp ne i8** %67, null
br i1 %68, label %69, label %76
69: ; preds = %66
%70 = load i8*, i8** %10
%71 = call i8* @strdup(i8* %70)
%72 = load i8**, i8*** %5
store i8* %71, i8** %72
%73 = icmp ne i8* %71, null
br i1 %73, label %75, label %74
74: ; preds = %69
br label %96
75: ; preds = %69
br label %76
76: ; preds = %75, %66
%77 = load i8**, i8*** %6
%78 = icmp ne i8** %77, null
br i1 %78, label %79, label %89
79: ; preds = %76
%80 = load i8*, i8** %12
%81 = call i8* @strdup(i8* %80)
%82 = load i8**, i8*** %6
store i8* %81, i8** %82
%83 = icmp ne i8* %81, null
br i1 %83, label %88, label %84
84: ; preds = %79
%85 = load i8**, i8*** %5
%86 = load i8*, i8** %85
%87 = call i32 @free(i8* %86)
br label %96
88: ; preds = %79
br label %89
89: ; preds = %88, %76
br label %90
90: ; preds = %89, %61
br label %91
91: ; preds = %90, %57, %52
br label %92
92: ; preds = %91, %32
br label %93
93: ; preds = %92, %26
br label %94
94: ; preds = %93, %20
%95 = load i32, i32* %8
store i32 %95, i32* %9
br label %96
96: ; preds = %94, %84, %74, %19
%97 = load i8*, i8** %7
%98 = call i32 @free(i8* %97)
%99 = load i32, i32* %9
ret i32 %99
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
@AF_INET6 = global i32 0
define i32 @host_is_ipv6_with_port(i8* %0, i8** %1, i8** writeonly %2) {
%4 = alloca %struct.sockaddr_in6
%5 = tail call i32 @assert(i8* %0)
%6 = tail call i8* @strdup(i8* %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %54, label %8
8: ; preds = %3
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 91
br i1 %10, label %11, label %54
11: ; preds = %8
%12 = getelementptr inbounds i8, i8* %6, i64 1
%13 = tail call i8* @strchr(i8* %12, i8 signext 93)
%14 = icmp eq i8* %13, null
br i1 %14, label %54, label %15
15: ; preds = %11
store i8 0, i8* %13
%16 = getelementptr inbounds i8, i8* %13, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 58
br i1 %18, label %19, label %54
19: ; preds = %15
%20 = getelementptr inbounds i8, i8* %13, i64 2
br label %21
21: ; preds = %21, %19
%22 = phi i8* [ %20, %19 ], [ %26, %21 ]
%23 = load i8, i8* %22
%24 = tail call i64 @isdigit(i8 signext %23)
%25 = icmp eq i64 %24, 0
%26 = getelementptr inbounds i8, i8* %22, i64 1
br i1 %25, label %27, label %21
27: ; preds = %21
%28 = load i8, i8* %22
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %54
30: ; preds = %27
%31 = tail call i64 @strlen(i8* %20)
%32 = icmp eq i64 %31, 0
br i1 %32, label %54, label %33
33: ; preds = %30
%34 = bitcast %struct.sockaddr_in6* %4 to i8*
%35 = load i32, i32* @AF_INET6
%36 = call i32 @inet_pton(i32 %35, i8* %12, %struct.sockaddr_in6* %4)
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %51
38: ; preds = %33
%39 = icmp eq i8** %1, null
br i1 %39, label %43, label %40
40: ; preds = %38
%41 = call i8* @strdup(i8* %12)
store i8* %41, i8** %1
%42 = icmp eq i8* %41, null
br i1 %42, label %53, label %43
43: ; preds = %40, %38
%44 = icmp eq i8** %2, null
br i1 %44, label %51, label %45
45: ; preds = %43
%46 = call i8* @strdup(i8* %20)
store i8* %46, i8** %2
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i8*, i8** %1
%50 = call i32 @free(i8* %49)
br label %53
51: ; preds = %43, %45, %33
%52 = phi i32 [ 0, %33 ], [ 1, %43 ], [ 1, %45 ]
br label %54
53: ; preds = %40, %48
br label %54
54: ; preds = %27, %30, %51, %11, %15, %3, %53, %8
%55 = phi i32 [ -1, %53 ], [ -1, %3 ], [ 0, %8 ], [ 0, %11 ], [ 0, %15 ], [ %52, %51 ], [ 0, %27 ], [ 0, %30 ]
%56 = call i32 @free(i8* %6)
ret i32 %55
}