#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__ memcmp(char const *, char *, int);
int /*<<< orphan*/ set_last_error(char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int archive_filename_to_resource(const char *name, char *result,
                                 size_t maxlength) {
  if (maxlength < strlen(name) + 2)
    do {
      set_last_error("Bad path found in archive");
      return -1;
    } while (0);
  if (memcmp(name, "data/", 5) == 0) {
    strcpy(result, &name[5]);
  } else {
    result[0] = '/';
    strcpy(&result[1], name);
  }
  return 0;
}


x86 assembly (clang, -O0):
<archive_filename_to_resource>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
add    $0x2,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    1197 <archive_filename_to_resource+0x57>
jmp    117f <archive_filename_to_resource+0x3f>
lea    0xe7a(%rip),%rdi
call   1050 <set_last_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <archive_filename_to_resource+0xab>
mov    -0x10(%rbp),%rdi
lea    0xe78(%rip),%rsi
mov    $0x5,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
jne    11cc <archive_filename_to_resource+0x8c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x5,%rsi
call   1030 <strcpy@plt>
jmp    11e4 <archive_filename_to_resource+0xa4>
mov    -0x18(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<archive_filename_to_resource>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
add    $0x2,%eax
cltq
cmp    %rbx,%rax
jbe    1170 <archive_filename_to_resource+0x30>
lea    0xe9c(%rip),%rdi
call   1050 <set_last_error@plt>
mov    $0xffffffff,%eax
jmp    11a4 <archive_filename_to_resource+0x64>
lea    0xea3(%rip),%rsi
mov    %r15,%rdi
mov    $0x5,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     1193 <archive_filename_to_resource+0x53>
movb   $0x2f,(%r14)
add    $0x1,%r14
jmp    1197 <archive_filename_to_resource+0x57>
add    $0x5,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Bad path found in archive\00"
@.str.1 = [6 x i8] c"data/\00"
define i32 @archive_filename_to_resource(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%8 = load i64, i64* %7
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 2
%12 = sext i32 %11 to i64
%13 = icmp ult i64 %8, %12
br i1 %13, label %14, label %18
14: ; preds = %3
br label %15
15: ; preds = %14
%16 = call i32 @set_last_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %35
17: ; No predecessors!
br label %18
18: ; preds = %17, %3
%19 = load i8*, i8** %5
%20 = call i64 @memcmp(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 5
%26 = call i32 @strcpy(i8* %23, i8* %25)
br label %34
27: ; preds = %18
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 47, i8* %29
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = load i8*, i8** %5
%33 = call i32 @strcpy(i8* %31, i8* %32)
br label %34
34: ; preds = %27, %22
store i32 0, i32* %4
br label %35
35: ; preds = %34, %15
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [26 x i8] c"Bad path found in archive\00"
@.str.1 = [6 x i8] c"data/\00"
define i32 @archive_filename_to_resource(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = add nsw i32 %4, 2
%6 = sext i32 %5 to i64
%7 = icmp ugt i64 %6, %2
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @set_last_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %19
10: ; preds = %3
%11 = tail call i64 @memcmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %0, i64 5
%15 = tail call i32 @strcpy(i8* %1, i8* %14)
br label %19
16: ; preds = %10
store i8 47, i8* %1
%17 = getelementptr inbounds i8, i8* %1, i64 1
%18 = tail call i32 @strcpy(i8* %17, i8* %0)
br label %19
19: ; preds = %13, %16, %8
%20 = phi i32 [ -1, %8 ], [ 0, %16 ], [ 0, %13 ]
ret i32 %20
}