#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 */
scalar_t__ isalnum(char);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
char *strdup(char const *);
scalar_t__ strlen(char const *);
scalar_t__ strncasecmp(char const *, char *, int);
char *lm_strtourl(const char *str) {
  int extra_size;
  const char *prefix;
  char *out;
  char *s = (char *)str;
  while (isalnum(*s))
    s++;
  if (*s == ':')
    return strdup(str);
  if (*str == '/' || (*str == '.' && *(str + 1) == '/')) {
    extra_size = 7;
    prefix = "file://";
  } else if (strncasecmp(str, "ftp.", 4) == 0) {
    extra_size = 6;
    prefix = "ftp://";
  } else {
    extra_size = 7;
    prefix = "http://";
  }
  if (!(out = malloc(strlen(str) + 1 + extra_size)))
    return 0;
  sprintf(out, "%s%s", prefix, str);
  return out;
}


x86 assembly (clang, -O0):
<lm_strtourl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
je     119b <lm_strtourl+0x3b>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1174 <lm_strtourl+0x14>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    11bd <lm_strtourl+0x5d>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    12a9 <lm_strtourl+0x149>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11ee <lm_strtourl+0x8e>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1205 <lm_strtourl+0xa5>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    1205 <lm_strtourl+0xa5>
movl   $0x7,-0x14(%rbp)
lea    0xe04(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1252 <lm_strtourl+0xf2>
mov    -0x10(%rbp),%rdi
lea    0xdf8(%rip),%rsi
mov    $0x4,%edx
call   1060 <strncasecmp@plt>
cmp    $0x0,%rax
jne    123b <lm_strtourl+0xdb>
movl   $0x6,-0x14(%rbp)
lea    0xddb(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    124d <lm_strtourl+0xed>
movl   $0x7,-0x14(%rbp)
lea    0xdcb(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1252 <lm_strtourl+0xf2>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1289 <lm_strtourl+0x129>
movq   $0x0,-0x8(%rbp)
jmp    12a9 <lm_strtourl+0x149>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd80(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lm_strtourl>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
mov    %rdi,%rbx
nopl   0x0(%rax,%rax,1)
movsbl (%rbx),%edi
call   1040 <isalnum@plt>
add    $0x1,%rbx
test   %rax,%rax
jne    1170 <lm_strtourl+0x10>
cmpb   $0x3a,-0x1(%rbx)
jne    1194 <lm_strtourl+0x34>
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <strdup@plt>
mov    (%r15),%al
lea    0xe62(%rip),%r14
mov    $0x8,%ebx
cmp    $0x2f,%al
je     11e8 <lm_strtourl+0x88>
cmp    $0x2e,%al
jne    11b2 <lm_strtourl+0x52>
cmpb   $0x2f,0x1(%r15)
je     11e8 <lm_strtourl+0x88>
lea    0xe4f(%rip),%rsi
mov    %r15,%rdi
mov    $0x4,%edx
call   1060 <strncasecmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    0xe38(%rip),%rax
lea    0xe38(%rip),%r14
cmove  %rax,%r14
mov    $0x8,%ebx
sub    %rcx,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     121a <lm_strtourl+0xba>
mov    %rax,%rbx
lea    0xe12(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %r15,%rcx
call   1070 <sprintf@plt>
jmp    121c <lm_strtourl+0xbc>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"file://\00"
@.str.1 = [5 x i8] c"ftp.\00"
@.str.2 = [7 x i8] c"ftp://\00"
@.str.3 = [8 x i8] c"http://\00"
@.str.4 = [5 x i8] c"%s%s\00"
define noalias i8* @lm_strtourl(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i8* [ %0, %1 ], [ %7, %2 ]
%4 = load i8, i8* %3
%5 = tail call i64 @isalnum(i8 signext %4)
%6 = icmp eq i64 %5, 0
%7 = getelementptr inbounds i8, i8* %3, i64 1
br i1 %6, label %8, label %2
8: ; preds = %2
%9 = load i8, i8* %3
%10 = icmp eq i8 %9, 58
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i8* @strdup(i8* %0)
br label %33
13: ; preds = %8
%14 = load i8, i8* %0
switch i8 %14, label %19 [
i8 47, label %24
i8 46, label %15
]
15: ; preds = %13
%16 = getelementptr inbounds i8, i8* %0, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 47
br i1 %18, label %24, label %19
19: ; preds = %13, %15
%20 = tail call i64 @strncasecmp(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0)
%23 = select i1 %21, i64 7, i64 8
br label %24
24: ; preds = %19, %15, %13
%25 = phi i64 [ 8, %13 ], [ 8, %15 ], [ %23, %19 ]
%26 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %15 ], [ %22, %19 ]
%27 = tail call i64 @strlen(i8* %0)
%28 = add i64 %25, %27
%29 = tail call i8* @malloc(i64 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %33, label %31
31: ; preds = %24
%32 = tail call i32 @sprintf(i8* %29, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %26, i8* %0)
br label %33
33: ; preds = %24, %31, %11
%34 = phi i8* [ %12, %11 ], [ %29, %31 ], [ null, %24 ]
ret i8* %34
}