#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 */
int EACCES;
int ENOMEM;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ PROT_WRITE;
scalar_t__ close(int);
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fsync(int);
int ftruncate(int, int);
char *malloc(int);
int /*<<< orphan*/ memset(void *, int, int);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int strlen(char *);
int createFiles(int fileSize, int fileNum, char *fileDir) {
  int length = strlen(fileDir) + 21 * sizeof(char);
  char *name = malloc(length);
  if (name == NULL) {
    return ENOMEM;
  }
  printf("Creating files\n");
  while (--fileNum >= 0) {
    snprintf(name, length, "%s/%d", fileDir, fileNum);
    int fd = open(name, O_CREAT | O_EXCL | O_RDWR | O_SYNC);
    if (fd == -1) {
      perror("While creating a file");
      free(name);
      return fd;
    }
    fchmod(fd, 0600);
    int result = ftruncate(fd, (10 * 1048576));
    if (result != 0) {
      perror("While calling truncate");
      close(fd);
      free(name);
      return result;
    }
    void *buffer = mmap(0, (10 * 1048576), PROT_WRITE, MAP_SHARED, fd, 0);
    if (buffer == (void *)-1) {
      perror("While mmapping a file");
      close(fd);
      free(name);
      return EACCES;
    }
    memset(buffer, 0xDEADBEEF, (10 * 1048576));
    munmap(buffer, (10 * 1048576));
    fsync(fd);
    while (close(fd) != 0)
      ;
  }
  free(name);
  return 0;
}


