#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 /*<<< orphan*/ free(char *);
scalar_t__ host_is_ipv6_with_port(char const *, int /*<<< orphan*/ *,
                                  int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *strchr(char const *, char);
char *strdup(char const *);
int strlen(char const *);
char *_fi_preparse_host(const char *host) {
  char *new = NULL;
  char *rv = NULL;
  if (host_is_ipv6_with_port(host, NULL, NULL)) {
    unsigned int len = 0;
    char *pl;
    char *pr;
    if (!(new = (char *)malloc(strlen(host) + 5)))
      goto cleanup;
    pl = strchr(host, '[');
    memcpy(new, host, pl - host);
    len += (pl - host);
    memcpy(new + len, "@l@", strlen("@l@"));
    len += strlen("@l@");
    pl++;
    pr = strchr(pl, ']');
    memcpy(new + len, pl, pr - pl);
    len += (pr - pl);
    memcpy(new + len, "@r@", strlen("@r@"));
    len += strlen("@r@");
    pr++;
    memcpy(new + len, pr, host + strlen(host) - pr);
    len += (host + strlen(host) - pr);
    new[len] = '\0';
  } else {
    if (!(new = strdup(host)))
      goto cleanup;
  }
  rv = new;
cleanup:
  if (!rv)
    free(new);
  return (rv);
}


x86 assembly (clang, -O0):
<_fi_preparse_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1040 <host_is_ipv6_with_port@plt>
cmp    $0x0,%rax
je     1370 <_fi_preparse_host+0x200>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x5,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11d3 <_fi_preparse_host+0x63>
jmp    1399 <_fi_preparse_host+0x229>
mov    -0x8(%rbp),%rdi
mov    $0x5b,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    %rax,%rcx
mov    -0x1c(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
lea    0xdd8(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%edx
lea    0xdc6(%rip),%rsi
call   1070 <memcpy@plt>
lea    0xdba(%rip),%rdi
call   1050 <strlen@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x5d,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rax,%rcx
mov    -0x1c(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
lea    0xd4c(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%edx
lea    0xd3a(%rip),%rsi
call   1070 <memcpy@plt>
lea    0xd2e(%rip),%rdi
call   1050 <strlen@plt>
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rcx
cltq
add    %rax,%rcx
mov    -0x30(%rbp),%rax
sub    %rax,%rcx
mov    -0x1c(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
jmp    1391 <_fi_preparse_host+0x221>
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    138c <_fi_preparse_host+0x21c>
jmp    1399 <_fi_preparse_host+0x229>
jmp    1391 <_fi_preparse_host+0x221>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    13ad <_fi_preparse_host+0x23d>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fi_preparse_host>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %esi,%esi
xor    %edx,%edx
call   1040 <host_is_ipv6_with_port@plt>
test   %rax,%rax
je     128e <_fi_preparse_host+0x11e>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x5(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     12a1 <_fi_preparse_host+0x131>
mov    %r14,%rdi
mov    $0x5b,%esi
call   1060 <strchr@plt>
mov    %rax,%r13
mov    %rax,%r15
sub    %r14,%r15
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1070 <memcpy@plt>
mov    %r15d,%ebx
add    %r12,%rbx
lea    0xe23(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
add    %r15d,%ebx
add    $0x1,%r13
mov    %r13,%rdi
mov    $0x5d,%esi
call   1060 <strchr@plt>
mov    %rax,%r15
lea    (%rbx,%r12,1),%rdi
mov    %r15d,%ebp
sub    %r13d,%ebp
mov    %r13,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
add    %ebx,%ebp
lea    (%r12,%rbp,1),%rbx
lea    0xdd0(%rip),%r13
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
add    %ebp,%ebx
add    $0x1,%r15
lea    (%rbx,%r12,1),%rbp
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r14,1),%edx
sub    %r15d,%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %r14d,%eax
sub    %r15d,%eax
add    %ebx,%eax
movb   $0x0,(%r12,%rax,1)
jmp    12ac <_fi_preparse_host+0x13c>
mov    %r14,%rdi
call   1090 <strdup@plt>
mov    %rax,%r12
test   %rax,%rax
jne    12ac <_fi_preparse_host+0x13c>
xor    %r12d,%r12d
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"@l@\00"
@.str.1 = [4 x i8] c"@r@\00"
define i8* @_fi_preparse_host(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %3
store i8* null, i8** %4
%8 = load i8*, i8** %2
%9 = call i64 @host_is_ipv6_with_port(i8* %8, i32* null, i32* null)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %118
11: ; preds = %1
store i32 0, i32* %5
%12 = load i8*, i8** %2
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 5
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %3
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %11
br label %126
19: ; preds = %11
%20 = load i8*, i8** %2
%21 = call i8* @strchr(i8* %20, i8 signext 91)
store i8* %21, i8** %6
%22 = load i8*, i8** %3
%23 = load i8*, i8** %2
%24 = load i8*, i8** %6
%25 = load i8*, i8** %2
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %25 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
%30 = call i32 @memcpy(i8* %22, i8* %23, i32 %29)
%31 = load i8*, i8** %6
%32 = load i8*, i8** %2
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = load i32, i32* %5
%37 = zext i32 %36 to i64
%38 = add nsw i64 %37, %35
%39 = trunc i64 %38 to i32
store i32 %39, i32* %5
%40 = load i8*, i8** %3
%41 = load i32, i32* %5
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%45 = call i32 @memcpy(i8* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %44)
%46 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%47 = load i32, i32* %5
%48 = add i32 %47, %46
store i32 %48, i32* %5
%49 = load i8*, i8** %6
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %6
%51 = load i8*, i8** %6
%52 = call i8* @strchr(i8* %51, i8 signext 93)
store i8* %52, i8** %7
%53 = load i8*, i8** %3
%54 = load i32, i32* %5
%55 = zext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
%57 = load i8*, i8** %6
%58 = load i8*, i8** %7
%59 = load i8*, i8** %6
%60 = ptrtoint i8* %58 to i64
%61 = ptrtoint i8* %59 to i64
%62 = sub i64 %60, %61
%63 = trunc i64 %62 to i32
%64 = call i32 @memcpy(i8* %56, i8* %57, i32 %63)
%65 = load i8*, i8** %7
%66 = load i8*, i8** %6
%67 = ptrtoint i8* %65 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
%70 = load i32, i32* %5
%71 = zext i32 %70 to i64
%72 = add nsw i64 %71, %69
%73 = trunc i64 %72 to i32
store i32 %73, i32* %5
%74 = load i8*, i8** %3
%75 = load i32, i32* %5
%76 = zext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%79 = call i32 @memcpy(i8* %77, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %78)
%80 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%81 = load i32, i32* %5
%82 = add i32 %81, %80
store i32 %82, i32* %5
%83 = load i8*, i8** %7
%84 = getelementptr inbounds i8, i8* %83, i32 1
store i8* %84, i8** %7
%85 = load i8*, i8** %3
%86 = load i32, i32* %5
%87 = zext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %85, i64 %87
%89 = load i8*, i8** %7
%90 = load i8*, i8** %2
%91 = load i8*, i8** %2
%92 = call i32 @strlen(i8* %91)
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i8, i8* %90, i64 %93
%95 = load i8*, i8** %7
%96 = ptrtoint i8* %94 to i64
%97 = ptrtoint i8* %95 to i64
%98 = sub i64 %96, %97
%99 = trunc i64 %98 to i32
%100 = call i32 @memcpy(i8* %88, i8* %89, i32 %99)
%101 = load i8*, i8** %2
%102 = load i8*, i8** %2
%103 = call i32 @strlen(i8* %102)
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds i8, i8* %101, i64 %104
%106 = load i8*, i8** %7
%107 = ptrtoint i8* %105 to i64
%108 = ptrtoint i8* %106 to i64
%109 = sub i64 %107, %108
%110 = load i32, i32* %5
%111 = zext i32 %110 to i64
%112 = add nsw i64 %111, %109
%113 = trunc i64 %112 to i32
store i32 %113, i32* %5
%114 = load i8*, i8** %3
%115 = load i32, i32* %5
%116 = zext i32 %115 to i64
%117 = getelementptr inbounds i8, i8* %114, i64 %116
store i8 0, i8* %117
br label %124
118: ; preds = %1
%119 = load i8*, i8** %2
%120 = call i8* @strdup(i8* %119)
store i8* %120, i8** %3
%121 = icmp ne i8* %120, null
br i1 %121, label %123, label %122
122: ; preds = %118
br label %126
123: ; preds = %118
br label %124
124: ; preds = %123, %19
%125 = load i8*, i8** %3
store i8* %125, i8** %4
br label %126
126: ; preds = %124, %122, %18
%127 = load i8*, i8** %4
%128 = icmp ne i8* %127, null
br i1 %128, label %132, label %129
129: ; preds = %126
%130 = load i8*, i8** %3
%131 = call i32 @free(i8* %130)
br label %132
132: ; preds = %129, %126
%133 = load i8*, i8** %4
ret i8* %133
}

LLVM IR (-O2):
@.str = [4 x i8] c"@l@\00"
@.str.1 = [4 x i8] c"@r@\00"
define i8* @_fi_preparse_host(i8* %0) {
%2 = tail call i64 @host_is_ipv6_with_port(i8* %0, i32* null, i32* null)
%3 = icmp eq i64 %2, 0
br i1 %3, label %59, label %4
4: ; preds = %1
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 5
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %62, label %10
10: ; preds = %4
%11 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 91)
%12 = ptrtoint i8* %11 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = tail call i32 @memcpy(i8* %8, i8* %0, i32 %15)
%17 = and i64 %14, 4294967295
%18 = getelementptr inbounds i8, i8* %8, i64 %17
%19 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @memcpy(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%22 = add i32 %21, %15
%23 = getelementptr inbounds i8, i8* %11, i64 1
%24 = tail call i8* @strchr(i8* %23, i8 signext 93)
%25 = zext i32 %22 to i64
%26 = getelementptr inbounds i8, i8* %8, i64 %25
%27 = ptrtoint i8* %24 to i64
%28 = ptrtoint i8* %23 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
%31 = tail call i32 @memcpy(i8* %26, i8* %23, i32 %30)
%32 = add i32 %22, %30
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %8, i64 %33
%35 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%36 = tail call i32 @memcpy(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%38 = add i32 %32, %37
%39 = getelementptr inbounds i8, i8* %24, i64 1
%40 = zext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %8, i64 %40
%42 = tail call i32 @strlen(i8* %0)
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %0, i64 %43
%45 = ptrtoint i8* %44 to i64
%46 = ptrtoint i8* %39 to i64
%47 = sub i64 %45, %46
%48 = trunc i64 %47 to i32
%49 = tail call i32 @memcpy(i8* %41, i8* %39, i32 %48)
%50 = tail call i32 @strlen(i8* %0)
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %0, i64 %51
%53 = ptrtoint i8* %52 to i64
%54 = sub i64 %53, %46
%55 = trunc i64 %54 to i32
%56 = add i32 %38, %55
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %8, i64 %57
store i8 0, i8* %58
br label %65
59: ; preds = %1
%60 = tail call i8* @strdup(i8* %0)
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %65
62: ; preds = %59, %4
%63 = phi i8* [ %8, %4 ], [ null, %59 ]
%64 = tail call i32 @free(i8* %63)
br label %65
65: ; preds = %10, %59, %62
%66 = phi i8* [ null, %62 ], [ %60, %59 ], [ %8, %10 ]
ret i8* %66
}