#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 */
char *allocate_general_path(char *, char *, char *);
scalar_t__ escape_spaces(char **, int, int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *allocate_docid(char *parent, char *name) {
  char *docid = NULL;
  char *suffix = strrchr(name, '.');
  char *bare_name = NULL;
  int copied;
  if (suffix != NULL && strcmp(suffix, ".json") == 0) {
    bare_name = malloc(strlen(name) - 4);
    if (bare_name != NULL) {
      char *esc_bare_name = bare_name;
      int len = strlen(name) - 5;
      strncpy(bare_name, name, len);
      bare_name[len] = 0;
      if (escape_spaces(&esc_bare_name, 1, &copied))
        docid = allocate_general_path(parent, esc_bare_name, "%2F");
      if (copied)
        free(esc_bare_name);
      free(bare_name);
    } else
      fprintf(stderr, "utils: failed to allocate docid\n");
  } else {
    char *temp = name;
    if (escape_spaces(&temp, 0, &copied)) {
      docid = allocate_general_path(parent, temp, "%2F");
      if (copied)
        free(temp);
    }
  }
  return docid;
}


x86 assembly (clang, -O0):
<allocate_docid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12ab <allocate_docid+0x11b>
mov    -0x20(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12ab <allocate_docid+0x11b>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
sub    $0x4,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1291 <allocate_docid+0x101>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x5,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x38(%rbp),%rdi
mov    $0x1,%esi
lea    -0x2c(%rbp),%rdx
call   10b0 <escape_spaces@plt>
cmp    $0x0,%rax
je     1270 <allocate_docid+0xe0>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    0xd9f(%rip),%rdx
call   10a0 <allocate_general_path@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1283 <allocate_docid+0xf3>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    12a6 <allocate_docid+0x116>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1301 <allocate_docid+0x171>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
lea    -0x2c(%rbp),%rdx
call   10b0 <escape_spaces@plt>
cmp    $0x0,%rax
je     12fc <allocate_docid+0x16c>
mov    -0x8(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    0xd2b(%rip),%rdx
call   10a0 <allocate_general_path@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12f7 <allocate_docid+0x167>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    12fc <allocate_docid+0x16c>
jmp    1301 <allocate_docid+0x171>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_docid>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11c7 <allocate_docid+0x37>
lea    0xe46(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1214 <allocate_docid+0x84>
mov    %rbx,0x8(%rsp)
lea    0x8(%rsp),%rdi
xor    %ebx,%ebx
lea    0x14(%rsp),%rdx
xor    %esi,%esi
call   10b0 <escape_spaces@plt>
test   %rax,%rax
je     129f <allocate_docid+0x10f>
mov    0x8(%rsp),%rsi
lea    0xe12(%rip),%rdx
mov    %r15,%rdi
call   10a0 <allocate_general_path@plt>
mov    %rax,%rbx
cmpl   $0x0,0x14(%rsp)
je     129f <allocate_docid+0x10f>
mov    0x8(%rsp),%rdi
jmp    129a <allocate_docid+0x10a>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    -0x4(%rax),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     12ae <allocate_docid+0x11e>
mov    %rax,%r14
mov    %rax,0x8(%rsp)
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%r12
lea    -0x5(%r12),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strncpy@plt>
movb   $0x0,-0x5(%r14,%r12,1)
lea    0x8(%rsp),%rdi
lea    0x14(%rsp),%rdx
mov    $0x1,%esi
call   10b0 <escape_spaces@plt>
test   %rax,%rax
je     12c7 <allocate_docid+0x137>
mov    0x8(%rsp),%rsi
lea    0xd8b(%rip),%rdx
mov    %r15,%rdi
call   10a0 <allocate_general_path@plt>
mov    %rax,%rbx
cmpl   $0x0,0x14(%rsp)
je     1297 <allocate_docid+0x107>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
lea    0xd4c(%rip),%rsi
call   1080 <fprintf@plt>
xor    %ebx,%ebx
jmp    129f <allocate_docid+0x10f>
xor    %ebx,%ebx
cmpl   $0x0,0x14(%rsp)
jne    128d <allocate_docid+0xfd>
jmp    1297 <allocate_docid+0x107>

LLVM IR (-O0):
@.str = [6 x i8] c".json\00"
@.str.1 = [4 x i8] c"%2F\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"utils: failed to allocate docid\0A\00"
define i8* @allocate_docid(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8* null, i8** %5
%12 = load i8*, i8** %4
%13 = call i8* @strrchr(i8* %12, i8 signext 46)
store i8* %13, i8** %6
store i8* null, i8** %7
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %59
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %59
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %22, 4
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %55
27: ; preds = %20
%28 = load i8*, i8** %7
store i8* %28, i8** %9
%29 = load i8*, i8** %4
%30 = call i32 @strlen(i8* %29)
%31 = sub nsw i32 %30, 5
store i32 %31, i32* %10
%32 = load i8*, i8** %7
%33 = load i8*, i8** %4
%34 = load i32, i32* %10
%35 = call i32 @strncpy(i8* %32, i8* %33, i32 %34)
%36 = load i8*, i8** %7
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
%40 = call i64 @escape_spaces(i8** %9, i32 1, i32* %8)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %27
%43 = load i8*, i8** %3
%44 = load i8*, i8** %9
%45 = call i8* @allocate_general_path(i8* %43, i8* %44, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %45, i8** %5
br label %46
46: ; preds = %42, %27
%47 = load i32, i32* %8
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i8*, i8** %9
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %49, %46
%53 = load i8*, i8** %7
%54 = call i32 @free(i8* %53)
br label %58
55: ; preds = %20
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %55, %52
br label %74
59: ; preds = %16, %2
%60 = load i8*, i8** %4
store i8* %60, i8** %11
%61 = call i64 @escape_spaces(i8** %11, i32 0, i32* %8)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %73
63: ; preds = %59
%64 = load i8*, i8** %3
%65 = load i8*, i8** %11
%66 = call i8* @allocate_general_path(i8* %64, i8* %65, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %66, i8** %5
%67 = load i32, i32* %8
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %63
%70 = load i8*, i8** %11
%71 = call i32 @free(i8* %70)
br label %72
72: ; preds = %69, %63
br label %73
73: ; preds = %72, %59
br label %74
74: ; preds = %73, %58
%75 = load i8*, i8** %5
ret i8* %75
}

LLVM IR (-O2):
@.str = [6 x i8] c".json\00"
@.str.1 = [4 x i8] c"%2F\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"utils: failed to allocate docid\0A\00"
define i8* @allocate_docid(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = tail call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 46)
%7 = bitcast i32* %3 to i8*
%8 = icmp eq i8* %6, null
br i1 %8, label %41, label %9
9: ; preds = %2
%10 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %41
12: ; preds = %9
%13 = tail call i32 @strlen(i8* %1)
%14 = add nsw i32 %13, -4
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %38, label %17
17: ; preds = %12
%18 = bitcast i8** %4 to i8*
store i8* %15, i8** %4
%19 = tail call i32 @strlen(i8* %1)
%20 = add nsw i32 %19, -5
%21 = tail call i32 @strncpy(i8* %15, i8* %1, i32 %20)
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i8, i8* %15, i64 %22
store i8 0, i8* %23
%24 = call i64 @escape_spaces(i8** %4, i32 1, i32* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %17
%27 = load i8*, i8** %4
%28 = call i8* @allocate_general_path(i8* %0, i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %26, %17
%30 = phi i8* [ %28, %26 ], [ null, %17 ]
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %29
%34 = load i8*, i8** %4
%35 = call i32 @free(i8* %34)
br label %36
36: ; preds = %33, %29
%37 = call i32 @free(i8* %15)
br label %55
38: ; preds = %12
%39 = load i32, i32* @stderr
%40 = tail call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %55
41: ; preds = %9, %2
%42 = bitcast i8** %5 to i8*
store i8* %1, i8** %5
%43 = call i64 @escape_spaces(i8** %5, i32 0, i32* %3)
%44 = icmp eq i64 %43, 0
br i1 %44, label %53, label %45
45: ; preds = %41
%46 = load i8*, i8** %5
%47 = call i8* @allocate_general_path(i8* %0, i8* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* %3
%49 = icmp eq i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %45
%51 = load i8*, i8** %5
%52 = call i32 @free(i8* %51)
br label %53
53: ; preds = %45, %50, %41
%54 = phi i8* [ %47, %50 ], [ %47, %45 ], [ null, %41 ]
br label %55
55: ; preds = %36, %38, %53
%56 = phi i8* [ %30, %36 ], [ null, %38 ], [ %54, %53 ]
ret i8* %56
}