#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 *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int strlen(char *);
char *form_filename(int chunk, char *dir) {
  char *path;
  int d;
  int dirlen, filelen, path_len;
  dirlen = strlen(dir);
  filelen = 1;
  d = chunk;
  while (d) {
    d /= 10;
    filelen++;
  }
  path_len = dirlen + filelen + 1 + 1;
  path = malloc(path_len);
  snprintf(path, path_len, "%s/%d", dir, chunk);
  return path;
}


x86 assembly (clang, -O0):
<form_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     117e <form_filename+0x4e>
mov    -0x1c(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1158 <form_filename+0x28>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
add    $0x1,%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    -0x4(%rbp),%r8d
lea    0xe51(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<form_filename>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    $0x1,%ecx
test   %r15d,%r15d
je     1182 <form_filename+0x52>
mov    %r15d,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %edx,%rsi
imul   $0x66666667,%rsi,%rdx
mov    %rdx,%rdi
shr    $0x3f,%rdi
sar    $0x22,%rdx
add    %edi,%edx
add    $0x1,%ecx
add    $0x9,%esi
cmp    $0x12,%esi
ja     1160 <form_filename+0x30>
lea    (%rax,%rcx,1),%ebp
add    $0x2,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0xe67(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rcx
mov    %r15d,%r8d
call   1040 <snprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%d\00"
define i8* @form_filename(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %7
store i32 1, i32* %8
%12 = load i32, i32* %3
store i32 %12, i32* %6
br label %13
13: ; preds = %16, %2
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* %6
%18 = sdiv i32 %17, 10
store i32 %18, i32* %6
%19 = load i32, i32* %8
%20 = add nsw i32 %19, 1
store i32 %20, i32* %8
br label %13
21: ; preds = %13
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = add nsw i32 %22, %23
%25 = add nsw i32 %24, 1
%26 = add nsw i32 %25, 1
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %5
%29 = load i8*, i8** %5
%30 = load i32, i32* %9
%31 = load i8*, i8** %4
%32 = load i32, i32* %3
%33 = call i32 @snprintf(i8* %29, i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %31, i32 %32)
%34 = load i8*, i8** %5
ret i8* %34
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%d\00"
define i8* @form_filename(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp eq i32 %0, 0
br i1 %4, label %12, label %5
5: ; preds = %2, %5
%6 = phi i32 [ %8, %5 ], [ %0, %2 ]
%7 = phi i32 [ %9, %5 ], [ 1, %2 ]
%8 = sdiv i32 %6, 10
%9 = add nuw nsw i32 %7, 1
%10 = add i32 %6, 9
%11 = icmp ult i32 %10, 19
br i1 %11, label %12, label %5
12: ; preds = %5, %2
%13 = phi i32 [ 1, %2 ], [ %9, %5 ]
%14 = add i32 %3, 2
%15 = add i32 %14, %13
%16 = tail call i8* @malloc(i32 %15)
%17 = tail call i32 @snprintf(i8* %16, i32 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %0)
ret i8* %16
}