#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*/ free(char *);
scalar_t__ is_number(char *);
char *strdup(char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok(char *, char *);
void parse_url(char *url, char *host, int limit, int *port) {
  char *url_copy = strdup(url);
  if (url_copy != NULL) {
    char *token = strtok(url_copy, "/:");
    *port = 80;
    while (token != NULL) {
      if (strlen(token) > 0) {
        if (is_number(token))
          *port = atoi(token);
        else
          strncpy(host, token, limit);
      }
      token = strtok(NULL, "/:");
    }
    free(url_copy);
  }
}


x86 assembly (clang, -O0):
<parse_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     123b <parse_url+0xcb>
mov    -0x28(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x50,(%rax)
cmpq   $0x0,-0x30(%rbp)
je     1232 <parse_url+0xc2>
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x0,%rax
jle    1219 <parse_url+0xa9>
mov    -0x30(%rbp),%rdi
call   1050 <is_number@plt>
cmp    $0x0,%rax
je     1204 <parse_url+0x94>
mov    -0x30(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1214 <parse_url+0xa4>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <strncpy@plt>
jmp    1219 <parse_url+0xa9>
xor    %eax,%eax
mov    %eax,%edi
lea    0xddc(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    11bd <parse_url+0x4d>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_url>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r13
mov    %edx,%r14d
mov    %rsi,%r12
call   1090 <strdup@plt>
test   %rax,%rax
je     121e <parse_url+0xae>
mov    %rax,%r15
lea    0xe64(%rip),%rsi
mov    %rax,%rdi
call   1070 <strtok@plt>
movl   $0x50,0x0(%r13)
test   %rax,%rax
je     1208 <parse_url+0x98>
mov    %rax,%rbx
lea    0xe45(%rip),%rbp
jmp    11de <parse_url+0x6e>
nopl   (%rax)
mov    %rbx,%rdi
call   1080 <atoi@plt>
mov    %eax,0x0(%r13)
xor    %edi,%edi
mov    %rbp,%rsi
call   1070 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1208 <parse_url+0x98>
mov    %rbx,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
jle    11cc <parse_url+0x5c>
mov    %rbx,%rdi
call   1050 <is_number@plt>
test   %rax,%rax
jne    11c0 <parse_url+0x50>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <strncpy@plt>
jmp    11cc <parse_url+0x5c>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"/:\00"
define void @parse_url(i8* %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%11 = load i8*, i8** %5
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %45
15: ; preds = %4
%16 = load i8*, i8** %9
%17 = call i8* @strtok(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %10
%18 = load i32*, i32** %8
store i32 80, i32* %18
br label %19
19: ; preds = %40, %15
%20 = load i8*, i8** %10
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %42
22: ; preds = %19
%23 = load i8*, i8** %10
%24 = call i64 @strlen(i8* %23)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %22
%27 = load i8*, i8** %10
%28 = call i64 @is_number(i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i8*, i8** %10
%32 = call i32 @atoi(i8* %31)
%33 = load i32*, i32** %8
store i32 %32, i32* %33
br label %39
34: ; preds = %26
%35 = load i8*, i8** %6
%36 = load i8*, i8** %10
%37 = load i32, i32* %7
%38 = call i32 @strncpy(i8* %35, i8* %36, i32 %37)
br label %39
39: ; preds = %34, %30
br label %40
40: ; preds = %39, %22
%41 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %41, i8** %10
br label %19
42: ; preds = %19
%43 = load i8*, i8** %9
%44 = call i32 @free(i8* %43)
br label %45
45: ; preds = %42, %4
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"/:\00"
define void @parse_url(i8* nocapture %0, i8* %1, i32 %2, i32* nocapture writeonly %3) {
%5 = tail call i8* @strdup(i8* %0)
%6 = icmp eq i8* %5, null
br i1 %6, label %26, label %7
7: ; preds = %4
%8 = tail call i8* @strtok(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 80, i32* %3
%9 = icmp eq i8* %8, null
br i1 %9, label %24, label %10
10: ; preds = %7, %21
%11 = phi i8* [ %22, %21 ], [ %8, %7 ]
%12 = tail call i64 @strlen(i8* %11)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = tail call i64 @is_number(i8* %11)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @atoi(i8* %11)
store i32 %18, i32* %3
br label %21
19: ; preds = %14
%20 = tail call i32 @strncpy(i8* %1, i8* %11, i32 %2)
br label %21
21: ; preds = %17, %19, %10
%22 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %10
24: ; preds = %21, %7
%25 = tail call i32 @free(i8* %5)
br label %26
26: ; preds = %24, %4
ret void
}