#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 *strchr(char const *, char);
scalar_t__ strncmp(char *, char const *, int);
char *strndup(char const *, int);
char *get_host_from_link(char const *link) {
  char const *e;
  char const *s;
  if (NULL == link)
    return NULL;
  while (' ' == *link)
    link++;
  if (0 == strncmp("http://", link, 7))
    s = link + 7;
  else if (0 == strncmp("https://", link, 8))
    s = link + 8;
  else if (0 == strncmp("ftp://", link, 6))
    s = link + 6;
  else
    s = link;
  if (NULL == (e = strchr(s, '/')))
    return NULL;
  return strndup(s, e - s);
}


x86 assembly (clang, -O0):
<get_host_from_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1155 <get_host_from_link+0x25>
movq   $0x0,-0x8(%rbp)
jmp    1274 <get_host_from_link+0x144>
jmp    115a <get_host_from_link+0x2a>
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x20,%eax
cmp    %ecx,%eax
jne    117f <get_host_from_link+0x4f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    115a <get_host_from_link+0x2a>
mov    -0x10(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x7,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11b3 <get_host_from_link+0x83>
mov    -0x10(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x20(%rbp)
jmp    122d <get_host_from_link+0xfd>
mov    -0x10(%rbp),%rsi
lea    0xe4a(%rip),%rdi
mov    $0x8,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11e7 <get_host_from_link+0xb7>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    1228 <get_host_from_link+0xf8>
mov    -0x10(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x6,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    121b <get_host_from_link+0xeb>
mov    -0x10(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x20(%rbp)
jmp    1223 <get_host_from_link+0xf3>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1228 <get_host_from_link+0xf8>
jmp    122d <get_host_from_link+0xfd>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    125a <get_host_from_link+0x12a>
movq   $0x0,-0x8(%rbp)
jmp    1274 <get_host_from_link+0x144>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1050 <strndup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_host_from_link>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11ec <get_host_from_link+0xbc>
mov    %rdi,%rbx
lea    -0x1(%rdi),%r14
add    $0xffffffff,%ebx
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebx
cmpb   $0x20,0x1(%r14)
lea    0x1(%r14),%r14
je     1150 <get_host_from_link+0x20>
lea    0xe9b(%rip),%rdi
mov    %r14,%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11b5 <get_host_from_link+0x85>
lea    0xe8a(%rip),%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11bd <get_host_from_link+0x8d>
lea    0xe7a(%rip),%rdi
mov    %r14,%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
mov    $0x6,%r15d
cmovne %rcx,%r15
jmp    11c3 <get_host_from_link+0x93>
mov    $0x7,%r15d
jmp    11c3 <get_host_from_link+0x93>
mov    $0x8,%r15d
add    %r15,%r14
mov    %r14,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11ec <get_host_from_link+0xbc>
sub    %r15d,%eax
sub    %ebx,%eax
mov    %r14,%rdi
mov    %eax,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <strndup@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"http://\00"
@.str.1 = [9 x i8] c"https://\00"
@.str.2 = [7 x i8] c"ftp://\00"
define i8* @get_host_from_link(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* null, %6
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %57
9: ; preds = %1
br label %10
10: ; preds = %15, %9
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 32, %13
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %3
br label %10
18: ; preds = %10
%19 = load i8*, i8** %3
%20 = call i64 @strncmp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %19, i32 7)
%21 = icmp eq i64 0, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = getelementptr inbounds i8, i8* %23, i64 7
store i8* %24, i8** %5
br label %43
25: ; preds = %18
%26 = load i8*, i8** %3
%27 = call i64 @strncmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 8)
%28 = icmp eq i64 0, %27
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 8
store i8* %31, i8** %5
br label %42
32: ; preds = %25
%33 = load i8*, i8** %3
%34 = call i64 @strncmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %33, i32 6)
%35 = icmp eq i64 0, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i64 6
store i8* %38, i8** %5
br label %41
39: ; preds = %32
%40 = load i8*, i8** %3
store i8* %40, i8** %5
br label %41
41: ; preds = %39, %36
br label %42
42: ; preds = %41, %29
br label %43
43: ; preds = %42, %22
%44 = load i8*, i8** %5
%45 = call i8* @strchr(i8* %44, i8 signext 47)
store i8* %45, i8** %4
%46 = icmp eq i8* null, %45
br i1 %46, label %47, label %48
47: ; preds = %43
store i8* null, i8** %2
br label %57
48: ; preds = %43
%49 = load i8*, i8** %5
%50 = load i8*, i8** %4
%51 = load i8*, i8** %5
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
%55 = trunc i64 %54 to i32
%56 = call i8* @strndup(i8* %49, i32 %55)
store i8* %56, i8** %2
br label %57
57: ; preds = %48, %47, %8
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@.str = [8 x i8] c"http://\00"
@.str.1 = [9 x i8] c"https://\00"
@.str.2 = [7 x i8] c"ftp://\00"
define noalias i8* @get_host_from_link(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %29, label %3
3: ; preds = %1, %3
%4 = phi i8* [ %7, %3 ], [ %0, %1 ]
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 32
%7 = getelementptr inbounds i8, i8* %4, i64 1
br i1 %6, label %3, label %8
8: ; preds = %3
%9 = tail call i64 @strncmp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %4, i32 7)
%10 = icmp eq i64 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %8
%12 = tail call i64 @strncmp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = tail call i64 @strncmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 6)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i64 6, i64 0
br label %18
18: ; preds = %11, %8, %14
%19 = phi i64 [ %17, %14 ], [ 7, %8 ], [ 8, %11 ]
%20 = getelementptr i8, i8* %4, i64 %19
%21 = tail call i8* @strchr(i8* %20, i8 signext 47)
%22 = icmp eq i8* %21, null
br i1 %22, label %29, label %23
23: ; preds = %18
%24 = ptrtoint i8* %21 to i64
%25 = ptrtoint i8* %20 to i64
%26 = sub i64 %24, %25
%27 = trunc i64 %26 to i32
%28 = tail call i8* @strndup(i8* %20, i32 %27)
br label %29
29: ; preds = %18, %1, %23
%30 = phi i8* [ %28, %23 ], [ null, %1 ], [ null, %18 ]
ret i8* %30
}