#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;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int CheckPt_exist(char *File, int buffersize) {
  if (buffersize < 0) {
    printf("ERROR Buffer size less than 0!\n");
    return -1;
  }
  if (File[0] != '\0') {
    printf("ERROR FileName has not been initialized to \\0\n");
    return -1;
  }
  struct stat st;
  char FileName[256];
  char FileEx[20];
  char FileNameKeep[256];
  int len;
  int i;
  int CheckFileExist;
  File[0] = '\0';
  CheckFileExist = 0;
  if (stat("CHECKPOINT", &st) == 0) {
    DIR *d;
    struct dirent *dir;
    d = opendir("CHECKPOINT");
    if (d) {
      printf("CHECKPOINT directory exists!\n");
      while ((dir = readdir(d)) != NULL && CheckFileExist == 0) {
        strcpy(FileName, dir->d_name);
        len = strlen(FileName);
        if (len > 4) {
          for (i = 4; i >= 0; i--) {
            FileEx[4 - i] = FileName[len - i];
          }
          FileEx[4] = '\0';
          if (strcmp(FileEx, "ckpt\0") == 0) {
            CheckFileExist = 1;
            strcpy(FileNameKeep, FileName);
          }
        }
      }
      closedir(d);
    }
  } else {
    return -1;
  }
  if (CheckFileExist == 1) {
    if (buffersize < len) {
      printf("ERROR Buffersize is to small!\n");
      return -1;
    }
    printf("At least one .ckpt file was found!\n");
    printf("%s\n", FileNameKeep);
    strncpy(File, FileNameKeep, buffersize - 1);
    return 0;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<CheckPt_exist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c6 <CheckPt_exist+0x36>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1408 <CheckPt_exist+0x278>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f0 <CheckPt_exist+0x60>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1408 <CheckPt_exist+0x278>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x24c(%rbp)
lea    0xe47(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jne    137a <CheckPt_exist+0x1ea>
lea    0xe2d(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x258(%rbp)
cmpq   $0x0,-0x258(%rbp)
je     1375 <CheckPt_exist+0x1e5>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x258(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x260(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x261(%rbp)
je     1282 <CheckPt_exist+0xf2>
cmpl   $0x0,-0x24c(%rbp)
sete   %al
mov    %al,-0x261(%rbp)
mov    -0x261(%rbp),%al
test   $0x1,%al
jne    1295 <CheckPt_exist+0x105>
jmp    1369 <CheckPt_exist+0x1d9>
lea    -0x120(%rbp),%rdi
mov    -0x260(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
lea    -0x120(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x4,-0x244(%rbp)
jle    1364 <CheckPt_exist+0x1d4>
movl   $0x4,-0x248(%rbp)
cmpl   $0x0,-0x248(%rbp)
jl     131e <CheckPt_exist+0x18e>
mov    -0x244(%rbp),%eax
sub    -0x248(%rbp),%eax
cltq
mov    -0x120(%rbp,%rax,1),%cl
mov    $0x4,%eax
sub    -0x248(%rbp),%eax
cltq
mov    %cl,-0x140(%rbp,%rax,1)
mov    -0x248(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x248(%rbp)
jmp    12d4 <CheckPt_exist+0x144>
movb   $0x0,-0x13c(%rbp)
lea    -0x140(%rbp),%rdi
lea    0xd8c(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    135f <CheckPt_exist+0x1cf>
movl   $0x1,-0x24c(%rbp)
lea    -0x240(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1364 <CheckPt_exist+0x1d4>
jmp    124a <CheckPt_exist+0xba>
mov    -0x258(%rbp),%rdi
call   1080 <closedir@plt>
jmp    1386 <CheckPt_exist+0x1f6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1408 <CheckPt_exist+0x278>
cmpl   $0x1,-0x24c(%rbp)
jne    1401 <CheckPt_exist+0x271>
mov    -0x14(%rbp),%eax
cmp    -0x244(%rbp),%eax
jge    13bc <CheckPt_exist+0x22c>
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1408 <CheckPt_exist+0x278>
lea    0xcd4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x240(%rbp),%rsi
lea    0xce3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x240(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1408 <CheckPt_exist+0x278>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CheckPt_exist>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
test   %esi,%esi
js     11b6 <CheckPt_exist+0x26>
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
je     11db <CheckPt_exist+0x4b>
lea    0xeb5(%rip),%rdi
jmp    11bd <CheckPt_exist+0x2d>
lea    0xeda(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x238,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %esi,%ebx
movb   $0x0,(%r14)
lea    0xe18(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10a0 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11c7 <CheckPt_exist+0x37>
lea    0xdfd(%rip),%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     11c7 <CheckPt_exist+0x37>
mov    %rax,%r15
lea    0xe35(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12d6 <CheckPt_exist+0x146>
lea    0x30(%rsp),%r12
jmp    1251 <CheckPt_exist+0xc1>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12d6 <CheckPt_exist+0x146>
mov    (%rax),%rsi
mov    %r12,%rdi
call   1040 <strcpy@plt>
mov    %r12,%rdi
call   1070 <strlen@plt>
cmp    $0x5,%eax
jl     1240 <CheckPt_exist+0xb0>
mov    %eax,%r13d
mov    %eax,%eax
lea    0x2c(%rsp),%rdx
movzbl 0x4(%rdx,%rax,1),%ecx
mov    %cl,0x14(%rsp)
mov    (%rdx,%rax,1),%eax
mov    %eax,0x10(%rsp)
movb   $0x0,0x14(%rsp)
lea    0x10(%rsp),%rdi
lea    0xe22(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1240 <CheckPt_exist+0xb0>
lea    0x130(%rsp),%rdi
lea    0x30(%rsp),%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   10b0 <readdir@plt>
mov    %r15,%rdi
call   1080 <closedir@plt>
cmp    %ebx,%r13d
jle    12e3 <CheckPt_exist+0x153>
lea    0xd62(%rip),%rdi
call   1050 <puts@plt>
jmp    11c7 <CheckPt_exist+0x37>
mov    %r15,%rdi
call   1080 <closedir@plt>
jmp    11c7 <CheckPt_exist+0x37>
lea    0xd21(%rip),%rdi
call   1050 <puts@plt>
lea    0x130(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <puts@plt>
add    $0xffffffff,%ebx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
xor    %ebp,%ebp
jmp    11c7 <CheckPt_exist+0x37>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [32 x i8] c"ERROR Buffer size less than 0!\0A\00"
@.str.1 = [47 x i8] c"ERROR FileName has not been initialized to \\0\0A\00"
@.str.2 = [11 x i8] c"CHECKPOINT\00"
@.str.3 = [30 x i8] c"CHECKPOINT directory exists!\0A\00"
@.str.4 = [6 x i8] c"ckpt\00\00"
@.str.5 = [31 x i8] c"ERROR Buffersize is to small!\0A\00"
@.str.6 = [36 x i8] c"At least one .ckpt file was found!\0A\00"
@.str.7 = [4 x i8] c"%s\0A\00"
define i32 @CheckPt_exist(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca [256 x i8]
%8 = alloca [20 x i8]
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca %struct.dirent*
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %110
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %110
27: ; preds = %19
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 0, i8* %29
store i32 0, i32* %12
%30 = call i64 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %6)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %90
32: ; preds = %27
%33 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %13
%34 = load i32*, i32** %13
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %89
36: ; preds = %32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %85, %36
%39 = load i32*, i32** %13
%40 = call %struct.dirent* @readdir(i32* %39)
store %struct.dirent* %40, %struct.dirent** %14
%41 = icmp ne %struct.dirent* %40, null
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %12
%44 = icmp eq i32 %43, 0
br label %45
45: ; preds = %42, %38
%46 = phi i1 [ false, %38 ], [ %44, %42 ]
br i1 %46, label %47, label %86
47: ; preds = %45
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%49 = load %struct.dirent*, %struct.dirent** %14
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 @strcpy(i8* %48, i8* %51)
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%54 = call i32 @strlen(i8* %53)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp sgt i32 %55, 4
br i1 %56, label %57, label %85
57: ; preds = %47
store i32 4, i32* %11
br label %58
58: ; preds = %72, %57
%59 = load i32, i32* %11
%60 = icmp sge i32 %59, 0
br i1 %60, label %61, label %75
61: ; preds = %58
%62 = load i32, i32* %10
%63 = load i32, i32* %11
%64 = sub nsw i32 %62, %63
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %65
%67 = load i8, i8* %66
%68 = load i32, i32* %11
%69 = sub nsw i32 4, %68
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 %70
store i8 %67, i8* %71
br label %72
72: ; preds = %61
%73 = load i32, i32* %11
%74 = add nsw i32 %73, -1
store i32 %74, i32* %11
br label %58
75: ; preds = %58
%76 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 4
store i8 0, i8* %76
%77 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%78 = call i64 @strcmp(i8* %77, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%79 = icmp eq i64 %78, 0
br i1 %79, label %80, label %84
80: ; preds = %75
store i32 1, i32* %12
%81 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%82 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%83 = call i32 @strcpy(i8* %81, i8* %82)
br label %84
84: ; preds = %80, %75
br label %85
85: ; preds = %84, %47
br label %38
86: ; preds = %45
%87 = load i32*, i32** %13
%88 = call i32 @closedir(i32* %87)
br label %89
89: ; preds = %86, %32
br label %91
90: ; preds = %27
store i32 -1, i32* %3
br label %110
91: ; preds = %89
%92 = load i32, i32* %12
%93 = icmp eq i32 %92, 1
br i1 %93, label %94, label %109
94: ; preds = %91
%95 = load i32, i32* %5
%96 = load i32, i32* %10
%97 = icmp slt i32 %95, %96
br i1 %97, label %98, label %100
98: ; preds = %94
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %110
100: ; preds = %94
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0))
%102 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %102)
%104 = load i8*, i8** %4
%105 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%106 = load i32, i32* %5
%107 = sub nsw i32 %106, 1
%108 = call i32 @strncpy(i8* %104, i8* %105, i32 %107)
store i32 0, i32* %3
br label %110
109: ; preds = %91
store i32 -1, i32* %3
br label %110
110: ; preds = %109, %100, %98, %90, %25, %17
%111 = load i32, i32* %3
ret i32 %111
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str.2 = [11 x i8] c"CHECKPOINT\00"
@.str.4 = [6 x i8] c"ckpt\00\00"
@str = [35 x i8] c"At least one .ckpt file was found!\00"
@str.8 = [30 x i8] c"ERROR Buffersize is to small!\00"
@str.9 = [29 x i8] c"CHECKPOINT directory exists!\00"
@str.10 = [46 x i8] c"ERROR FileName has not been initialized to \\0\00"
@str.11 = [31 x i8] c"ERROR Buffer size less than 0!\00"
define i32 @CheckPt_exist(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = alloca [256 x i8]
%5 = alloca [20 x i8]
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%7 = alloca [256 x i8]
%8 = icmp slt i32 %1, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.11, i64 0, i64 0))
br label %64
11: ; preds = %2
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.10, i64 0, i64 0))
br label %64
16: ; preds = %11
%17 = bitcast %struct.stat* %3 to i8*
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
store i8 0, i8* %0
%21 = call i64 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %62
23: ; preds = %16
%24 = call i32* @opendir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %62, label %26
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.9, i64 0, i64 0))
%28 = call %struct.dirent* @readdir(i32* %24)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %48, label %30
30: ; preds = %26
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 4
%32 = getelementptr [256 x i8], [256 x i8]* %4, i64 0, i64 -4
br label %33
33: ; preds = %30, %45
%34 = phi %struct.dirent* [ %28, %30 ], [ %46, %45 ]
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = call i32 @strcpy(i8* %18, i8* %36)
%38 = call i32 @strlen(i8* %18)
%39 = icmp sgt i32 %38, 4
br i1 %39, label %40, label %45
40: ; preds = %33
%41 = zext i32 %38 to i64
%42 = getelementptr i8, i8* %32, i64 %41
store i8 0, i8* %31
%43 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %40, %33
%46 = call %struct.dirent* @readdir(i32* %24)
%47 = icmp eq %struct.dirent* %46, null
br i1 %47, label %48, label %33
48: ; preds = %45, %26
%49 = call i32 @closedir(i32* %24)
br label %62
50: ; preds = %40
%51 = call i32 @strcpy(i8* %20, i8* %18)
%52 = call %struct.dirent* @readdir(i32* %24)
%53 = call i32 @closedir(i32* %24)
%54 = icmp sgt i32 %38, %1
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.8, i64 0, i64 0))
br label %62
57: ; preds = %50
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%59 = call i32 @puts(i8* %20)
%60 = add nsw i32 %1, -1
%61 = call i32 @strncpy(i8* %0, i8* %20, i32 %60)
br label %62
62: ; preds = %48, %23, %16, %57, %55
%63 = phi i32 [ -1, %55 ], [ 0, %57 ], [ -1, %16 ], [ -1, %23 ], [ -1, %48 ]
br label %64
64: ; preds = %62, %14, %9
%65 = phi i32 [ -1, %9 ], [ -1, %14 ], [ %63, %62 ]
ret i32 %65
}