#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 asprintf(char **, char *, char *);
int /*<<< orphan*/ free(char *);
char *strchr(char *, char);
char *strdup(char const *);
int tls_host_port(const char *hostport, char **host, char **port) {
  char *h, *p, *s;
  int rv = 1;
  *host = NULL;
  *port = NULL;
  if ((s = strdup(hostport)) == NULL)
    goto err;
  h = p = s;
  if (p[0] == '[') {
    h++;
    if ((p = strchr(s, ']')) == NULL)
      goto done;
    *p++ = '\0';
  }
  if ((p = strchr(p, ':')) == NULL)
    goto done;
  if (strchr(p + 1, ':') != NULL)
    goto done;
  *p++ = '\0';
  if (asprintf(host, "%s", h) == -1)
    goto err;
  if (asprintf(port, "%s", p) == -1)
    goto err;
  rv = 0;
  goto done;
err:
  free(*host);
  *host = NULL;
  free(*port);
  *port = NULL;
  rv = -1;
done:
  free(s);
  return (rv);
}


x86 assembly (clang, -O0):
<tls_host_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    118d <tls_host_port+0x4d>
jmp    128c <tls_host_port+0x14c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5b,%eax
jne    11e8 <tls_host_port+0xa8>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x5d,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d6 <tls_host_port+0x96>
jmp    12c1 <tls_host_port+0x181>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1209 <tls_host_port+0xc9>
jmp    12c1 <tls_host_port+0x181>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     122a <tls_host_port+0xea>
jmp    12c1 <tls_host_port+0x181>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdb5(%rip),%rsi
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    125e <tls_host_port+0x11e>
jmp    128c <tls_host_port+0x14c>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xd93(%rip),%rsi
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1280 <tls_host_port+0x140>
jmp    128c <tls_host_port+0x14c>
movl   $0x0,-0x34(%rbp)
jmp    12c1 <tls_host_port+0x181>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tls_host_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
movq   $0x0,(%rsi)
movq   $0x0,(%rdx)
call   1060 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1206 <tls_host_port+0xc6>
cmpb   $0x5b,(%rbx)
mov    %rbx,%rdi
mov    %rbx,%r12
jne    11a2 <tls_host_port+0x62>
mov    %rbx,%rdi
mov    $0x5d,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     122b <tls_host_port+0xeb>
mov    %rbx,%r12
add    $0x1,%r12
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     1230 <tls_host_port+0xf0>
mov    %rax,%r13
lea    0x1(%rax),%rdi
mov    %rdi,(%rsp)
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    1230 <tls_host_port+0xf0>
movb   $0x0,0x0(%r13)
lea    0xe24(%rip),%rsi
mov    %r15,%rdi
mov    %r12,%rdx
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1206 <tls_host_port+0xc6>
lea    0xe0d(%rip),%rsi
mov    %r14,%rdi
mov    (%rsp),%rdx
call   1050 <asprintf@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
jne    1230 <tls_host_port+0xf0>
mov    (%r15),%rdi
call   1030 <free@plt>
movq   $0x0,(%r15)
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    $0xffffffff,%ebp
jmp    1230 <tls_host_port+0xf0>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i32 @tls_host_port(i8* nocapture %0, i8** %1, i8** %2) {
store i8* null, i8** %1
store i8* null, i8** %2
%4 = tail call i8* @strdup(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %30, label %6
6: ; preds = %3
%7 = load i8, i8* %4
%8 = icmp eq i8 %7, 91
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = tail call i8* @strchr(i8* %4, i8 signext 93)
%11 = icmp eq i8* %10, null
br i1 %11, label %35, label %12
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %4, i64 1
%14 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %10
br label %15
15: ; preds = %12, %6
%16 = phi i8* [ %14, %12 ], [ %4, %6 ]
%17 = phi i8* [ %13, %12 ], [ %4, %6 ]
%18 = tail call i8* @strchr(i8* %16, i8 signext 58)
%19 = icmp eq i8* %18, null
br i1 %19, label %35, label %20
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %18, i64 1
%22 = tail call i8* @strchr(i8* %21, i8 signext 58)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %35
24: ; preds = %20
store i8 0, i8* %18
%25 = tail call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
%26 = icmp eq i32 %25, -1
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = tail call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %21)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %27, %24, %3
%31 = load i8*, i8** %1
%32 = tail call i32 @free(i8* %31)
store i8* null, i8** %1
%33 = load i8*, i8** %2
%34 = tail call i32 @free(i8* %33)
store i8* null, i8** %2
br label %35
35: ; preds = %27, %20, %15, %9, %30
%36 = phi i32 [ -1, %30 ], [ 1, %9 ], [ 1, %15 ], [ 1, %20 ], [ 0, %27 ]
%37 = tail call i32 @free(i8* %4)
ret i32 %36
}