x86 assembly (clang, -O0):
<createFiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cltq
add    $0x15,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122c <createFiles+0x4c>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cc <createFiles+0x1ec>
lea    0xdcd(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jl     13bc <createFiles+0x1dc>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    -0xc(%rbp),%r8d
lea    0xdb0(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2d37(%rip),%rax
or     (%rax),%esi
mov    0x2d56(%rip),%rax
or     (%rax),%esi
mov    0x2d45(%rip),%rax
or     (%rax),%esi
call   10f0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12bf <createFiles+0xdf>
lea    0xd70(%rip),%rdi
call   1100 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13cc <createFiles+0x1ec>
mov    -0x2c(%rbp),%edi
mov    $0x180,%esi
call   10d0 <fchmod@plt>
mov    -0x2c(%rbp),%edi
mov    $0xa00000,%esi
call   1080 <ftruncate@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     130e <createFiles+0x12e>
lea    0xd3f(%rip),%rdi
call   1100 <perror@plt>
mov    -0x2c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13cc <createFiles+0x1ec>
mov    0x2c93(%rip),%rax
mov    (%rax),%edx
mov    0x2cca(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    $0xa00000,%esi
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x38(%rbp)
jne    1377 <createFiles+0x197>
lea    0xcf3(%rip),%rdi
call   1100 <perror@plt>
mov    -0x2c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cc <createFiles+0x1ec>
mov    -0x38(%rbp),%rdi
mov    $0xdeadbeef,%esi
mov    $0xa00000,%edx
call   1090 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    $0xa00000,%esi
call   10c0 <munmap@plt>
mov    -0x2c(%rbp),%edi
call   10e0 <fsync@plt>
mov    -0x2c(%rbp),%edi
call   10a0 <close@plt>
cmp    $0x0,%rax
je     13b7 <createFiles+0x1d7>
jmp    13a0 <createFiles+0x1c0>
jmp    1238 <createFiles+0x58>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createFiles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rdi
mov    %esi,%r13d
mov    %rdx,0x10(%rsp)
call   1050 <strlen@plt>
add    $0x15,%eax
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1327 <createFiles+0x147>
mov    %rax,%r15
lea    0xe2a(%rip),%rdi
call   1040 <puts@plt>
test   %r13d,%r13d
jle    131a <createFiles+0x13a>
nopl   (%rax)
lea    -0x1(%r13),%ebp
mov    %r15,%rdi
mov    0xc(%rsp),%esi
lea    0xdbe(%rip),%rdx
mov    0x10(%rsp),%rcx
mov    %ebp,%r8d
call   1070 <snprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
mov    0x2d70(%rip),%rax
or     (%rax),%esi
mov    0x2d5f(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
call   10f0 <open@plt>
cmp    $0xffffffff,%eax
je     1343 <createFiles+0x163>
mov    %eax,%r12d
mov    %eax,%edi
mov    $0x180,%esi
call   10d0 <fchmod@plt>
mov    %r12d,%edi
mov    $0xa00000,%esi
call   1080 <ftruncate@plt>
test   %eax,%eax
jne    135f <createFiles+0x17f>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edx
mov    0x2d30(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0xa00000,%esi
mov    %r12d,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1380 <createFiles+0x1a0>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xdeadbeef,%esi
mov    $0xa00000,%edx
call   1090 <memset@plt>
mov    %rbx,%rdi
mov    $0xa00000,%esi
call   10c0 <munmap@plt>
mov    %r12d,%edi
call   10e0 <fsync@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
test   %rax,%rax
jne    1300 <createFiles+0x120>
cmp    $0x2,%r13d
mov    %ebp,%r13d
jge    1230 <createFiles+0x50>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    1331 <createFiles+0x151>
mov    0x2c8a(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcbc(%rip),%rdi
call   1100 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r14d
jmp    1331 <createFiles+0x151>
mov    %eax,%r14d
lea    0xcb3(%rip),%rdi
call   1100 <perror@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1331 <createFiles+0x151>
lea    0xcac(%rip),%rdi
call   1100 <perror@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2c25(%rip),%rax
jmp    132e <createFiles+0x14e>

LLVM IR (-O0):
@ENOMEM = global i32 0
@.str = [16 x i8] c"Creating files\0A\00"
@.str.1 = [6 x i8] c"%s/%d\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [22 x i8] c"While creating a file\00"
@.str.3 = [23 x i8] c"While calling truncate\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [22 x i8] c"While mmapping a file\00"
@EACCES = global i32 0
define i32 @createFiles(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%13 = load i8*, i8** %7
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = add i64 %15, 21
%17 = trunc i64 %16 to i32
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %3
%23 = load i32, i32* @ENOMEM
store i32 %23, i32* %4
br label %96
24: ; preds = %3
%25 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %92, %24
%27 = load i32, i32* %6
%28 = add nsw i32 %27, -1
store i32 %28, i32* %6
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %93
30: ; preds = %26
%31 = load i8*, i8** %9
%32 = load i32, i32* %8
%33 = load i8*, i8** %7
%34 = load i32, i32* %6
%35 = call i32 @snprintf(i8* %31, i32 %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %33, i32 %34)
%36 = load i8*, i8** %9
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_EXCL
%39 = or i32 %37, %38
%40 = load i32, i32* @O_RDWR
%41 = or i32 %39, %40
%42 = load i32, i32* @O_SYNC
%43 = or i32 %41, %42
%44 = call i32 @open(i8* %36, i32 %43)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %30
%48 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%49 = load i8*, i8** %9
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %10
store i32 %51, i32* %4
br label %96
52: ; preds = %30
%53 = load i32, i32* %10
%54 = call i32 @fchmod(i32 %53, i32 384)
%55 = load i32, i32* %10
%56 = call i32 @ftruncate(i32 %55, i32 10485760)
store i32 %56, i32* %11
%57 = load i32, i32* %11
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %66
59: ; preds = %52
%60 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%61 = load i32, i32* %10
%62 = call i64 @close(i32 %61)
%63 = load i8*, i8** %9
%64 = call i32 @free(i8* %63)
%65 = load i32, i32* %11
store i32 %65, i32* %4
br label %96
66: ; preds = %52
%67 = load i32, i32* @PROT_WRITE
%68 = load i32, i32* @MAP_SHARED
%69 = load i32, i32* %10
%70 = call i8* @mmap(i32 0, i32 10485760, i32 %67, i32 %68, i32 %69, i32 0)
store i8* %70, i8** %12
%71 = load i8*, i8** %12
%72 = icmp eq i8* %71, inttoptr (i64 -1 to i8*)
br i1 %72, label %73, label %80
73: ; preds = %66
%74 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%75 = load i32, i32* %10
%76 = call i64 @close(i32 %75)
%77 = load i8*, i8** %9
%78 = call i32 @free(i8* %77)
%79 = load i32, i32* @EACCES
store i32 %79, i32* %4
br label %96
80: ; preds = %66
%81 = load i8*, i8** %12
%82 = call i32 @memset(i8* %81, i32 -559038737, i32 10485760)
%83 = load i8*, i8** %12
%84 = call i32 @munmap(i8* %83, i32 10485760)
%85 = load i32, i32* %10
%86 = call i32 @fsync(i32 %85)
br label %87
87: ; preds = %91, %80
%88 = load i32, i32* %10
%89 = call i64 @close(i32 %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %92
91: ; preds = %87
br label %87
92: ; preds = %87
br label %26
93: ; preds = %26
%94 = load i8*, i8** %9
%95 = call i32 @free(i8* %94)
store i32 0, i32* %4
br label %96
96: ; preds = %93, %73, %59, %47, %22
%97 = load i32, i32* %4
ret i32 %97
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@.str.1 = [6 x i8] c"%s/%d\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.2 = [22 x i8] c"While creating a file\00"
@.str.3 = [23 x i8] c"While calling truncate\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.4 = [22 x i8] c"While mmapping a file\00"
@EACCES = global i32 0
@str = [15 x i8] c"Creating files\00"
define i32 @createFiles(i32 %0, i32 %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %2)
%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 = %3
%9 = load i32, i32* @ENOMEM
br label %58
10: ; preds = %3
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%12 = icmp sgt i32 %1, 0
br i1 %12, label %15, label %56
13: ; preds = %53
%14 = icmp sgt i32 %16, 1
br i1 %14, label %15, label %56
15: ; preds = %10, %13
%16 = phi i32 [ %17, %13 ], [ %1, %10 ]
%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* %2, i32 %17)
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_EXCL
%21 = or i32 %20, %19
%22 = load i32, i32* @O_RDWR
%23 = or i32 %21, %22
%24 = load i32, i32* @O_SYNC
%25 = or i32 %23, %24
%26 = tail call i32 @open(i8* %6, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %15
%29 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @free(i8* %6)
br label %58
31: ; preds = %15
%32 = tail call i32 @fchmod(i32 %26, i32 384)
%33 = tail call i32 @ftruncate(i32 %26, i32 10485760)
%34 = icmp eq i32 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i64 @close(i32 %26)
%38 = tail call i32 @free(i8* %6)
br label %58
39: ; preds = %31
%40 = load i32, i32* @PROT_WRITE
%41 = load i32, i32* @MAP_SHARED
%42 = tail call i8* @mmap(i32 0, i32 10485760, i32 %40, i32 %41, i32 %26, i32 0)
%43 = icmp eq i8* %42, inttoptr (i64 -1 to i8*)
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%46 = tail call i64 @close(i32 %26)
%47 = tail call i32 @free(i8* %6)
%48 = load i32, i32* @EACCES
br label %58
49: ; preds = %39
%50 = tail call i32 @memset(i8* %42, i32 -559038737, i32 10485760)
%51 = tail call i32 @munmap(i8* %42, i32 10485760)
%52 = tail call i32 @fsync(i32 %26)
br label %53
53: ; preds = %53, %49
%54 = tail call i64 @close(i32 %26)
%55 = icmp eq i64 %54, 0
br i1 %55, label %13, label %53
56: ; preds = %13, %10
%57 = tail call i32 @free(i8* %6)
br label %58
58: ; preds = %44, %35, %28, %56, %8
%59 = phi i32 [ %9, %8 ], [ 0, %56 ], [ %48, %44 ], [ %33, %35 ], [ -1, %28 ]
ret i32 %59
}