#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*/ getcwd(char *, int);
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *construct_resourceurl(char *str, int ispath) {
  char *res = 0;
  if (!ispath) {
    res = malloc(strlen(str) + 1);
    if (!res)
      return 0;
    strcpy(res, str);
  } else {
    char prefix[(512)];
    prefix[0] = '\0';
    int len = strlen(str);
    int preslash = 0;
    if (len && str[0] != '/') {
      getcwd(prefix, (512));
      preslash = 1;
    }
    int postslash = 0;
    if (len && str[len - 1] != '/')
      postslash = 1;
    res = malloc(16 + strlen(prefix) + len + 1);
    if (!res)
      return 0;
    sprintf(res, "file://%s%s%s%s", prefix, (preslash ? "/" : ""), str,
            (postslash ? "/" : ""));
  }
  return res;
}


x86 assembly (clang, -O0):
<construct_resourceurl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11b5 <construct_resourceurl+0x65>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a3 <construct_resourceurl+0x53>
movq   $0x0,-0x8(%rbp)
jmp    12df <construct_resourceurl+0x18f>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    12d7 <construct_resourceurl+0x187>
movb   $0x0,-0x220(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x224(%rbp)
movl   $0x0,-0x228(%rbp)
cmpl   $0x0,-0x224(%rbp)
je     120d <construct_resourceurl+0xbd>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     120d <construct_resourceurl+0xbd>
lea    -0x220(%rbp),%rdi
mov    $0x200,%esi
call   1050 <getcwd@plt>
movl   $0x1,-0x228(%rbp)
movl   $0x0,-0x22c(%rbp)
cmpl   $0x0,-0x224(%rbp)
je     124b <construct_resourceurl+0xfb>
mov    -0x10(%rbp),%rax
mov    -0x224(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     124b <construct_resourceurl+0xfb>
movl   $0x1,-0x22c(%rbp)
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x10,%edi
add    -0x224(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1286 <construct_resourceurl+0x136>
movq   $0x0,-0x8(%rbp)
jmp    12df <construct_resourceurl+0x18f>
mov    -0x20(%rbp),%rdi
lea    -0x220(%rbp),%rdx
mov    -0x228(%rbp),%esi
lea    0xd73(%rip),%rcx
lea    0xd6b(%rip),%rax
cmp    $0x0,%esi
cmovne %rax,%rcx
mov    -0x10(%rbp),%r8
mov    -0x22c(%rbp),%esi
lea    0xd54(%rip),%r9
lea    0xd4c(%rip),%rax
cmp    $0x0,%esi
cmovne %rax,%r9
lea    0xd2e(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<construct_resourceurl>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rdi,%r15
test   %esi,%esi
je     11b3 <construct_resourceurl+0x63>
movb   $0x0,(%rsp)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11d8 <construct_resourceurl+0x88>
lea    0xe91(%rip),%r13
cmpb   $0x2f,(%r15)
mov    %r13,%r14
je     119d <construct_resourceurl+0x4d>
mov    %rsp,%rdi
mov    $0x200,%esi
call   1050 <getcwd@plt>
lea    0xe73(%rip),%r14
movslq %ebx,%rax
cmpb   $0x2f,-0x1(%rax,%r15,1)
lea    0xe63(%rip),%r12
cmove  %r13,%r12
jmp    11e2 <construct_resourceurl+0x92>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     121a <construct_resourceurl+0xca>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
jmp    121c <construct_resourceurl+0xcc>
lea    0xe32(%rip),%r14
mov    %r14,%r12
mov    %rsp,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x11,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     121a <construct_resourceurl+0xca>
mov    %rax,%rbx
lea    0xdfc(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
mov    %r14,%rcx
mov    %r15,%r8
mov    %r12,%r9
call   1070 <sprintf@plt>
jmp    121c <construct_resourceurl+0xcc>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"file://%s%s%s%s\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @construct_resourceurl(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [512 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %6
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %13
store i8* null, i8** %3
br label %79
21: ; preds = %13
%22 = load i8*, i8** %6
%23 = load i8*, i8** %4
%24 = call i32 @strcpy(i8* %22, i8* %23)
br label %77
25: ; preds = %2
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
store i8 0, i8* %26
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* %27)
store i32 %28, i32* %8
store i32 0, i32* %9
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %25
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 47
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%39 = call i32 @getcwd(i8* %38, i32 512)
store i32 1, i32* %9
br label %40
40: ; preds = %37, %31, %25
store i32 0, i32* %10
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %40
%44 = load i8*, i8** %4
%45 = load i32, i32* %8
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %44, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 47
br i1 %51, label %52, label %53
52: ; preds = %43
store i32 1, i32* %10
br label %53
53: ; preds = %52, %43, %40
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%55 = call i32 @strlen(i8* %54)
%56 = add nsw i32 16, %55
%57 = load i32, i32* %8
%58 = add nsw i32 %56, %57
%59 = add nsw i32 %58, 1
%60 = call i8* @malloc(i32 %59)
store i8* %60, i8** %6
%61 = load i8*, i8** %6
%62 = icmp ne i8* %61, null
br i1 %62, label %64, label %63
63: ; preds = %53
store i8* null, i8** %3
br label %79
64: ; preds = %53
%65 = load i8*, i8** %6
%66 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%67 = load i32, i32* %9
%68 = icmp ne i32 %67, 0
%69 = zext i1 %68 to i64
%70 = select i1 %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%71 = load i8*, i8** %4
%72 = load i32, i32* %10
%73 = icmp ne i32 %72, 0
%74 = zext i1 %73 to i64
%75 = select i1 %73, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%76 = call i32 @sprintf(i8* %65, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %66, i8* %70, i8* %71, i8* %75)
br label %77
77: ; preds = %64, %21
%78 = load i8*, i8** %6
store i8* %78, i8** %3
br label %79
79: ; preds = %77, %63, %20
%80 = load i8*, i8** %3
ret i8* %80
}

LLVM IR (-O2):
@.str = [16 x i8] c"file://%s%s%s%s\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @construct_resourceurl(i8* %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = icmp eq i32 %1, 0
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
%7 = add nsw i32 %6, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %40, label %10
10: ; preds = %5
%11 = tail call i32 @strcpy(i8* %8, i8* %0)
br label %40
12: ; preds = %2
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
store i8 0, i8* %13
%14 = tail call i32 @strlen(i8* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %29, label %16
16: ; preds = %12
%17 = load i8, i8* %0
%18 = icmp eq i8 %17, 47
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @getcwd(i8* %13, i32 512)
br label %21
21: ; preds = %19, %16
%22 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %16 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %19 ]
%23 = add nsw i32 %14, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 47
%28 = select i1 %27, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
br label %29
29: ; preds = %12, %21
%30 = phi i8* [ %22, %21 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %12 ]
%31 = phi i8* [ %28, %21 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %12 ]
%32 = call i32 @strlen(i8* %13)
%33 = add i32 %14, 17
%34 = add i32 %33, %32
%35 = call i8* @malloc(i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %29
%38 = call i32 @sprintf(i8* %35, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %30, i8* %0, i8* %31)
br label %40
39: ; preds = %29
br label %40
40: ; preds = %10, %37, %39, %5
%41 = phi i8* [ null, %5 ], [ null, %39 ], [ %35, %37 ], [ %8, %10 ]
ret i8* %41
}