#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__ ENOENT;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int lock_reg(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
             int /*<<< orphan*/, int /*<<< orphan*/);
int lock_test(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
              int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
char *strerror(scalar_t__);
int main(int argc, char **argv) {
  int ret = 0;
  int fid;
  if (argc < 2) {
    printf("Usage: checkLock filename\n"
           "Checks for ability to get advisory write lock on file filename.\n"
           "Returns 0 on success, 1 on failure.\n"
           "--checkSetLock option: Print warning if setting lock fails.\n");
    return -1;
  }
  if (0 == strcmp("--checkSetLock", argv[1])) {
    const char filename[] = "/proc/self/exe";
    int fid = open(filename, O_RDONLY, 0);
    if (fid >= 0) {
      ret = lock_reg(fid, F_SETLK, F_RDLCK, 0, SEEK_SET, 0);
      if (ret < 0) {
        printf(
            "WARNING: fcntl locks are not working.\nRunning processes are not"
            " protected against overwrite from other computers.\n"
            "fcntl call returned %d\n",
            ret);
        return 0;
      }
      lock_reg(fid, F_SETLK, F_UNLCK, 0, SEEK_SET, 0);
      close(fid);
    }
    return 0;
  } else {
    fid = open(argv[1], O_RDONLY, 0);
    if (fid > 0) {
      ret = lock_test(fid, F_WRLCK, 0, SEEK_SET, 0);
      if (ret == 1)
        printf("checkLock: File %s is locked.\n", argv[1]);
      else if (ret > 0)
        printf("checkLock: File %s is locked by PID %d\n", argv[1], ret);
      close(fid);
    } else {
      if (errno != ENOENT) {
        printf("File %s can't be opened: %s\n", argv[1], strerror(errno));
        ret = 1;
      }
    }
    return (ret != 0 ? 1 : 0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b1 <main+0x41>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b9 <main+0x249>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xefb(%rip),%rdi
call   1070 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12ae <main+0x13e>
mov    0xef0(%rip),%rax
mov    %rax,-0x27(%rbp)
mov    0xeee(%rip),%eax
mov    %eax,-0x1f(%rbp)
mov    0xee8(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xee0(%rip),%al
mov    %al,-0x19(%rbp)
lea    -0x27(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     12a2 <main+0x132>
mov    -0x2c(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    0x2d88(%rip),%rax
mov    (%rax),%r8d
xor    %r9d,%r9d
mov    %r9d,%ecx
call   1040 <lock_reg@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1270 <main+0x100>
mov    -0x14(%rbp),%esi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13b9 <main+0x249>
mov    -0x2c(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    0x2d55(%rip),%rax
mov    (%rax),%edx
mov    0x2d34(%rip),%rax
mov    (%rax),%r8d
xor    %r9d,%r9d
mov    %r9d,%ecx
call   1040 <lock_reg@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13b9 <main+0x249>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1350 <main+0x1e0>
mov    -0x18(%rbp),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
mov    0x2cda(%rip),%rax
mov    (%rax),%ecx
xor    %r8d,%r8d
mov    %r8d,%edx
call   1030 <lock_test@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    131b <main+0x1ab>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1343 <main+0x1d3>
cmpl   $0x0,-0x14(%rbp)
jle    133e <main+0x1ce>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x14(%rbp),%edx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1343 <main+0x1d3>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    13a6 <main+0x236>
mov    0x2c71(%rip),%rax
mov    (%rax),%rax
mov    0x2c47(%rip),%rcx
cmp    (%rcx),%rax
je     13a1 <main+0x231>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c4b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    13a6 <main+0x236>
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jg     11a5 <main+0x25>
lea    0xf76(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1316 <main+0x196>
mov    %rsi,%rbx
mov    0x8(%rsi),%rsi
lea    0xe4d(%rip),%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1220 <main+0xa0>
mov    0x8(%rbx),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
jle    12b4 <main+0x134>
mov    %eax,%r14d
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%ecx
mov    %r14d,%edi
xor    %edx,%edx
xor    %r8d,%r8d
call   1030 <lock_test@plt>
mov    %eax,%r15d
cmp    $0x1,%eax
jne    12ee <main+0x16e>
mov    0x8(%rbx),%rsi
lea    0xe94(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1305 <main+0x185>
movabs $0x6578652f666c65,%rax
mov    %rax,0xf(%rsp)
movabs $0x65732f636f72702f,%rax
2f 73 65
mov    %rax,0x8(%rsp)
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
xor    %ebp,%ebp
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1316 <main+0x196>
mov    %eax,%ebx
mov    0x2d7a(%rip),%r14
mov    (%r14),%esi
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
mov    0x2d47(%rip),%r15
mov    (%r15),%r8d
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1050 <lock_reg@plt>
test   %eax,%eax
js     1323 <main+0x1a3>
mov    (%r14),%esi
mov    0x2d3e(%rip),%rax
mov    (%rax),%edx
mov    (%r15),%r8d
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1050 <lock_reg@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1316 <main+0x196>
mov    0x2d0d(%rip),%rax
mov    (%rax),%rdi
mov    0x2ce3(%rip),%rax
cmp    (%rax),%rdi
je     1316 <main+0x196>
mov    0x8(%rbx),%rbx
call   10a0 <strerror@plt>
lea    0xe15(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    1316 <main+0x196>
jl     1305 <main+0x185>
mov    0x8(%rbx),%rsi
lea    0xdcc(%rip),%rdi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %ebp,%ebp
test   %r15d,%r15d
setne  %bpl
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1316 <main+0x196>

LLVM IR (-O0):
@.str = [187 x i8] c"Usage: checkLock filename\0AChecks for ability to get advisory write lock on file filename.\0AReturns 0 on success, 1 on failure.\0A--checkSetLock option: Print warning if setting lock fails.\0A\00"
@.str.1 = [15 x i8] c"--checkSetLock\00"
@__const.main.filename = [15 x i8] c"/proc/self/exe\00"
@O_RDONLY = global i32 0
@F_SETLK = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [138 x i8] c"WARNING: fcntl locks are not working.\0ARunning processes are not protected against overwrite from other computers.\0Afcntl call returned %d\0A\00"
@F_UNLCK = global i32 0
@F_WRLCK = global i32 0
@.str.3 = [31 x i8] c"checkLock: File %s is locked.\0A\00"
@.str.4 = [40 x i8] c"checkLock: File %s is locked by PID %d\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [29 x i8] c"File %s can't be opened: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [15 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([187 x i8], [187 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %97
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = icmp eq i64 0, %18
br i1 %19, label %20, label %47
20: ; preds = %14
%21 = bitcast [15 x i8]* %8 to i8*
%22 = getelementptr inbounds [15 x i8], [15 x i8]* %8, i64 0, i64 0
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23, i32 0)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %46
27: ; preds = %20
%28 = load i32, i32* %9
%29 = load i32, i32* @F_SETLK
%30 = load i32, i32* @F_RDLCK
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @lock_reg(i32 %28, i32 %29, i32 %30, i32 0, i32 %31, i32 0)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = load i32, i32* %6
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str.2, i64 0, i64 0), i32 %36)
store i32 0, i32* %3
br label %97
38: ; preds = %27
%39 = load i32, i32* %9
%40 = load i32, i32* @F_SETLK
%41 = load i32, i32* @F_UNLCK
%42 = load i32, i32* @SEEK_SET
%43 = call i32 @lock_reg(i32 %39, i32 %40, i32 %41, i32 0, i32 %42, i32 0)
%44 = load i32, i32* %9
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %38, %20
store i32 0, i32* %3
br label %97
47: ; preds = %14
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = load i32, i32* @O_RDONLY
%52 = call i32 @open(i8* %50, i32 %51, i32 0)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %80
55: ; preds = %47
%56 = load i32, i32* %7
%57 = load i32, i32* @F_WRLCK
%58 = load i32, i32* @SEEK_SET
%59 = call i32 @lock_test(i32 %56, i32 %57, i32 0, i32 %58, i32 0)
store i32 %59, i32* %6
%60 = load i32, i32* %6
%61 = icmp eq i32 %60, 1
br i1 %61, label %62, label %67
62: ; preds = %55
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 1
%65 = load i8*, i8** %64
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %77
67: ; preds = %55
%68 = load i32, i32* %6
%69 = icmp sgt i32 %68, 0
br i1 %69, label %70, label %76
70: ; preds = %67
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 1
%73 = load i8*, i8** %72
%74 = load i32, i32* %6
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %73, i32 %74)
br label %76
76: ; preds = %70, %67
br label %77
77: ; preds = %76, %62
%78 = load i32, i32* %7
%79 = call i32 @close(i32 %78)
br label %92
80: ; preds = %47
%81 = load i64, i64* @errno
%82 = load i64, i64* @ENOENT
%83 = icmp ne i64 %81, %82
br i1 %83, label %84, label %91
84: ; preds = %80
%85 = load i8**, i8*** %5
%86 = getelementptr inbounds i8*, i8** %85, i64 1
%87 = load i8*, i8** %86
%88 = load i64, i64* @errno
%89 = call i8* @strerror(i64 %88)
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %87, i8* %89)
store i32 1, i32* %6
br label %91
91: ; preds = %84, %80
br label %92
92: ; preds = %91, %77
%93 = load i32, i32* %6
%94 = icmp ne i32 %93, 0
%95 = zext i1 %94 to i64
%96 = select i1 %94, i32 1, i32 0
store i32 %96, i32* %3
br label %97
97: ; preds = %92, %46, %35, %12
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
@.str.1 = [15 x i8] c"--checkSetLock\00"
@__const.main.filename = [15 x i8] c"/proc/self/exe\00"
@O_RDONLY = global i32 0
@F_SETLK = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [138 x i8] c"WARNING: fcntl locks are not working.\0ARunning processes are not protected against overwrite from other computers.\0Afcntl call returned %d\0A\00"
@F_UNLCK = global i32 0
@F_WRLCK = global i32 0
@.str.3 = [31 x i8] c"checkLock: File %s is locked.\0A\00"
@.str.4 = [40 x i8] c"checkLock: File %s is locked by PID %d\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [29 x i8] c"File %s can't be opened: %s\0A\00"
@str = [186 x i8] c"Usage: checkLock filename\0AChecks for ability to get advisory write lock on file filename.\0AReturns 0 on success, 1 on failure.\0A--checkSetLock option: Print warning if setting lock fails.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [15 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([186 x i8], [186 x i8]* @str, i64 0, i64 0))
br label %62
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @strcmp(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %32
12: ; preds = %7
%13 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14, i32 0)
%16 = icmp sgt i32 %15, -1
br i1 %16, label %17, label %31
17: ; preds = %12
%18 = load i32, i32* @F_SETLK
%19 = load i32, i32* @F_RDLCK
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @lock_reg(i32 %15, i32 %18, i32 %19, i32 0, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([138 x i8], [138 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %31
25: ; preds = %17
%26 = load i32, i32* @F_SETLK
%27 = load i32, i32* @F_UNLCK
%28 = load i32, i32* @SEEK_SET
%29 = tail call i32 @lock_reg(i32 %15, i32 %26, i32 %27, i32 0, i32 %28, i32 0)
%30 = tail call i32 @close(i32 %15)
br label %31
31: ; preds = %12, %25, %23
br label %62
32: ; preds = %7
%33 = load i8*, i8** %8
%34 = load i32, i32* @O_RDONLY
%35 = tail call i32 @open(i8* %33, i32 %34, i32 0)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %54
37: ; preds = %32
%38 = load i32, i32* @F_WRLCK
%39 = load i32, i32* @SEEK_SET
%40 = tail call i32 @lock_test(i32 %35, i32 %38, i32 0, i32 %39, i32 0)
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i8*, i8** %8
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %50
45: ; preds = %37
%46 = icmp sgt i32 %40, 0
br i1 %46, label %47, label %50
47: ; preds = %45
%48 = load i8*, i8** %8
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i8* %48, i32 %40)
br label %50
50: ; preds = %45, %47, %42
%51 = tail call i32 @close(i32 %35)
%52 = icmp ne i32 %40, 0
%53 = zext i1 %52 to i32
br label %62
54: ; preds = %32
%55 = load i64, i64* @errno
%56 = load i64, i64* @ENOENT
%57 = icmp eq i64 %55, %56
br i1 %57, label %62, label %58
58: ; preds = %54
%59 = load i8*, i8** %8
%60 = tail call i8* @strerror(i64 %55)
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %59, i8* %60)
br label %62
62: ; preds = %50, %58, %54, %31, %5
%63 = phi i32 [ -1, %5 ], [ 0, %31 ], [ %53, %50 ], [ 1, %58 ], [ 0, %54 ]
ret i32 %63
}