#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strchr(char *, char);
int strlen(char *);
char *strstr(char *, char *);
void get_resource_uri(char *URI, char *resourceURI, int length) {
  char *startPos = URI;
  char *temp = NULL;
  if (NULL != (temp = strstr(URI, "://"))) {
    startPos = strchr(temp + 3, '/');
    if (!startPos) {
      printf("Resource URI is missing\n");
      return;
    }
  }
  char *endPos = strchr(startPos, '?');
  if (!endPos) {
    endPos = URI + strlen(URI);
  }
  endPos -= 1;
  if (endPos - startPos <= length) {
    memcpy(resourceURI, startPos + 1, endPos - startPos);
  }
  printf("URI: %s, ResourceURI:%s\n", URI, resourceURI);
}


x86 assembly (clang, -O0):
<get_resource_uri>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     11ce <get_resource_uri+0x7e>
mov    -0x28(%rbp),%rdi
add    $0x3,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <get_resource_uri+0x79>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1264 <get_resource_uri+0x114>
jmp    11ce <get_resource_uri+0x7e>
mov    -0x20(%rbp),%rdi
mov    $0x3f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120c <get_resource_uri+0xbc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jg     124e <get_resource_uri+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_resource_uri>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe89(%rip),%rsi
call   1080 <strstr@plt>
mov    %rbp,%rbx
test   %rax,%rax
je     119d <get_resource_uri+0x3d>
add    $0x3,%rax
mov    %rax,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11fa <get_resource_uri+0x9a>
mov    %rbx,%rdi
mov    $0x3f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
jne    11bc <get_resource_uri+0x5c>
mov    %rbp,%rdi
call   1040 <strlen@plt>
cltq
add    %rbp,%rax
mov    %rbx,%rdx
not    %rdx
add    %rax,%rdx
movslq %r15d,%rax
cmp    %rax,%rdx
jg     11dc <get_resource_uri+0x7c>
add    $0x1,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <memcpy@plt>
lea    0xe21(%rip),%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>
lea    0xe1c(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"://\00"
@.str.1 = [25 x i8] c"Resource URI is missing\0A\00"
@.str.2 = [25 x i8] c"URI: %s, ResourceURI:%s\0A\00"
define void @get_resource_uri(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
store i8* %10, i8** %7
store i8* null, i8** %8
%11 = load i8*, i8** %4
%12 = call i8* @strstr(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %8
%13 = icmp ne i8* null, %12
br i1 %13, label %14, label %23
14: ; preds = %3
%15 = load i8*, i8** %8
%16 = getelementptr inbounds i8, i8* %15, i64 3
%17 = call i8* @strchr(i8* %16, i8 signext 47)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %60
22: ; preds = %14
br label %23
23: ; preds = %22, %3
%24 = load i8*, i8** %7
%25 = call i8* @strchr(i8* %24, i8 signext 63)
store i8* %25, i8** %9
%26 = load i8*, i8** %9
%27 = icmp ne i8* %26, null
br i1 %27, label %34, label %28
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = load i8*, i8** %4
%31 = call i32 @strlen(i8* %30)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8* %33, i8** %9
br label %34
34: ; preds = %28, %23
%35 = load i8*, i8** %9
%36 = getelementptr inbounds i8, i8* %35, i64 -1
store i8* %36, i8** %9
%37 = load i8*, i8** %9
%38 = load i8*, i8** %7
%39 = ptrtoint i8* %37 to i64
%40 = ptrtoint i8* %38 to i64
%41 = sub i64 %39, %40
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = icmp sle i64 %41, %43
br i1 %44, label %45, label %56
45: ; preds = %34
%46 = load i8*, i8** %5
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = load i8*, i8** %9
%50 = load i8*, i8** %7
%51 = ptrtoint i8* %49 to i64
%52 = ptrtoint i8* %50 to i64
%53 = sub i64 %51, %52
%54 = trunc i64 %53 to i32
%55 = call i32 @memcpy(i8* %46, i8* %48, i32 %54)
br label %56
56: ; preds = %45, %34
%57 = load i8*, i8** %4
%58 = load i8*, i8** %5
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %57, i8* %58)
br label %60
60: ; preds = %56, %20
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"://\00"
@.str.2 = [25 x i8] c"URI: %s, ResourceURI:%s\0A\00"
@str = [24 x i8] c"Resource URI is missing\00"
define void @get_resource_uri(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %4, i64 3
%8 = tail call i8* @strchr(i8* %7, i8 signext 47)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %34
12: ; preds = %6, %3
%13 = phi i8* [ %8, %6 ], [ %0, %3 ]
%14 = tail call i8* @strchr(i8* dereferenceable(1) %13, i8 signext 63)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = tail call i32 @strlen(i8* %0)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %0, i64 %18
br label %20
20: ; preds = %16, %12
%21 = phi i8* [ %14, %12 ], [ %19, %16 ]
%22 = getelementptr inbounds i8, i8* %21, i64 -1
%23 = ptrtoint i8* %22 to i64
%24 = ptrtoint i8* %13 to i64
%25 = sub i64 %23, %24
%26 = sext i32 %2 to i64
%27 = icmp sgt i64 %25, %26
br i1 %27, label %32, label %28
28: ; preds = %20
%29 = getelementptr inbounds i8, i8* %13, i64 1
%30 = trunc i64 %25 to i32
%31 = tail call i32 @memcpy(i8* %1, i8* %29, i32 %30)
br label %32
32: ; preds = %28, %20
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
br label %34
34: ; preds = %32, %10
ret void
}