#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 *);
char *strchr(char *, char);
char *strdup(char const *);
scalar_t__ strncmp(char const *, char *, int);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ warnx(char *, char const *);
char *url2host(const char *host, short *port, char **path) {
  char *url, *ep;
  if (0 == strncmp(host, "https://", 8)) {
    *port = 443;
    if (NULL == (url = strdup(host + 8))) {
      warn("strdup");
      return (NULL);
    }
  } else if (0 == strncmp(host, "http://", 7)) {
    *port = 80;
    if (NULL == (url = strdup(host + 7))) {
      warn("strdup");
      return (NULL);
    }
  } else {
    warnx("%s: unknown schema", host);
    return (NULL);
  }
  if (NULL != (ep = strchr(url, '/'))) {
    *path = strdup(ep);
    *ep = '\0';
  } else
    *path = strdup("");
  if (NULL == *path) {
    warn("strdup");
    free(url);
    return (NULL);
  }
  return (url);
}


x86 assembly (clang, -O0):
<url2host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
mov    $0x8,%edx
call   1040 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11dd <url2host+0x7d>
mov    -0x18(%rbp),%rax
movw   $0x1bb,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x8,%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11d8 <url2host+0x78>
lea    0xe43(%rip),%rdi
call   1050 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f6 <url2host+0x196>
jmp    1268 <url2host+0x108>
mov    -0x10(%rbp),%rdi
lea    0xe28(%rip),%rsi
mov    $0x7,%edx
call   1040 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1246 <url2host+0xe6>
mov    -0x18(%rbp),%rax
movw   $0x50,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1241 <url2host+0xe1>
lea    0xdda(%rip),%rdi
call   1050 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f6 <url2host+0x196>
jmp    1263 <url2host+0x103>
mov    -0x10(%rbp),%rsi
lea    0xdc7(%rip),%rdi
call   1070 <warnx@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f6 <url2host+0x196>
jmp    1268 <url2host+0x108>
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     12a7 <url2host+0x147>
mov    -0x30(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
jmp    12bd <url2host+0x15d>
lea    0xd69(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
jne    12ee <url2host+0x18e>
lea    0xd36(%rip),%rdi
call   1050 <warn@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f6 <url2host+0x196>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url2host>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe8b(%rip),%rsi
mov    $0x8,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11b1 <url2host+0x51>
lea    0xe85(%rip),%rsi
mov    %rbx,%rdi
mov    $0x7,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11bd <url2host+0x5d>
lea    0xe74(%rip),%rdi
mov    %rbx,%rsi
call   1070 <warnx@plt>
jmp    123b <url2host+0xdb>
movw   $0x1bb,(%r15)
add    $0x8,%rbx
jmp    11c7 <url2host+0x67>
movw   $0x50,(%r15)
add    $0x7,%rbx
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1205 <url2host+0xa5>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1213 <url2host+0xb3>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <strdup@plt>
mov    %rax,(%r14)
movb   $0x0,(%r15)
mov    (%r14),%rax
test   %rax,%rax
jne    123d <url2host+0xdd>
jmp    1227 <url2host+0xc7>
lea    0xdfd(%rip),%rdi
call   1050 <warn@plt>
jmp    123b <url2host+0xdb>
lea    0xdfd(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,(%r14)
test   %rax,%rax
jne    123d <url2host+0xdd>
lea    0xddb(%rip),%rdi
call   1050 <warn@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"https://\00"
@.str.1 = [7 x i8] c"strdup\00"
@.str.2 = [8 x i8] c"http://\00"
@.str.3 = [19 x i8] c"%s: unknown schema\00"
@.str.4 = [1 x i8] zeroinitializer
define i8* @url2host(i8* %0, i16* nocapture writeonly %1, i8** nocapture %2) {
%4 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %3
store i16 443, i16* %1
%7 = getelementptr inbounds i8, i8* %0, i64 8
%8 = tail call i8* @strdup(i8* %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %23
10: ; preds = %6
%11 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %38
12: ; preds = %3
%13 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 7)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
store i16 80, i16* %1
%16 = getelementptr inbounds i8, i8* %0, i64 7
%17 = tail call i8* @strdup(i8* %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %38
21: ; preds = %12
%22 = tail call i32 @warnx(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %38
23: ; preds = %15, %6
%24 = phi i8* [ %8, %6 ], [ %17, %15 ]
%25 = tail call i8* @strchr(i8* dereferenceable(1) %24, i8 signext 47)
%26 = icmp eq i8* %25, null
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = tail call i8* @strdup(i8* %25)
store i8* %28, i8** %2
store i8 0, i8* %25
%29 = load i8*, i8** %2
br label %32
30: ; preds = %23
%31 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
store i8* %31, i8** %2
br label %32
32: ; preds = %30, %27
%33 = phi i8* [ %31, %30 ], [ %29, %27 ]
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = tail call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%37 = tail call i32 @free(i8* %24)
br label %38
38: ; preds = %32, %35, %21, %19, %10
%39 = phi i8* [ null, %10 ], [ null, %35 ], [ null, %19 ], [ null, %21 ], [ %24, %32 ]
ret i8* %39
}