#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 */
char *strrchr(char *, char);
char *strstr(char *, char *);
int parse_url(char *s, char **protocol, char **host, char **port) {
  char *p;
  int cnt = 0;
  p = strstr(s, "://");
  if (p) {
    *protocol = s;
    *p = '\0';
    s = p + 3;
  } else {
    *protocol = NULL;
  }
  if (*s == '[') {
    p = strrchr(s, ']');
    if (p) {
      *host = s + 1;
      *p = '\0';
      p++;
      if (*p == ':')
        *port = p + 1;
      else
        *port = NULL;
      return 0;
    } else {
      return -1;
    }
  }
  p = strrchr(s, ':');
  if (p) {
    *port = p + 1;
    *p = '\0';
  } else {
    *port = NULL;
  }
  *host = s;
  return 0;
}


x86 assembly (clang, -O0):
<parse_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb6(%rip),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1181 <parse_url+0x61>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
jmp    118c <parse_url+0x6c>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5b,%eax
jne    1222 <parse_url+0x102>
mov    -0x10(%rbp),%rdi
mov    $0x5d,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1216 <parse_url+0xf6>
mov    -0x10(%rbp),%rcx
add    $0x1,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    11ff <parse_url+0xdf>
mov    -0x30(%rbp),%rcx
add    $0x1,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
jmp    120a <parse_url+0xea>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1277 <parse_url+0x157>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <parse_url+0x157>
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     125a <parse_url+0x13a>
mov    -0x30(%rbp),%rcx
add    $0x1,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
jmp    1265 <parse_url+0x145>
mov    -0x28(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_url>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0xec5(%rip),%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     118d <parse_url+0x6d>
mov    %rbx,(%r12)
movb   $0x0,(%rax)
add    $0x3,%rax
mov    %rax,%rbx
cmpb   $0x5b,(%rbx)
jne    119a <parse_url+0x7a>
mov    %rbx,%rdi
mov    $0x5d,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     11bb <parse_url+0x9b>
mov    %rax,%rcx
add    $0x1,%rbx
mov    %rbx,(%r15)
movb   $0x0,(%rax)
mov    %rax,%rdx
add    $0x2,%rdx
xor    %eax,%eax
cmpb   $0x3a,0x1(%rcx)
cmovne %rax,%rdx
mov    %rdx,(%r14)
jmp    11ce <parse_url+0xae>
movq   $0x0,(%r12)
cmpb   $0x5b,(%rbx)
je     1158 <parse_url+0x38>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     11c2 <parse_url+0xa2>
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,(%r14)
movb   $0x0,(%rax)
jmp    11c9 <parse_url+0xa9>
mov    $0xffffffff,%eax
jmp    11ce <parse_url+0xae>
movq   $0x0,(%r14)
mov    %rbx,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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