#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 */
scalar_t__ EEXIST;
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *, int,
                           char *);
int getpid();
char *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int rename(char const *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int preserveFile(const char *fn) {
  int i, fd = open(fn, O_RDONLY);
  if (fd != -1) {
    size_t size = strlen(fn) + 8;
    char *newfn = malloc(size);
    if (newfn == NULL) {
      fprintf(stderr,
              "kickstart[%d]: %s[%d]: "
              "malloc: %s\n",
              getpid(),
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/pegasus-isi/pegasus/refs/heads/master/"
              "src/tools/pegasus-kickstart/statinfo.c",
              154, strerror(errno));
      return -1;
    }
    close(fd);
    strncpy(newfn, fn, size);
    for (i = 0; i < 1000; ++i) {
      snprintf(newfn + size - 8, 8, ".%03d", i);
      if ((fd = open(newfn, O_RDONLY)) == -1) {
        if (errno == ENOENT)
          break;
        else
          return -1;
      }
      close(fd);
    }
    if (i < 1000) {
      return rename(fn, newfn);
    } else {
      errno = EEXIST;
      return -1;
    }
  } else {
    errno = 0;
    return 0;
  }
}


x86 assembly (clang, -O0):
<preserveFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     1336 <preserveFile+0x196>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x8,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1248 <preserveFile+0xa8>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    %rax,%r9
lea    0xdd6(%rip),%rsi
lea    0xdf2(%rip),%rcx
mov    $0x9a,%r8d
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <preserveFile+0x1ab>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3e8,-0x14(%rbp)
jge    12f4 <preserveFile+0x154>
mov    -0x28(%rbp),%rdi
add    -0x20(%rbp),%rdi
add    $0xfffffffffffffff8,%rdi
mov    -0x14(%rbp),%ecx
mov    $0x8,%esi
lea    0xe3a(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d48(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12de <preserveFile+0x13e>
mov    0x2d0e(%rip),%rax
mov    (%rax),%rax
mov    0x2cf4(%rip),%rcx
cmp    (%rcx),%rax
jne    12d2 <preserveFile+0x132>
jmp    12f4 <preserveFile+0x154>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <preserveFile+0x1ab>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1268 <preserveFile+0xc8>
cmpl   $0x3e8,-0x14(%rbp)
jge    1316 <preserveFile+0x176>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10b0 <rename@plt>
mov    %eax,-0x4(%rbp)
jmp    134b <preserveFile+0x1ab>
mov    0x2cbb(%rip),%rax
mov    (%rax),%rcx
mov    0x2ca1(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <preserveFile+0x1ab>
mov    0x2c8b(%rip),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<preserveFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e33(%rip),%r13
mov    0x0(%r13),%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1277 <preserveFile+0xd7>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
add    $0x8,%eax
movslq %eax,%rbp
mov    %rbp,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     1289 <preserveFile+0xe9>
mov    %rax,%r15
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1030 <strncpy@plt>
lea    (%r15,%rbp,1),%rbx
add    $0xfffffffffffffff8,%rbx
xor    %ebp,%ebp
lea    0xeb9(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x8,%esi
mov    %r12,%rdx
mov    %ebp,%ecx
call   1060 <snprintf@plt>
mov    0x0(%r13),%esi
mov    %r15,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12d1 <preserveFile+0x131>
mov    %eax,%edi
call   1070 <close@plt>
add    $0x1,%ebp
cmp    $0x3e8,%ebp
jne    1220 <preserveFile+0x80>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    0x2d5e(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
jmp    1305 <preserveFile+0x165>
mov    0x2d4a(%rip),%rax
movq   $0x0,(%rax)
xor    %eax,%eax
jmp    1305 <preserveFile+0x165>
mov    0x2d40(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    0x2d26(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd4f(%rip),%rsi
lea    0xd6b(%rip),%rcx
mov    %ebx,%edi
mov    %ebp,%edx
mov    $0x9a,%r8d
mov    %rax,%r9
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1305 <preserveFile+0x165>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rcx
mov    0x2cd6(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    1305 <preserveFile+0x165>
mov    %r14,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <rename@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"kickstart[%d]: %s[%d]: malloc: %s\0A\00"
@.str.1 = [167 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/pegasus-isi/pegasus/refs/heads/master/src/tools/pegasus-kickstart/statinfo.c\00"
@errno = global i64 0
@.str.2 = [6 x i8] c".%03d\00"
@ENOENT = global i64 0
@EEXIST = global i64 0
define i32 @preserveFile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %70
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 8
%17 = sext i32 %16 to i64
store i64 %17, i64* %6
%18 = load i64, i64* %6
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = call i32 (...) @getpid()
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %24, i8* getelementptr inbounds ([167 x i8], [167 x i8]* @.str.1, i64 0, i64 0), i32 154, i8* %26)
store i32 -1, i32* %2
br label %71
28: ; preds = %13
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %7
%32 = load i8*, i8** %3
%33 = load i64, i64* %6
%34 = call i32 @strncpy(i8* %31, i8* %32, i64 %33)
store i32 0, i32* %4
br label %35
35: ; preds = %58, %28
%36 = load i32, i32* %4
%37 = icmp slt i32 %36, 1000
br i1 %37, label %38, label %61
38: ; preds = %35
%39 = load i8*, i8** %7
%40 = load i64, i64* %6
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = getelementptr inbounds i8, i8* %41, i64 -8
%43 = load i32, i32* %4
%44 = call i32 @snprintf(i8* %42, i32 8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = load i8*, i8** %7
%46 = load i32, i32* @O_RDONLY
%47 = call i32 @open(i8* %45, i32 %46)
store i32 %47, i32* %5
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %38
%50 = load i64, i64* @errno
%51 = load i64, i64* @ENOENT
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %49
br label %61
54: ; preds = %49
store i32 -1, i32* %2
br label %71
55: ; preds = %38
%56 = load i32, i32* %5
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55
%59 = load i32, i32* %4
%60 = add nsw i32 %59, 1
store i32 %60, i32* %4
br label %35
61: ; preds = %53, %35
%62 = load i32, i32* %4
%63 = icmp slt i32 %62, 1000
br i1 %63, label %64, label %68
64: ; preds = %61
%65 = load i8*, i8** %3
%66 = load i8*, i8** %7
%67 = call i32 @rename(i8* %65, i8* %66)
store i32 %67, i32* %2
br label %71
68: ; preds = %61
%69 = load i64, i64* @EEXIST
store i64 %69, i64* @errno
store i32 -1, i32* %2
br label %71
70: ; preds = %1
store i64 0, i64* @errno
store i32 0, i32* %2
br label %71
71: ; preds = %70, %68, %64, %54, %22
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"kickstart[%d]: %s[%d]: malloc: %s\0A\00"
@.str.1 = [167 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/pegasus-isi/pegasus/refs/heads/master/src/tools/pegasus-kickstart/statinfo.c\00"
@errno = global i64 0
@.str.2 = [6 x i8] c".%03d\00"
@ENOENT = global i64 0
@EEXIST = global i64 0
define i32 @preserveFile(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %40, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
%7 = add nsw i32 %6, 8
%8 = sext i32 %7 to i64
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %17
11: ; preds = %5
%12 = load i32, i32* @stderr
%13 = tail call i32 (...) @getpid()
%14 = load i64, i64* @errno
%15 = tail call i8* @strerror(i64 %14)
%16 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %13, i8* getelementptr inbounds ([167 x i8], [167 x i8]* @.str.1, i64 0, i64 0), i32 154, i8* %15)
br label %41
17: ; preds = %5
%18 = tail call i32 @close(i32 %3)
%19 = tail call i32 @strncpy(i8* %9, i8* %0, i64 %8)
%20 = getelementptr inbounds i8, i8* %9, i64 %8
%21 = getelementptr inbounds i8, i8* %20, i64 -8
br label %22
22: ; preds = %17, %32
%23 = phi i32 [ 0, %17 ], [ %34, %32 ]
%24 = tail call i32 @snprintf(i8* %21, i32 8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @O_RDONLY
%26 = tail call i32 @open(i8* %9, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOENT
%31 = icmp eq i64 %29, %30
br i1 %31, label %36, label %41
32: ; preds = %22
%33 = tail call i32 @close(i32 %26)
%34 = add nuw nsw i32 %23, 1
%35 = icmp eq i32 %34, 1000
br i1 %35, label %38, label %22
36: ; preds = %28
%37 = tail call i32 @rename(i8* %0, i8* %9)
br label %41
38: ; preds = %32
%39 = load i64, i64* @EEXIST
store i64 %39, i64* @errno
br label %41
40: ; preds = %1
store i64 0, i64* @errno
br label %41
41: ; preds = %11, %36, %38, %28, %40
%42 = phi i32 [ 0, %40 ], [ -1, %11 ], [ %37, %36 ], [ -1, %38 ], [ -1, %28 ]
ret i32 %42
}