#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;
};

/* Variables and functions */
int ENOMEM;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int stat(char *, struct stat *);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
int cleanupFiles(int fileNum, char *fileDir) {
  int length = strlen(fileDir) + 21 * sizeof(char);
  char *name = malloc(length);
  if (name == NULL) {
    return ENOMEM;
  }
  printf("Cleaning up files\n");
  while (--fileNum >= 0) {
    struct stat buf;
    snprintf(name, length, "%s/%d", fileDir, fileNum);
    int retVal = stat(name, &buf);
    if (retVal == 0)
      unlink(name);
  }
  free(name);
  return 0;
}


x86 assembly (clang, -O0):
<cleanupFiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
add    $0x15,%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b9 <cleanupFiles+0x49>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    122a <cleanupFiles+0xba>
lea    0xe40(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     121a <cleanupFiles+0xaa>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8d
lea    0xe26(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1215 <cleanupFiles+0xa5>
mov    -0x20(%rbp),%rdi
call   1040 <unlink@plt>
jmp    11c5 <cleanupFiles+0x55>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cleanupFiles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
mov    %rsi,%rdi
call   1060 <strlen@plt>
mov    %eax,%r15d
add    $0x15,%r15d
mov    %r15d,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1202 <cleanupFiles+0x92>
mov    %rax,%rbp
lea    0xe60(%rip),%rdi
call   1050 <puts@plt>
test   %ebx,%ebx
jle    11f6 <cleanupFiles+0x86>
add    $0x1,%ebx
lea    0xe47(%rip),%r12
mov    %rsp,%r13
jmp    11c8 <cleanupFiles+0x58>
xchg   %ax,%ax
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jbe    11f6 <cleanupFiles+0x86>
lea    -0x2(%rbx),%r8d
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r12,%rdx
mov    %r14,%rcx
call   1070 <snprintf@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1080 <stat@plt>
test   %eax,%eax
jne    11c0 <cleanupFiles+0x50>
mov    %rbp,%rdi
call   1040 <unlink@plt>
jmp    11c0 <cleanupFiles+0x50>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    120b <cleanupFiles+0x9b>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOMEM = global i32 0
@.str = [19 x i8] c"Cleaning up files\0A\00"
@.str.1 = [6 x i8] c"%s/%d\00"
define i32 @cleanupFiles(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
%12 = sext i32 %11 to i64
%13 = add i64 %12, 21
%14 = trunc i64 %13 to i32
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = load i32, i32* @ENOMEM
store i32 %20, i32* %3
br label %44
21: ; preds = %2
%22 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %40, %21
%24 = load i32, i32* %4
%25 = add nsw i32 %24, -1
store i32 %25, i32* %4
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %41
27: ; preds = %23
%28 = load i8*, i8** %7
%29 = load i32, i32* %6
%30 = load i8*, i8** %5
%31 = load i32, i32* %4
%32 = call i32 @snprintf(i8* %28, i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %30, i32 %31)
%33 = load i8*, i8** %7
%34 = call i32 @stat(i8* %33, %struct.stat* %8)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %27
%38 = load i8*, i8** %7
%39 = call i32 @unlink(i8* %38)
br label %40
40: ; preds = %37, %27
br label %23
41: ; preds = %23
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
store i32 0, i32* %3
br label %44
44: ; preds = %41, %19
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOMEM = global i32 0
@.str.1 = [6 x i8] c"%s/%d\00"
@str = [18 x i8] c"Cleaning up files\00"
define i32 @cleanupFiles(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %4, 21
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @ENOMEM
br label %27
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%12 = icmp sgt i32 %0, 0
br i1 %12, label %13, label %25
13: ; preds = %10
%14 = bitcast %struct.stat* %3 to i8*
br label %15
15: ; preds = %13, %23
%16 = phi i32 [ %0, %13 ], [ %17, %23 ]
%17 = add nsw i32 %16, -1
%18 = tail call i32 @snprintf(i8* %6, i32 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %17)
%19 = call i32 @stat(i8* %6, %struct.stat* %3)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = tail call i32 @unlink(i8* %6)
br label %23
23: ; preds = %21, %15
%24 = icmp ugt i32 %16, 1
br i1 %24, label %15, label %25
25: ; preds = %23, %10
%26 = tail call i32 @free(i8* %6)
br label %27
27: ; preds = %25, %8
%28 = phi i32 [ %9, %8 ], [ 0, %25 ]
ret i32 %28
}