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

/* Variables and functions */
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ startuperror(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char *);
scalar_t__ unlink(char *);
void remove_lockfiles(char *dir) {
  DIR *dirdata;
  struct dirent *ent;
  struct stat statbuf;
  char tmpname[PATH_MAX];
  if (dir && *dir) {
    if ((dirdata = opendir(dir))) {
      while ((ent = readdir(dirdata))) {
        sprintf(tmpname, "%s%s%s", dir,
                (dir[strlen(dir) - 1] != '/') ? "/" : "", ent->d_name);
        stat(tmpname, &statbuf);
        if (S_ISDIR(statbuf.st_mode) == 0)
          if (strcmp(tmpname + strlen(tmpname) - 5, ".LOCK") == 0)
            if (unlink(tmpname) != 0)
              startuperror("Cannot unlink file %s: %s\n", tmpname,
                           strerror(errno));
      }
      closedir(dirdata);
    }
  }
}


x86 assembly (clang, -O0):
<remove_lockfiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     132a <remove_lockfiles+0x17a>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     132a <remove_lockfiles+0x17a>
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1325 <remove_lockfiles+0x175>
jmp    121f <remove_lockfiles+0x6f>
mov    -0x10(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     131c <remove_lockfiles+0x16c>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rdx
mov    -0x38(%rbp),%rdi
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xdbb(%rip),%rcx
lea    0xd92(%rip),%rax
cmp    $0x2f,%esi
cmovne %rax,%rcx
mov    -0x18(%rbp),%rax
mov    (%rax),%r8d
lea    0xd76(%rip),%rsi
call   10c0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <stat@plt>
mov    -0x20(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1317 <remove_lockfiles+0x167>
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffffb,%rdi
lea    0xd3e(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1312 <remove_lockfiles+0x162>
mov    -0x38(%rbp),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
je     130d <remove_lockfiles+0x15d>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xd07(%rip),%rdi
call   1030 <startuperror@plt>
jmp    1312 <remove_lockfiles+0x162>
jmp    1317 <remove_lockfiles+0x167>
jmp    121f <remove_lockfiles+0x6f>
mov    -0x10(%rbp),%rdi
call   1080 <closedir@plt>
jmp    132a <remove_lockfiles+0x17a>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_lockfiles>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%r12
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     12f9 <remove_lockfiles+0x149>
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
je     12f9 <remove_lockfiles+0x149>
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12f9 <remove_lockfiles+0x149>
mov    %rax,%r15
mov    %r12,-0x38(%rbp)
mov    %rax,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12ed <remove_lockfiles+0x13d>
mov    %rax,%r12
lea    -0x5(%rbx),%rax
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%r13
jmp    1244 <remove_lockfiles+0x94>
nopl   (%rax)
mov    %r15,%rdi
call   10b0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     12ed <remove_lockfiles+0x13d>
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%r14,1)
lea    0xdac(%rip),%rcx
lea    0xdc7(%rip),%rax
cmove  %rax,%rcx
mov    (%r12),%r8d
mov    %rbx,%rdi
lea    0xd8c(%rip),%rsi
mov    %r14,%rdx
call   10c0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <stat@plt>
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
jne    1230 <remove_lockfiles+0x80>
mov    %rbx,%rdi
call   1060 <strlen@plt>
movslq %eax,%rdi
add    -0x40(%rbp),%rdi
lea    0xd5f(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1230 <remove_lockfiles+0x80>
mov    %rbx,%rdi
call   1040 <unlink@plt>
test   %rax,%rax
je     1230 <remove_lockfiles+0x80>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd31(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <startuperror@plt>
jmp    1230 <remove_lockfiles+0x80>
mov    %r15,%rdi
call   1080 <closedir@plt>
mov    -0x38(%rbp),%r12
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [7 x i8] c"%s%s%s\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [6 x i8] c".LOCK\00"
@.str.4 = [27 x i8] c"Cannot unlink file %s: %s\0A\00"
@errno = global i32 0
define void @remove_lockfiles(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %2
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %6
%11 = alloca i8, i64 %9
store i64 %9, i64* %7
%12 = load i8*, i8** %2
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %71
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %71
19: ; preds = %14
%20 = load i8*, i8** %2
%21 = call i32* @opendir(i8* %20)
store i32* %21, i32** %3
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %70
23: ; preds = %19
br label %24
24: ; preds = %66, %23
%25 = load i32*, i32** %3
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %4
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %67
28: ; preds = %24
%29 = load i8*, i8** %2
%30 = load i8*, i8** %2
%31 = load i8*, i8** %2
%32 = call i32 @strlen(i8* %31)
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %30, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 47
%39 = zext i1 %38 to i64
%40 = select i1 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%41 = load %struct.dirent*, %struct.dirent** %4
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %29, i8* %40, i32 %43)
%45 = call i32 @stat(i8* %11, %struct.stat* %5)
%46 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i64 @S_ISDIR(i32 %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %66
50: ; preds = %28
%51 = call i32 @strlen(i8* %11)
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %11, i64 %52
%54 = getelementptr inbounds i8, i8* %53, i64 -5
%55 = call i64 @strcmp(i8* %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %65
57: ; preds = %50
%58 = call i64 @unlink(i8* %11)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = load i32, i32* @errno
%62 = call i32 @strerror(i32 %61)
%63 = call i32 @startuperror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %11, i32 %62)
br label %64
64: ; preds = %60, %57
br label %65
65: ; preds = %64, %50
br label %66
66: ; preds = %65, %28
br label %24
67: ; preds = %24
%68 = load i32*, i32** %3
%69 = call i32 @closedir(i32* %68)
br label %70
70: ; preds = %67, %19
br label %71
71: ; preds = %70, %14, %1
%72 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@PATH_MAX = global i32 0
@.str = [7 x i8] c"%s%s%s\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [6 x i8] c".LOCK\00"
@.str.4 = [27 x i8] c"Cannot unlink file %s: %s\0A\00"
@errno = global i32 0
define void @remove_lockfiles(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = icmp eq i8* %0, null
br i1 %8, label %55, label %9
9: ; preds = %1
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
br i1 %11, label %55, label %12
12: ; preds = %9
%13 = tail call i32* @opendir(i8* %0)
%14 = icmp eq i32* %13, null
br i1 %14, label %55, label %15
15: ; preds = %12
%16 = call %struct.dirent* @readdir(i32* %13)
%17 = icmp eq %struct.dirent* %16, null
br i1 %17, label %53, label %18
18: ; preds = %15
%19 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%20 = getelementptr inbounds i8, i8* %7, i64 -5
br label %21
21: ; preds = %18, %50
%22 = phi %struct.dirent* [ %16, %18 ], [ %51, %50 ]
%23 = call i32 @strlen(i8* %0)
%24 = add nsw i32 %23, -1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %0, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 47
%29 = select i1 %28, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %29, i32 %31)
%33 = call i32 @stat(i8* %7, %struct.stat* %2)
%34 = load i32, i32* %19
%35 = call i64 @S_ISDIR(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %50
37: ; preds = %21
%38 = call i32 @strlen(i8* %7)
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %20, i64 %39
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %37
%44 = call i64 @unlink(i8* %7)
%45 = icmp eq i64 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %43
%47 = load i32, i32* @errno
%48 = call i32 @strerror(i32 %47)
%49 = call i32 @startuperror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %7, i32 %48)
br label %50
50: ; preds = %37, %46, %43, %21
%51 = call %struct.dirent* @readdir(i32* %13)
%52 = icmp eq %struct.dirent* %51, null
br i1 %52, label %53, label %21
53: ; preds = %50, %15
%54 = call i32 @closedir(i32* %13)
br label %55
55: ; preds = %12, %53, %9, %1
ret void
}