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

/* Variables and functions */
int atoi(char const *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ htrace_log(struct htrace_log *, char *, ...);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strcspn(char const *, char *);
int parse_endpoint(struct htrace_log *lg, const char *endpoint,
                   int default_port, char **remote_out, int *port) {
  const char *remotestr;
  const char *portstr;
  char *remote = NULL;
  int remote_len;
  if (endpoint[0] == '[') {
    remotestr = endpoint + 1;
    remote_len = strcspn(remotestr, "]");
    if (remotestr[remote_len] != ']') {
      htrace_log(lg, "parse_hostport: found open square bracket, but "
                     "not matching close square bracket.\n");
      return 0;
    }
    if (remotestr[remote_len + 1] == ':') {
      portstr = remotestr + remote_len + 2;
    } else {
      portstr = NULL;
    }
  } else {
    remotestr = endpoint;
    remote_len = strcspn(remotestr, ":");
    if (remotestr[remote_len] == ':') {
      portstr = remotestr + remote_len + 1;
    } else {
      portstr = NULL;
    }
  }
  remote = malloc(remote_len + 1);
  if (!remote) {
    htrace_log(lg, "parse_hostport: unable to allocate %d-byte string.\n",
               remote_len);
    return 0;
  }
  memcpy(remote, remotestr, remote_len);
  remote[remote_len] = '\0';
  if (!portstr) {
    *port = default_port;
  } else {
    int p = atoi(portstr);
    if ((p <= 0) || (p > 0xffff)) {
      free(remote);
      htrace_log(lg, "parse_hostport: parse port string '%s'\n", portstr);
      return 0;
    }
    *port = p;
  }
  *remote_out = remote;
  return 1;
}


x86 assembly (clang, -O0):
<parse_endpoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5b,%eax
jne    1224 <parse_endpoint+0xc4>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1040 <strcspn@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5d,%eax
je     11e5 <parse_endpoint+0x85>
mov    -0x10(%rbp),%rdi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1080 <htrace_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1352 <parse_endpoint+0x1f2>
mov    -0x38(%rbp),%rax
mov    -0x4c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3a,%eax
jne    1217 <parse_endpoint+0xb7>
mov    -0x38(%rbp),%rax
movslq -0x4c(%rbp),%rcx
add    %rcx,%rax
add    $0x2,%rax
mov    %rax,-0x40(%rbp)
jmp    121f <parse_endpoint+0xbf>
movq   $0x0,-0x40(%rbp)
jmp    1279 <parse_endpoint+0x119>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1040 <strcspn@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3a,%eax
jne    126c <parse_endpoint+0x10c>
mov    -0x38(%rbp),%rax
movslq -0x4c(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1274 <parse_endpoint+0x114>
movq   $0x0,-0x40(%rbp)
jmp    1279 <parse_endpoint+0x119>
mov    -0x4c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12b4 <parse_endpoint+0x154>
mov    -0x10(%rbp),%rdi
mov    -0x4c(%rbp),%edx
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1080 <htrace_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1352 <parse_endpoint+0x1f2>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x4c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x48(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x40(%rbp)
jne    12e9 <parse_endpoint+0x189>
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
jmp    1340 <parse_endpoint+0x1e0>
mov    -0x40(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jle    130c <parse_endpoint+0x1ac>
cmpl   $0xffff,-0x50(%rbp)
jle    1337 <parse_endpoint+0x1d7>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rdx
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1080 <htrace_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    1352 <parse_endpoint+0x1f2>
mov    -0x50(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_endpoint>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %rcx,%r12
mov    %edx,%r13d
mov    %rsi,%r14
mov    %rdi,%rbx
cmpb   $0x5b,(%rsi)
jne    11ce <parse_endpoint+0x6e>
lea    0x1(%r14),%r15
lea    0xe71(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcspn@plt>
movslq %eax,%rbp
cmpb   $0x5d,0x1(%r14,%rbp,1)
jne    1292 <parse_endpoint+0x132>
mov    %r12,0x8(%rsp)
mov    %rbx,(%rsp)
lea    (%r14,%rbp,1),%rax
add    $0x1,%rax
lea    0x1(%rbp),%rdi
add    $0x2,%rax
xor    %r12d,%r12d
cmpb   $0x3a,0x1(%r15,%rbp,1)
cmove  %rax,%r12
jmp    1202 <parse_endpoint+0xa2>
mov    %r12,0x8(%rsp)
mov    %rbx,(%rsp)
lea    0xe77(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcspn@plt>
mov    %eax,%ebp
movslq %eax,%rdi
xor    %r12d,%r12d
cmpb   $0x3a,(%r14,%rdi,1)
lea    0x1(%r14,%rdi,1),%rax
cmove  %rax,%r12
add    $0x1,%edi
mov    %r14,%r15
call   1060 <malloc@plt>
test   %rax,%rax
je     1279 <parse_endpoint+0x119>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
test   %r12,%r12
je     1261 <parse_endpoint+0x101>
mov    %r12,%rdi
call   1070 <atoi@plt>
mov    %eax,%r13d
add    $0xffff0000,%eax
cmp    $0xffff0000,%eax
ja     1261 <parse_endpoint+0x101>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe3d(%rip),%rsi
xor    %r14d,%r14d
mov    (%rsp),%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1080 <htrace_log@plt>
jmp    12a6 <parse_endpoint+0x146>
mov    0x10(%rsp),%rax
mov    %r13d,(%rax)
mov    0x8(%rsp),%rax
mov    %rbx,(%rax)
mov    $0x1,%r14d
jmp    12a6 <parse_endpoint+0x146>
lea    0xdd7(%rip),%rsi
xor    %r14d,%r14d
mov    (%rsp),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <htrace_log@plt>
jmp    12a6 <parse_endpoint+0x146>
lea    0xd69(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <htrace_log@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.htrace_log = type { i32 }
@.str = [2 x i8] c"]\00"
@.str.1 = [83 x i8] c"parse_hostport: found open square bracket, but not matching close square bracket.\0A\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [52 x i8] c"parse_hostport: unable to allocate %d-byte string.\0A\00"
@.str.4 = [40 x i8] c"parse_hostport: parse port string '%s'\0A\00"
define i32 @parse_endpoint(%struct.htrace_log* %0, i8* %1, i32 %2, i8** %3, i32* %4) {
%6 = alloca i32
%7 = alloca %struct.htrace_log*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8**
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
store %struct.htrace_log* %0, %struct.htrace_log** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8** %3, i8*** %10
store i32* %4, i32** %11
store i8* null, i8** %14
%17 = load i8*, i8** %8
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 91
br i1 %21, label %22, label %54
22: ; preds = %5
%23 = load i8*, i8** %8
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8* %24, i8** %12
%25 = load i8*, i8** %12
%26 = call i32 @strcspn(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %26, i32* %15
%27 = load i8*, i8** %12
%28 = load i32, i32* %15
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 93
br i1 %33, label %34, label %37
34: ; preds = %22
%35 = load %struct.htrace_log*, %struct.htrace_log** %7
%36 = call i32 (%struct.htrace_log*, i8*, ...) @htrace_log(%struct.htrace_log* %35, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %6
br label %117
37: ; preds = %22
%38 = load i8*, i8** %12
%39 = load i32, i32* %15
%40 = add nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 58
br i1 %45, label %46, label %52
46: ; preds = %37
%47 = load i8*, i8** %12
%48 = load i32, i32* %15
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
%51 = getelementptr inbounds i8, i8* %50, i64 2
store i8* %51, i8** %13
br label %53
52: ; preds = %37
store i8* null, i8** %13
br label %53
53: ; preds = %52, %46
br label %73
54: ; preds = %5
%55 = load i8*, i8** %8
store i8* %55, i8** %12
%56 = load i8*, i8** %12
%57 = call i32 @strcspn(i8* %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 %57, i32* %15
%58 = load i8*, i8** %12
%59 = load i32, i32* %15
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 58
br i1 %64, label %65, label %71
65: ; preds = %54
%66 = load i8*, i8** %12
%67 = load i32, i32* %15
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = getelementptr inbounds i8, i8* %69, i64 1
store i8* %70, i8** %13
br label %72
71: ; preds = %54
store i8* null, i8** %13
br label %72
72: ; preds = %71, %65
br label %73
73: ; preds = %72, %53
%74 = load i32, i32* %15
%75 = add nsw i32 %74, 1
%76 = call i8* @malloc(i32 %75)
store i8* %76, i8** %14
%77 = load i8*, i8** %14
%78 = icmp ne i8* %77, null
br i1 %78, label %83, label %79
79: ; preds = %73
%80 = load %struct.htrace_log*, %struct.htrace_log** %7
%81 = load i32, i32* %15
%82 = call i32 (%struct.htrace_log*, i8*, ...) @htrace_log(%struct.htrace_log* %80, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %81)
store i32 0, i32* %6
br label %117
83: ; preds = %73
%84 = load i8*, i8** %14
%85 = load i8*, i8** %12
%86 = load i32, i32* %15
%87 = call i32 @memcpy(i8* %84, i8* %85, i32 %86)
%88 = load i8*, i8** %14
%89 = load i32, i32* %15
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i8, i8* %88, i64 %90
store i8 0, i8* %91
%92 = load i8*, i8** %13
%93 = icmp ne i8* %92, null
br i1 %93, label %97, label %94
94: ; preds = %83
%95 = load i32, i32* %9
%96 = load i32*, i32** %11
store i32 %95, i32* %96
br label %114
97: ; preds = %83
%98 = load i8*, i8** %13
%99 = call i32 @atoi(i8* %98)
store i32 %99, i32* %16
%100 = load i32, i32* %16
%101 = icmp sle i32 %100, 0
br i1 %101, label %105, label %102
102: ; preds = %97
%103 = load i32, i32* %16
%104 = icmp sgt i32 %103, 65535
br i1 %104, label %105, label %111
105: ; preds = %102, %97
%106 = load i8*, i8** %14
%107 = call i32 @free(i8* %106)
%108 = load %struct.htrace_log*, %struct.htrace_log** %7
%109 = load i8*, i8** %13
%110 = call i32 (%struct.htrace_log*, i8*, ...) @htrace_log(%struct.htrace_log* %108, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %109)
store i32 0, i32* %6
br label %117
111: ; preds = %102
%112 = load i32, i32* %16
%113 = load i32*, i32** %11
store i32 %112, i32* %113
br label %114
114: ; preds = %111, %94
%115 = load i8*, i8** %14
%116 = load i8**, i8*** %10
store i8* %115, i8** %116
store i32 1, i32* %6
br label %117
117: ; preds = %114, %105, %79, %34
%118 = load i32, i32* %6
ret i32 %118
}

LLVM IR (-O2):
%struct.htrace_log = type { i32 }
@.str = [2 x i8] c"]\00"
@.str.1 = [83 x i8] c"parse_hostport: found open square bracket, but not matching close square bracket.\0A\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [52 x i8] c"parse_hostport: unable to allocate %d-byte string.\0A\00"
@.str.4 = [40 x i8] c"parse_hostport: parse port string '%s'\0A\00"
define i32 @parse_endpoint(%struct.htrace_log* %0, i8* %1, i32 %2, i8** nocapture writeonly %3, i32* nocapture writeonly %4) {
%6 = load i8, i8* %1
%7 = icmp eq i8 %6, 91
br i1 %7, label %8, label %25
8: ; preds = %5
%9 = getelementptr inbounds i8, i8* %1, i64 1
%10 = tail call i32 @strcspn(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 93
br i1 %14, label %17, label %15
15: ; preds = %8
%16 = tail call i32 (%struct.htrace_log*, i8*, ...) @htrace_log(%struct.htrace_log* %0, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.1, i64 0, i64 0))
br label %57
17: ; preds = %8
%18 = add nsw i32 %10, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %9, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 58
%23 = getelementptr inbounds i8, i8* %12, i64 2
%24 = select i1 %22, i8* %23, i8* null
br label %34
25: ; preds = %5
%26 = tail call i32 @strcspn(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %1, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 58
%31 = getelementptr inbounds i8, i8* %28, i64 1
%32 = select i1 %30, i8* %31, i8* null
%33 = add nsw i32 %26, 1
br label %34
34: ; preds = %25, %17
%35 = phi i32 [ %33, %25 ], [ %18, %17 ]
%36 = phi i8* [ %1, %25 ], [ %9, %17 ]
%37 = phi i8* [ %32, %25 ], [ %24, %17 ]
%38 = phi i32 [ %26, %25 ], [ %10, %17 ]
%39 = tail call i8* @malloc(i32 %35)
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = tail call i32 (%struct.htrace_log*, i8*, ...) @htrace_log(%struct.htrace_log* %0, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %57
43: ; preds = %34
%44 = tail call i32 @memcpy(i8* %39, i8* %36, i32 %38)
%45 = sext i32 %38 to i64
%46 = getelementptr inbounds i8, i8* %39, i64 %45
store i8 0, i8* %46
%47 = icmp eq i8* %37, null
br i1 %47, label %55, label %48
48: ; preds = %43
%49 = tail call i32 @atoi(i8* %37)
%50 = add i32 %49, -65536
%51 = icmp ult i32 %50, -65535
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = tail call i32 @free(i8* %39)
%54 = tail call i32 (%struct.htrace_log*, i8*, ...) @htrace_log(%struct.htrace_log* %0, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %37)
br label %57
55: ; preds = %48, %43
%56 = phi i32 [ %2, %43 ], [ %49, %48 ]
store i32 %56, i32* %4
store i8* %39, i8** %3
br label %57
57: ; preds = %52, %55, %41, %15
%58 = phi i32 [ 0, %15 ], [ 1, %55 ], [ 0, %41 ], [ 0, %52 ]
ret i32 %58
}