#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 atoi(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *strchr(char *, char);
size_t strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, size_t);
int http_parse_url(char *url, char *host, char *file, int *port) {
  char *ptr1, *ptr2;
  int len = 0;
  if (!url || !host || !file || !port) {
    return -1;
  }
  ptr1 = url;
  if (!strncmp(ptr1, "http://", strlen("http://"))) {
    ptr1 += strlen("http://");
  } else {
    return -1;
  }
  ptr2 = strchr(ptr1, '/');
  if (ptr2) {
    len = strlen(ptr1) - strlen(ptr2);
    memcpy(host, ptr1, len);
    host[len] = '\0';
    if (*(ptr2 + 1)) {
      memcpy(file, ptr2 + 1, strlen(ptr2) - 1);
      file[strlen(ptr2) - 1] = '\0';
    }
  } else {
    memcpy(host, ptr1, strlen(ptr1));
    host[strlen(ptr1)] = '\0';
  }
  ptr1 = strchr(host, ':');
  if (ptr1) {
    *ptr1++ = '\0';
    *port = atoi(ptr1);
  } else {
    *port = 80;
  }
  return 0;
}


x86 assembly (clang, -O0):
<http_parse_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119e <http_parse_url+0x4e>
cmpq   $0x0,-0x18(%rbp)
je     119e <http_parse_url+0x4e>
cmpq   $0x0,-0x20(%rbp)
je     119e <http_parse_url+0x4e>
cmpq   $0x0,-0x28(%rbp)
jne    11aa <http_parse_url+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <http_parse_url+0x213>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    0xe3f(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rdx
lea    0xe2c(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11fb <http_parse_url+0xab>
lea    0xe17(%rip),%rdi
call   1040 <strlen@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1207 <http_parse_url+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1363 <http_parse_url+0x213>
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12c8 <http_parse_url+0x178>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movslq -0x3c(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
cmpb   $0x0,0x1(%rax)
je     12c3 <http_parse_url+0x173>
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
sub    $0x1,%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    130d <http_parse_url+0x1bd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x80(%rbp),%rdi
mov    -0x78(%rbp),%rsi
mov    %rax,%rdx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x70(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1352 <http_parse_url+0x202>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    135c <http_parse_url+0x20c>
mov    -0x28(%rbp),%rax
movl   $0x50,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_parse_url>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     126c <http_parse_url+0x11c>
mov    %rsi,%r12
test   %rsi,%rsi
je     126c <http_parse_url+0x11c>
mov    %rdx,%r15
test   %rdx,%rdx
je     126c <http_parse_url+0x11c>
mov    %rcx,%r14
test   %rcx,%rcx
je     126c <http_parse_url+0x11c>
mov    %rdi,%rbx
lea    0xe69(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    126c <http_parse_url+0x11c>
add    $0x7,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
test   %r13,%r13
je     1223 <http_parse_url+0xd3>
mov    %r13,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
movslq %ebp,%rbp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r12,%rbp,1)
cmpb   $0x0,0x1(%r13)
je     123f <http_parse_url+0xef>
mov    %r13,%rbx
add    $0x1,%rbx
mov    %r13,%rdi
call   1040 <strlen@plt>
lea    -0x1(%rax),%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
add    %r15,%rax
add    $0xffffffffffffffff,%rax
jmp    123c <http_parse_url+0xec>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %r12,%rax
movb   $0x0,(%rax)
mov    %r12,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1262 <http_parse_url+0x112>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
call   1070 <atoi@plt>
jmp    1267 <http_parse_url+0x117>
mov    $0x50,%eax
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"http://\00"
define i32 @http_parse_url(i8* %0, i8* %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32* %3, i32** %9
store i32 0, i32* %12
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %24
15: ; preds = %4
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = load i8*, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i32*, i32** %9
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %21, %18, %15, %4
store i32 -1, i32* %5
br label %99
25: ; preds = %21
%26 = load i8*, i8** %6
store i8* %26, i8** %10
%27 = load i8*, i8** %10
%28 = call i64 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @strncmp(i8* %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %25
%32 = call i64 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%33 = load i8*, i8** %10
%34 = getelementptr inbounds i8, i8* %33, i64 %32
store i8* %34, i8** %10
br label %36
35: ; preds = %25
store i32 -1, i32* %5
br label %99
36: ; preds = %31
%37 = load i8*, i8** %10
%38 = call i8* @strchr(i8* %37, i8 signext 47)
store i8* %38, i8** %11
%39 = load i8*, i8** %11
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %75
41: ; preds = %36
%42 = load i8*, i8** %10
%43 = call i64 @strlen(i8* %42)
%44 = load i8*, i8** %11
%45 = call i64 @strlen(i8* %44)
%46 = sub i64 %43, %45
%47 = trunc i64 %46 to i32
store i32 %47, i32* %12
%48 = load i8*, i8** %7
%49 = load i8*, i8** %10
%50 = load i32, i32* %12
%51 = sext i32 %50 to i64
%52 = call i32 @memcpy(i8* %48, i8* %49, i64 %51)
%53 = load i8*, i8** %7
%54 = load i32, i32* %12
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 0, i8* %56
%57 = load i8*, i8** %11
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = load i8, i8* %58
%60 = icmp ne i8 %59, 0
br i1 %60, label %61, label %74
61: ; preds = %41
%62 = load i8*, i8** %8
%63 = load i8*, i8** %11
%64 = getelementptr inbounds i8, i8* %63, i64 1
%65 = load i8*, i8** %11
%66 = call i64 @strlen(i8* %65)
%67 = sub i64 %66, 1
%68 = call i32 @memcpy(i8* %62, i8* %64, i64 %67)
%69 = load i8*, i8** %8
%70 = load i8*, i8** %11
%71 = call i64 @strlen(i8* %70)
%72 = sub i64 %71, 1
%73 = getelementptr inbounds i8, i8* %69, i64 %72
store i8 0, i8* %73
br label %74
74: ; preds = %61, %41
br label %85
75: ; preds = %36
%76 = load i8*, i8** %7
%77 = load i8*, i8** %10
%78 = load i8*, i8** %10
%79 = call i64 @strlen(i8* %78)
%80 = call i32 @memcpy(i8* %76, i8* %77, i64 %79)
%81 = load i8*, i8** %7
%82 = load i8*, i8** %10
%83 = call i64 @strlen(i8* %82)
%84 = getelementptr inbounds i8, i8* %81, i64 %83
store i8 0, i8* %84
br label %85
85: ; preds = %75, %74
%86 = load i8*, i8** %7
%87 = call i8* @strchr(i8* %86, i8 signext 58)
store i8* %87, i8** %10
%88 = load i8*, i8** %10
%89 = icmp ne i8* %88, null
br i1 %89, label %90, label %96
90: ; preds = %85
%91 = load i8*, i8** %10
%92 = getelementptr inbounds i8, i8* %91, i32 1
store i8* %92, i8** %10
store i8 0, i8* %91
%93 = load i8*, i8** %10
%94 = call i32 @atoi(i8* %93)
%95 = load i32*, i32** %9
store i32 %94, i32* %95
br label %98
96: ; preds = %85
%97 = load i32*, i32** %9
store i32 80, i32* %97
br label %98
98: ; preds = %96, %90
store i32 0, i32* %5
br label %99
99: ; preds = %98, %35, %24
%100 = load i32, i32* %5
ret i32 %100
}

LLVM IR (-O2):
@.str = [8 x i8] c"http://\00"
define i32 @http_parse_url(i8* %0, i8* %1, i8* %2, i32* writeonly %3) {
%5 = icmp ne i8* %0, null
%6 = icmp ne i8* %1, null
%7 = and i1 %5, %6
%8 = icmp ne i8* %2, null
%9 = and i1 %7, %8
%10 = icmp ne i32* %3, null
%11 = and i1 %9, %10
br i1 %11, label %12, label %51
12: ; preds = %4
%13 = tail call i32 @strncmp(i8* %0, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 7)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %51
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %0, i64 7
%17 = tail call i8* @strchr(i8* %16, i8 signext 47)
%18 = icmp eq i8* %17, null
%19 = tail call i64 @strlen(i8* %16)
br i1 %18, label %37, label %20
20: ; preds = %15
%21 = tail call i64 @strlen(i8* %17)
%22 = sub i64 %19, %21
%23 = shl i64 %22, 32
%24 = ashr exact i64 %23, 32
%25 = tail call i32 @memcpy(i8* %1, i8* %16, i64 %24)
%26 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 0, i8* %26
%27 = getelementptr inbounds i8, i8* %17, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %43, label %30
30: ; preds = %20
%31 = tail call i64 @strlen(i8* %17)
%32 = add i64 %31, -1
%33 = tail call i32 @memcpy(i8* %2, i8* %27, i64 %32)
%34 = tail call i64 @strlen(i8* %17)
%35 = add i64 %34, -1
%36 = getelementptr inbounds i8, i8* %2, i64 %35
br label %41
37: ; preds = %15
%38 = tail call i32 @memcpy(i8* %1, i8* %16, i64 %19)
%39 = tail call i64 @strlen(i8* %16)
%40 = getelementptr inbounds i8, i8* %1, i64 %39
br label %41
41: ; preds = %37, %30
%42 = phi i8* [ %36, %30 ], [ %40, %37 ]
store i8 0, i8* %42
br label %43
43: ; preds = %41, %20
%44 = tail call i8* @strchr(i8* %1, i8 signext 58)
%45 = icmp eq i8* %44, null
br i1 %45, label %49, label %46
46: ; preds = %43
%47 = getelementptr inbounds i8, i8* %44, i64 1
store i8 0, i8* %44
%48 = tail call i32 @atoi(i8* %47)
br label %49
49: ; preds = %43, %46
%50 = phi i32 [ %48, %46 ], [ 80, %43 ]
store i32 %50, i32* %3
br label %51
51: ; preds = %12, %4, %49
%52 = phi i32 [ 0, %49 ], [ -1, %4 ], [ -1, %12 ]
ret i32 %52
}