#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ chosp(char *);
int /*<<< orphan*/ free(char *const);
scalar_t__ lstat(char *, struct stat *);
char *pick_trash_dir(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *const,
                            int /*<<< orphan*/, char const *);
char *strdup(char *);
char *gen_trash_name(const char base_path[], const char name[]) {
  struct stat st;
  char buf[4096];
  int i;
  char *const trash_dir = pick_trash_dir(base_path);
  if (trash_dir == NULL) {
    return NULL;
  }
  i = 0;
  do {
    snprintf(buf, sizeof(buf), "%s/%03d_%s", trash_dir, i++, name);
    chosp(buf);
  } while (lstat(buf, &st) == 0);
  free(trash_dir);
  return strdup(buf);
}


x86 assembly (clang, -O0):
<gen_trash_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <pick_trash_dir@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jne    119e <gen_trash_name+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    1220 <gen_trash_name+0xc0>
movl   $0x0,-0x1024(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x1030(%rbp),%rcx
mov    -0x1024(%rbp),%r8d
mov    %r8d,%eax
add    $0x1,%eax
mov    %eax,-0x1024(%rbp)
mov    -0x18(%rbp),%r9
mov    $0x1000,%esi
lea    0xe27(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x1020(%rbp),%rdi
call   1080 <chosp@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
je     11a8 <gen_trash_name+0x48>
mov    -0x1030(%rbp),%rdi
call   1040 <free@plt>
lea    -0x1020(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_trash_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
call   1030 <pick_trash_dir@plt>
xor    %r8d,%r8d
test   %rax,%rax
je     11ea <gen_trash_name+0x8a>
mov    %rax,%r12
lea    0xe75(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%r8),%ebp
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %r14,%r9
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1080 <chosp@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <lstat@plt>
mov    %ebp,%r8d
test   %rax,%rax
je     11a0 <gen_trash_name+0x40>
mov    %r12,%rdi
call   1040 <free@plt>
lea    0x10(%rsp),%rdi
call   1070 <strdup@plt>
mov    %rax,%r8
mov    %r8,%rax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"%s/%03d_%s\00"
define i8* @gen_trash_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i8* @pick_trash_dir(i8* %10)
store i8* %11, i8** %9
%12 = load i8*, i8** %9
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %34
15: ; preds = %2
store i32 0, i32* %8
br label %16
16: ; preds = %25, %15
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%18 = load i8*, i8** %9
%19 = load i32, i32* %8
%20 = add nsw i32 %19, 1
store i32 %20, i32* %8
%21 = load i8*, i8** %5
%22 = call i32 @snprintf(i8* %17, i32 4096, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %19, i8* %21)
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%24 = call i32 @chosp(i8* %23)
br label %25
25: ; preds = %16
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%27 = call i64 @lstat(i8* %26, %struct.stat* %6)
%28 = icmp eq i64 %27, 0
br i1 %28, label %16, label %29
29: ; preds = %25
%30 = load i8*, i8** %9
%31 = call i32 @free(i8* %30)
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%33 = call i8* @strdup(i8* %32)
store i8* %33, i8** %3
br label %34
34: ; preds = %29, %14
%35 = load i8*, i8** %3
ret i8* %35
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"%s/%03d_%s\00"
define noalias i8* @gen_trash_name(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [4096 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = tail call i8* @pick_trash_dir(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %19, label %9
9: ; preds = %2, %9
%10 = phi i32 [ %11, %9 ], [ 0, %2 ]
%11 = add nuw nsw i32 %10, 1
%12 = call i32 @snprintf(i8* %6, i32 4096, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %10, i8* %1)
%13 = call i32 @chosp(i8* %6)
%14 = call i64 @lstat(i8* %6, %struct.stat* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %9, label %16
16: ; preds = %9
%17 = call i32 @free(i8* %7)
%18 = call i8* @strdup(i8* %6)
br label %19
19: ; preds = %2, %16
%20 = phi i8* [ %18, %16 ], [ null, %2 ]
ret i8* %20
}