#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 const);
size_t strlen(char const *);
scalar_t__ strncasecmp(char const *, char const *, size_t);
int is_safe_link(const char *link, size_t link_len) {
  static const size_t valid_uris_count = 4;
  static const char *valid_uris[] = {"http://", "https://", "ftp://",
                                     "mailto://"};
  size_t i;
  for (i = 0; i < valid_uris_count; ++i) {
    size_t len = strlen(valid_uris[i]);
    if (link_len > len && strncasecmp(link, valid_uris[i], len) == 0 &&
        isalnum(link[len]))
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<is_safe_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x4,-0x20(%rbp)
jae    11dc <is_safe_link+0xac>
mov    -0x20(%rbp),%rcx
lea    0x2ee2(%rip),%rax
mov    (%rax,%rcx,8),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    11c6 <is_safe_link+0x96>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rcx
lea    0x2eb8(%rip),%rax
mov    (%rax,%rcx,8),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <strncasecmp@plt>
cmp    $0x0,%rax
jne    11c6 <is_safe_link+0x96>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
je     11c6 <is_safe_link+0x96>
movl   $0x1,-0x4(%rbp)
jmp    11e3 <is_safe_link+0xb3>
jmp    11cb <is_safe_link+0x9b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1148 <is_safe_link+0x18>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_safe_link>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
cmp    $0x8,%rsi
jb     1185 <is_safe_link+0x65>
lea    0xec9(%rip),%rsi
mov    $0x7,%edx
mov    %r14,%rdi
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     11e6 <is_safe_link+0xc6>
cmp    $0x9,%rbx
jb     118b <is_safe_link+0x6b>
lea    0xeae(%rip),%rsi
mov    $0x8,%edx
mov    %r14,%rdi
call   1040 <strncasecmp@plt>
test   %rax,%rax
jne    118b <is_safe_link+0x6b>
movsbl 0x8(%r14),%edi
call   1030 <isalnum@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11c5 <is_safe_link+0xa5>
jmp    118b <is_safe_link+0x6b>
cmp    $0x7,%rbx
jne    11c3 <is_safe_link+0xa3>
lea    0xe7f(%rip),%rsi
mov    $0x6,%edx
mov    %r14,%rdi
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     11cd <is_safe_link+0xad>
cmp    $0xa,%rbx
jb     11c3 <is_safe_link+0xa3>
lea    0xe67(%rip),%rsi
mov    $0x9,%edx
mov    %r14,%rdi
call   1040 <strncasecmp@plt>
test   %rax,%rax
je     1202 <is_safe_link+0xe2>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
movsbl 0x6(%r14),%edi
call   1030 <isalnum@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11c5 <is_safe_link+0xa5>
jmp    11a4 <is_safe_link+0x84>
movsbl 0x7(%r14),%edi
call   1030 <isalnum@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11c5 <is_safe_link+0xa5>
jmp    114d <is_safe_link+0x2d>
movsbl 0x9(%r14),%edi
call   1030 <isalnum@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11c5 <is_safe_link+0xa5>
jmp    11c3 <is_safe_link+0xa3>

LLVM IR (-O0):
@is_safe_link.valid_uris_count = internal constant i64 4
@is_safe_link.valid_uris = internal global [4 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i32 0, i32 0)]
@.str = [8 x i8] c"http://\00"
@.str.1 = [9 x i8] c"https://\00"
@.str.2 = [7 x i8] c"ftp://\00"
@.str.3 = [10 x i8] c"mailto://\00"
define i32 @is_safe_link(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i64 0, i64* %6
br label %8
8: ; preds = %36, %2
%9 = load i64, i64* %6
%10 = icmp ult i64 %9, 4
br i1 %10, label %11, label %39
11: ; preds = %8
%12 = load i64, i64* %6
%13 = getelementptr inbounds [4 x i8*], [4 x i8*]* @is_safe_link.valid_uris, i64 0, i64 %12
%14 = load i8*, i8** %13
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %7
%16 = load i64, i64* %5
%17 = load i64, i64* %7
%18 = icmp ugt i64 %16, %17
br i1 %18, label %19, label %35
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = load i64, i64* %6
%22 = getelementptr inbounds [4 x i8*], [4 x i8*]* @is_safe_link.valid_uris, i64 0, i64 %21
%23 = load i8*, i8** %22
%24 = load i64, i64* %7
%25 = call i64 @strncasecmp(i8* %20, i8* %23, i64 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %19
%28 = load i8*, i8** %4
%29 = load i64, i64* %7
%30 = getelementptr inbounds i8, i8* %28, i64 %29
%31 = load i8, i8* %30
%32 = call i64 @isalnum(i8 signext %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 1, i32* %3
br label %40
35: ; preds = %27, %19, %11
br label %36
36: ; preds = %35
%37 = load i64, i64* %6
%38 = add i64 %37, 1
store i64 %38, i64* %6
br label %8
39: ; preds = %8
store i32 0, i32* %3
br label %40
40: ; preds = %39, %34
%41 = load i32, i32* %3
ret i32 %41
}

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"
@.str.3 = [10 x i8] c"mailto://\00"
define i32 @is_safe_link(i8* nocapture %0, i64 %1) {
%3 = icmp ugt i64 %1, 7
br i1 %3, label %4, label %22
4: ; preds = %2
%5 = tail call i64 @strncasecmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 7)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %0, i64 7
%9 = load i8, i8* %8
%10 = tail call i64 @isalnum(i8 signext %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %4, %7
%13 = icmp ugt i64 %1, 8
br i1 %13, label %14, label %24
14: ; preds = %12
%15 = tail call i64 @strncasecmp(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %0, i64 8
%19 = load i8, i8* %18
%20 = tail call i64 @isalnum(i8 signext %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %43
22: ; preds = %2
%23 = icmp eq i64 %1, 7
br i1 %23, label %24, label %42
24: ; preds = %12, %14, %17, %22
%25 = tail call i64 @strncasecmp(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64 6)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = getelementptr inbounds i8, i8* %0, i64 6
%29 = load i8, i8* %28
%30 = tail call i64 @isalnum(i8 signext %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %27, %24
%33 = icmp ugt i64 %1, 9
br i1 %33, label %34, label %42
34: ; preds = %32
%35 = tail call i64 @strncasecmp(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 9)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = getelementptr inbounds i8, i8* %0, i64 9
%39 = load i8, i8* %38
%40 = tail call i64 @isalnum(i8 signext %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %22, %37, %34, %32
br label %43
43: ; preds = %42, %37, %27, %17, %7
%44 = phi i32 [ 1, %7 ], [ 1, %17 ], [ 1, %27 ], [ 1, %37 ], [ 0, %42 ]
ret i32 %44
}