#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 */
typedef int uint8_t;
typedef int uint64_t;
typedef int int8_t;
typedef int /*<<< orphan*/ fmt_str;

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char *, int);
int snprintf(char *, int, char *, ...);
int strlen(char *);
int uint64t_strlen(int);
int chunk_relpath(uint64_t chunk_index, uint64_t chunks_per_subdir,
                  int max_chunk_index, char *out, int out_size) {
  int retval;
  int8_t dir_strlen = uint64t_strlen(chunks_per_subdir - 1);
  if (dir_strlen < 1)
    return -1;
  int8_t chunk_strlen = uint64t_strlen(max_chunk_index);
  if (chunk_strlen < 1)
    return -1;
  if (chunk_strlen % dir_strlen > 0)
    chunk_strlen += dir_strlen - (chunk_strlen % dir_strlen);
  retval = snprintf(out, out_size, "chunks/");
  if (retval < 0)
    return -1;
  char *chunk_index_substr = out + strlen(out);
  uint8_t subdir_count;
  uint8_t subdirs_strlen;
  if (max_chunk_index >= chunks_per_subdir) {
    subdir_count = (chunk_strlen / dir_strlen) - 1;
    subdirs_strlen = subdir_count * (dir_strlen + strlen("/"));
    chunk_index_substr += subdirs_strlen;
  }
  char fmt_str[8 + 1];
  retval = snprintf(fmt_str, sizeof(fmt_str), "%%0.%illu", chunk_strlen);
  if (retval < 0)
    return -1;
  int chunk_index_substr_buf_len = out_size - strlen(out);
  retval = snprintf(chunk_index_substr, chunk_index_substr_buf_len, fmt_str,
                    chunk_index);
  if (retval < 0)
    return -1;
  if (max_chunk_index >= chunks_per_subdir) {
    char *dir_substr = out + strlen(out);
    char *c_substr = chunk_index_substr;
    for (int i = 0; i < subdir_count; i++) {
      memcpy(dir_substr, c_substr, dir_strlen);
      dir_substr += dir_strlen;
      c_substr += dir_strlen;
      memcpy(dir_substr, "/", 1);
      dir_substr += 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<chunk_relpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
sub    $0x1,%edi
call   1060 <uint64t_strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    1180 <chunk_relpath+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1399 <chunk_relpath+0x259>
mov    -0x10(%rbp),%edi
call   1060 <uint64t_strlen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jge    11a1 <chunk_relpath+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1399 <chunk_relpath+0x259>
mov    -0x28(%rbp),%eax
cltd
idivl  -0x24(%rbp)
cmp    $0x0,%edx
jle    11c9 <chunk_relpath+0x89>
mov    -0x24(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x28(%rbp),%eax
cltd
idivl  -0x24(%rbp)
mov    -0x60(%rbp),%eax
sub    %edx,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    0xe29(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11f7 <chunk_relpath+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1399 <chunk_relpath+0x259>
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     126a <chunk_relpath+0x12a>
mov    -0x28(%rbp),%eax
cltd
idivl  -0x24(%rbp)
sub    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x70(%rbp)
lea    0xdc2(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x70(%rbp),%ecx
mov    %eax,%edx
mov    -0x6c(%rbp),%eax
add    %edx,%ecx
imul   %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
lea    -0x41(%rbp),%rdi
mov    -0x28(%rbp),%ecx
mov    $0x9,%esi
lea    0xd8b(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    129d <chunk_relpath+0x15d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1399 <chunk_relpath+0x259>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x74(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%esi
lea    -0x41(%rbp),%rdx
mov    -0x8(%rbp),%ecx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12e4 <chunk_relpath+0x1a4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1399 <chunk_relpath+0x259>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1392 <chunk_relpath+0x252>
mov    -0x18(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x80(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    138d <chunk_relpath+0x24d>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
lea    0xc9d(%rip),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1320 <chunk_relpath+0x1e0>
jmp    1392 <chunk_relpath+0x252>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chunk_relpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,0x8(%rsp)
mov    %rcx,%rbx
mov    %edx,%r14d
mov    %esi,%r12d
mov    %edi,%r13d
lea    -0x1(%r12),%edi
call   1060 <uint64t_strlen@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
jle    12c7 <chunk_relpath+0x187>
mov    %eax,%ebp
mov    %r13d,0xc(%rsp)
mov    %r14d,%edi
call   1060 <uint64t_strlen@plt>
test   %eax,%eax
jle    12c7 <chunk_relpath+0x187>
mov    %eax,%r13d
mov    %r12,0x10(%rsp)
mov    %r14d,0x4(%rsp)
mov    %ebp,%r14d
xor    %edx,%edx
div    %r14d
mov    %r14d,%r12d
sub    %edx,%r12d
test   %edx,%edx
cmove  %edx,%r12d
lea    0xe4a(%rip),%rdx
mov    %rbx,%rdi
mov    0x8(%rsp),%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
test   %eax,%eax
js     12c7 <chunk_relpath+0x187>
add    %r13d,%r12d
mov    %rbx,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    0x10(%rsp),%rax
cmp    %eax,0x4(%rsp)
jge    11ea <chunk_relpath+0xaa>
jmp    1210 <chunk_relpath+0xd0>
mov    %r12d,%eax
cltd
idiv   %r14d
mov    %eax,%r13d
add    $0xffffffff,%r13d
lea    0xe07(%rip),%rdi
call   1030 <strlen@plt>
add    %r14d,%eax
imul   %r13d,%eax
cltq
add    %rax,%rbp
lea    0xdf1(%rip),%rdx
lea    0x1f(%rsp),%rdi
mov    $0x9,%esi
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
test   %eax,%eax
js     12c1 <chunk_relpath+0x181>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    0x8(%rsp),%esi
sub    %eax,%esi
lea    0x1f(%rsp),%rdx
mov    %rbp,%rdi
mov    0xc(%rsp),%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    %eax,%r15d
sar    $0x1f,%r15d
mov    0x10(%rsp),%rcx
cmp    %ecx,0x4(%rsp)
jl     12c7 <chunk_relpath+0x187>
test   %eax,%eax
js     12c7 <chunk_relpath+0x187>
mov    %rbx,%rdi
call   1030 <strlen@plt>
xor    %r15d,%r15d
test   %r13d,%r13d
jle    12c7 <chunk_relpath+0x187>
cltq
add    %rax,%rbx
lea    0xd80(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1050 <memcpy@plt>
lea    (%rbx,%r14,1),%rdi
add    %r14,%rbp
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
add    %r14,%rbx
add    $0x1,%rbx
add    $0xffffffff,%r13d
jne    1290 <chunk_relpath+0x150>
jmp    12c7 <chunk_relpath+0x187>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"chunks/\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [10 x i8] c"%%0.%illu\00"
define i32 @chunk_relpath(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
%18 = alloca [9 x i8]
%19 = alloca i32
%20 = alloca i8*
%21 = alloca i8*
%22 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%23 = load i32, i32* %8
%24 = sub nsw i32 %23, 1
%25 = call i32 @uint64t_strlen(i32 %24)
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %29
28: ; preds = %5
store i32 -1, i32* %6
br label %135
29: ; preds = %5
%30 = load i32, i32* %9
%31 = call i32 @uint64t_strlen(i32 %30)
store i32 %31, i32* %14
%32 = load i32, i32* %14
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -1, i32* %6
br label %135
35: ; preds = %29
%36 = load i32, i32* %14
%37 = load i32, i32* %13
%38 = srem i32 %36, %37
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %35
%41 = load i32, i32* %13
%42 = load i32, i32* %14
%43 = load i32, i32* %13
%44 = srem i32 %42, %43
%45 = sub nsw i32 %41, %44
%46 = load i32, i32* %14
%47 = add nsw i32 %46, %45
store i32 %47, i32* %14
br label %48
48: ; preds = %40, %35
%49 = load i8*, i8** %10
%50 = load i32, i32* %11
%51 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %49, i32 %50, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %48
store i32 -1, i32* %6
br label %135
55: ; preds = %48
%56 = load i8*, i8** %10
%57 = load i8*, i8** %10
%58 = call i32 @strlen(i8* %57)
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %56, i64 %59
store i8* %60, i8** %15
%61 = load i32, i32* %9
%62 = load i32, i32* %8
%63 = icmp sge i32 %61, %62
br i1 %63, label %64, label %78
64: ; preds = %55
%65 = load i32, i32* %14
%66 = load i32, i32* %13
%67 = sdiv i32 %65, %66
%68 = sub nsw i32 %67, 1
store i32 %68, i32* %16
%69 = load i32, i32* %16
%70 = load i32, i32* %13
%71 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%72 = add nsw i32 %70, %71
%73 = mul nsw i32 %69, %72
store i32 %73, i32* %17
%74 = load i32, i32* %17
%75 = load i8*, i8** %15
%76 = sext i32 %74 to i64
%77 = getelementptr inbounds i8, i8* %75, i64 %76
store i8* %77, i8** %15
br label %78
78: ; preds = %64, %55
%79 = getelementptr inbounds [9 x i8], [9 x i8]* %18, i64 0, i64 0
%80 = load i32, i32* %14
%81 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %79, i32 9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %80)
store i32 %81, i32* %12
%82 = load i32, i32* %12
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %78
store i32 -1, i32* %6
br label %135
85: ; preds = %78
%86 = load i32, i32* %11
%87 = load i8*, i8** %10
%88 = call i32 @strlen(i8* %87)
%89 = sub nsw i32 %86, %88
store i32 %89, i32* %19
%90 = load i8*, i8** %15
%91 = load i32, i32* %19
%92 = getelementptr inbounds [9 x i8], [9 x i8]* %18, i64 0, i64 0
%93 = load i32, i32* %7
%94 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %90, i32 %91, i8* %92, i32 %93)
store i32 %94, i32* %12
%95 = load i32, i32* %12
%96 = icmp slt i32 %95, 0
br i1 %96, label %97, label %98
97: ; preds = %85
store i32 -1, i32* %6
br label %135
98: ; preds = %85
%99 = load i32, i32* %9
%100 = load i32, i32* %8
%101 = icmp sge i32 %99, %100
br i1 %101, label %102, label %134
102: ; preds = %98
%103 = load i8*, i8** %10
%104 = load i8*, i8** %10
%105 = call i32 @strlen(i8* %104)
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds i8, i8* %103, i64 %106
store i8* %107, i8** %20
%108 = load i8*, i8** %15
store i8* %108, i8** %21
store i32 0, i32* %22
br label %109
109: ; preds = %130, %102
%110 = load i32, i32* %22
%111 = load i32, i32* %16
%112 = icmp slt i32 %110, %111
br i1 %112, label %113, label %133
113: ; preds = %109
%114 = load i8*, i8** %20
%115 = load i8*, i8** %21
%116 = load i32, i32* %13
%117 = call i32 @memcpy(i8* %114, i8* %115, i32 %116)
%118 = load i32, i32* %13
%119 = load i8*, i8** %20
%120 = sext i32 %118 to i64
%121 = getelementptr inbounds i8, i8* %119, i64 %120
store i8* %121, i8** %20
%122 = load i32, i32* %13
%123 = load i8*, i8** %21
%124 = sext i32 %122 to i64
%125 = getelementptr inbounds i8, i8* %123, i64 %124
store i8* %125, i8** %21
%126 = load i8*, i8** %20
%127 = call i32 @memcpy(i8* %126, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%128 = load i8*, i8** %20
%129 = getelementptr inbounds i8, i8* %128, i64 1
store i8* %129, i8** %20
br label %130
130: ; preds = %113
%131 = load i32, i32* %22
%132 = add nsw i32 %131, 1
store i32 %132, i32* %22
br label %109
133: ; preds = %109
br label %134
134: ; preds = %133, %98
store i32 0, i32* %6
br label %135
135: ; preds = %134, %97, %84, %54, %34, %28
%136 = load i32, i32* %6
ret i32 %136
}

LLVM IR (-O2):
@.str = [8 x i8] c"chunks/\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [10 x i8] c"%%0.%illu\00"
define i32 @chunk_relpath(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca [9 x i8]
%7 = add nsw i32 %1, -1
%8 = tail call i32 @uint64t_strlen(i32 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %67, label %10
10: ; preds = %5
%11 = tail call i32 @uint64t_strlen(i32 %2)
%12 = icmp slt i32 %11, 1
br i1 %12, label %67, label %13
13: ; preds = %10
%14 = urem i32 %11, %8
%15 = icmp eq i32 %14, 0
%16 = sub nsw i32 %8, %14
%17 = select i1 %15, i32 0, i32 %16
%18 = add nsw i32 %17, %11
%19 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %3, i32 %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%20 = icmp slt i32 %19, 0
br i1 %20, label %67, label %21
21: ; preds = %13
%22 = tail call i32 @strlen(i8* %3)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %3, i64 %23
%25 = icmp slt i32 %2, %1
br i1 %25, label %34, label %26
26: ; preds = %21
%27 = sdiv i32 %18, %8
%28 = add nsw i32 %27, -1
%29 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%30 = add nsw i32 %29, %8
%31 = mul nsw i32 %30, %28
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %24, i64 %32
br label %34
34: ; preds = %26, %21
%35 = phi i8* [ %33, %26 ], [ %24, %21 ]
%36 = phi i32 [ %28, %26 ], [ undef, %21 ]
%37 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%38 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %37, i32 9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%39 = icmp slt i32 %38, 0
br i1 %39, label %65, label %40
40: ; preds = %34
%41 = call i32 @strlen(i8* %3)
%42 = sub nsw i32 %4, %41
%43 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %35, i32 %42, i8* %37, i32 %0)
%44 = icmp slt i32 %43, 0
%45 = or i1 %25, %44
%46 = ashr i32 %43, 31
br i1 %45, label %65, label %47
47: ; preds = %40
%48 = call i32 @strlen(i8* %3)
%49 = icmp sgt i32 %36, 0
br i1 %49, label %50, label %65
50: ; preds = %47
%51 = sext i32 %48 to i64
%52 = getelementptr inbounds i8, i8* %3, i64 %51
%53 = zext i32 %8 to i64
br label %54
54: ; preds = %50, %54
%55 = phi i32 [ 0, %50 ], [ %63, %54 ]
%56 = phi i8* [ %35, %50 ], [ %60, %54 ]
%57 = phi i8* [ %52, %50 ], [ %62, %54 ]
%58 = call i32 @memcpy(i8* %57, i8* %56, i32 %8)
%59 = getelementptr inbounds i8, i8* %57, i64 %53
%60 = getelementptr inbounds i8, i8* %56, i64 %53
%61 = call i32 @memcpy(i8* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%62 = getelementptr inbounds i8, i8* %59, i64 1
%63 = add nuw nsw i32 %55, 1
%64 = icmp eq i32 %63, %36
br i1 %64, label %65, label %54
65: ; preds = %54, %47, %40, %34
%66 = phi i32 [ -1, %34 ], [ %46, %40 ], [ 0, %47 ], [ 0, %54 ]
br label %67
67: ; preds = %65, %10, %13, %5
%68 = phi i32 [ -1, %5 ], [ %66, %65 ], [ -1, %10 ], [ -1, %13 ]
ret i32 %68
}