#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;
};
typedef int /*<<< orphan*/ lockfilename;

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int islocked(char *filename) {
  char lockfilename[PATH_MAX + 5];
  struct stat statbuf;
  if (!filename)
    return 0;
  if (strlen(filename) + 5 >= sizeof(lockfilename))
    return 0;
  strcpy(lockfilename, filename);
  strcat(lockfilename, ".LOCK");
  if (stat(lockfilename, &statbuf))
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<islocked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
add    $0x5,%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%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,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119d <islocked+0x5d>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1220 <islocked+0xe0>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rcx
add    $0x5,%eax
cltq
cmp    %rcx,%rax
jb     11cb <islocked+0x8b>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1220 <islocked+0xe0>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x38(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1212 <islocked+0xd2>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1220 <islocked+0xe0>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<islocked>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e9a(%rip),%rax
mov    (%rax),%ebx
add    $0x5,%ebx
mov    %rsp,%r12
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
xor    %r15d,%r15d
test   %rdi,%rdi
je     11b6 <islocked+0x76>
mov    %rdi,%r14
call   1040 <strlen@plt>
add    $0x5,%eax
cltq
cmp    %rbx,%rax
jae    11b6 <islocked+0x76>
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    0xe68(%rip),%rsi
mov    %r12,%rdi
call   1060 <strcat@plt>
lea    -0x28(%rbp),%rsi
mov    %r12,%rdi
call   1050 <stat@plt>
xor    %r15d,%r15d
test   %rax,%rax
sete   %r15b
mov    %r15d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c".LOCK\00"
define i32 @islocked(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.stat
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = add nsw i32 %8, 5
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %7
br label %31
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 5
%20 = sext i32 %19 to i64
%21 = icmp uge i64 %20, %10
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 0, i32* %2
store i32 1, i32* %7
br label %31
23: ; preds = %16
%24 = load i8*, i8** %3
%25 = call i32 @strcpy(i8* %12, i8* %24)
%26 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%27 = call i64 @stat(i8* %12, %struct.stat* %6)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 0, i32* %2
store i32 1, i32* %7
br label %31
30: ; preds = %23
store i32 1, i32* %2
store i32 1, i32* %7
br label %31
31: ; preds = %30, %29, %22, %15
%32 = load i8*, i8** %4
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [6 x i8] c".LOCK\00"
define i32 @islocked(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 5
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %2 to i8*
%8 = icmp eq i8* %0, null
br i1 %8, label %20, label %9
9: ; preds = %1
%10 = tail call i32 @strlen(i8* %0)
%11 = add nsw i32 %10, 5
%12 = sext i32 %11 to i64
%13 = icmp ult i64 %12, %5
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = call i32 @strcpy(i8* %6, i8* %0)
%16 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%17 = call i64 @stat(i8* %6, %struct.stat* %2)
%18 = icmp eq i64 %17, 0
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %14, %9, %1
%21 = phi i32 [ 0, %1 ], [ 0, %9 ], [ %19, %14 ]
ret i32 %21
}