#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 strlen(char const *);
char *strstr(char *, char const *);
char *collect_hostname(char *prev_pos) {
  char *start_pos, *curr_pos;
  int hostname_size = 0, stop_loop = 0;
  const char *srch_patt = "://";
  if (*prev_pos == '\0')
    return (NULL);
  curr_pos = prev_pos;
  if ((start_pos = strstr(curr_pos, srch_patt)) != NULL) {
    start_pos = start_pos + strlen(srch_patt);
    curr_pos = start_pos;
    while (stop_loop == 0) {
      switch (*curr_pos) {
      case '\0':
        stop_loop = 1;
        break;
      case '@':
        start_pos = ++curr_pos;
        hostname_size = 0;
        break;
      case ':':
      case '?':
      case ';':
      case '\\':
      case '/':
      case '\'':
      case '\"':
      case '>':
      case '\n':
      case '\r':
      case ')':
      case '&':
        *curr_pos = '\0';
        stop_loop = 1;
        break;
      default:
        curr_pos++;
        hostname_size++;
      }
    }
  }
  if (hostname_size == 0)
    return (NULL);
  else
    return (start_pos);
}


x86 assembly (clang, -O0):
<collect_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    0x1033(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1162 <collect_hostname+0x42>
movq   $0x0,-0x8(%rbp)
jmp    125b <collect_hostname+0x13b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     123c <collect_hostname+0x11c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1237 <collect_hostname+0x117>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
sub    $0x5c,%eax
ja     121d <collect_hostname+0xfd>
mov    -0x40(%rbp),%rax
lea    0xe27(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x1,-0x28(%rbp)
jmp    1232 <collect_hostname+0x112>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    1232 <collect_hostname+0x112>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x28(%rbp)
jmp    1232 <collect_hostname+0x112>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ae <collect_hostname+0x8e>
jmp    123c <collect_hostname+0x11c>
cmpl   $0x0,-0x24(%rbp)
jne    1253 <collect_hostname+0x133>
movq   $0x0,-0x8(%rbp)
jmp    125b <collect_hostname+0x13b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collect_hostname>:
push   %rbx
cmpb   $0x0,(%rdi)
je     1186 <collect_hostname+0x66>
lea    0x1047(%rip),%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1186 <collect_hostname+0x66>
mov    %rax,%rbx
lea    0x1033(%rip),%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%rbx
lea    0xeae(%rip),%rax
lea    0x1(%rbx),%rdx
xor    %ecx,%ecx
jmp    1167 <collect_hostname+0x47>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%ecx
add    $0x1,%rdx
movsbl -0x1(%rdx),%esi
cmp    $0x5c,%esi
ja     1160 <collect_hostname+0x40>
movslq (%rax,%rsi,4),%rsi
add    %rax,%rsi
jmp    *%rsi
mov    %rdx,%rbx
jmp    1152 <collect_hostname+0x32>
movb   $0x0,-0x1(%rdx)
test   %ecx,%ecx
jne    1188 <collect_hostname+0x68>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"://\00"
define i8* @collect_hostname(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %27, label %4
4: ; preds = %1
%5 = tail call i8* @strstr(i8* %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 %26, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 %9
br label %11
11: ; preds = %18, %7
%12 = phi i8* [ %19, %18 ], [ %10, %7 ]
br label %13
13: ; preds = %11, %21
%14 = phi i32 [ %23, %21 ], [ 0, %11 ]
%15 = phi i8* [ %22, %21 ], [ %12, %11 ]
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
switch i32 %17, label %21 [
i32 0, label %24
i32 64, label %18
i32 58, label %20
i32 63, label %20
i32 59, label %20
i32 92, label %20
i32 47, label %20
i32 39, label %20
i32 34, label %20
i32 62, label %20
i32 10, label %20
i32 13, label %20
i32 41, label %20
i32 38, label %20
]
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %15, i64 1
br label %11
20: ; preds = %13, %13, %13, %13, %13, %13, %13, %13, %13, %13, %13, %13
store i8 0, i8* %15
br label %24
21: ; preds = %13
%22 = getelementptr inbounds i8, i8* %15, i64 1
%23 = add nuw nsw i32 %14, 1
br label %13
24: ; preds = %13, %20
%25 = icmp eq i32 %14, 0
br i1 %25, label %26, label %27
26: ; preds = %4, %24
br label %27
27: ; preds = %26, %24, %1
%28 = phi i8* [ null, %1 ], [ null, %26 ], [ %12, %24 ]
ret i8* %28
}