#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 */
typedef int /*<<< orphan*/ str;
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ ERANGE;
scalar_t__ ESRCH;
int /*<<< orphan*/ MySAR_macro_print_msg(char *, int, int, char *, char *);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ kill(scalar_t__, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
scalar_t__ unlink(char *);
pid_t MySAR_read_lock(char *file) {
  char str[20];
  int fd;
  pid_t pid;
  if ((fd = open(file, O_RDONLY)) < 0)
    return 0;
  pid = read(fd, str, sizeof(str));
  close(fd);
  if (pid <= 0)
    return 0;
  str[sizeof(str) - 1] = '\0';
  pid = strtol(str, NULL, 10);
  if (!pid || errno == ERANGE) {
    if (unlink(file) < 0)
      MySAR_macro_print_msg(
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/"
          "mysar-binary-importer/src/lock.c",
          94, 0x02, "Unable to remove broken lock %s", file);
    ;
    return 0;
  }
  if (kill(pid, 0) < 0 && errno == ESRCH) {
    if (unlink(file) < 0)
      MySAR_macro_print_msg(
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/"
          "mysar-binary-importer/src/lock.c",
          104, 0x02, "Unable to remove stale lock %s", file);
    ;
    return 0;
  }
  return pid;
}


x86 assembly (clang, -O0):
<MySAR_read_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11a7 <MySAR_read_lock+0x37>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <MySAR_read_lock+0x164>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1050 <read@plt>
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x40(%rbp)
jg     11dc <MySAR_read_lock+0x6c>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <MySAR_read_lock+0x164>
movb   $0x0,-0x1d(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     121b <MySAR_read_lock+0xab>
mov    0x2dc8(%rip),%rax
mov    (%rax),%rax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%rax
jne    125c <MySAR_read_lock+0xec>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    124f <MySAR_read_lock+0xdf>
mov    -0x10(%rbp),%r8
lea    0xdc7(%rip),%rdi
mov    $0x5e,%esi
mov    $0x2,%edx
lea    0xe58(%rip),%rcx
call   1090 <MySAR_macro_print_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <MySAR_read_lock+0x164>
mov    -0x40(%rbp),%rdi
xor    %esi,%esi
call   1070 <kill@plt>
cmp    $0x0,%rax
jge    12cc <MySAR_read_lock+0x15c>
mov    0x2d58(%rip),%rax
mov    (%rax),%rax
mov    0x2d3e(%rip),%rcx
cmp    (%rcx),%rax
jne    12cc <MySAR_read_lock+0x15c>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    12bf <MySAR_read_lock+0x14f>
mov    -0x10(%rbp),%r8
lea    0xd57(%rip),%rdi
mov    $0x68,%esi
mov    $0x2,%edx
lea    0xe08(%rip),%rcx
call   1090 <MySAR_macro_print_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <MySAR_read_lock+0x164>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MySAR_read_lock>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     120d <MySAR_read_lock+0x9d>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1050 <read@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1040 <close@plt>
test   %r15,%r15
jle    120d <MySAR_read_lock+0x9d>
movb   $0x0,0x13(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
test   %rax,%rax
je     11e0 <MySAR_read_lock+0x70>
mov    %rax,%rbx
mov    0x2dff(%rip),%r15
mov    (%r15),%rax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
jne    121c <MySAR_read_lock+0xac>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    120d <MySAR_read_lock+0x9d>
lea    0xe0c(%rip),%rdi
lea    0xea7(%rip),%rcx
mov    $0x5e,%esi
mov    $0x2,%edx
mov    %r14,%r8
call   1090 <MySAR_macro_print_msg@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <kill@plt>
test   %rax,%rax
jns    120f <MySAR_read_lock+0x9f>
mov    (%r15),%rax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
jne    120f <MySAR_read_lock+0x9f>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    120d <MySAR_read_lock+0x9d>
lea    0xdb2(%rip),%rdi
lea    0xe6d(%rip),%rcx
mov    $0x68,%esi
jmp    1200 <MySAR_read_lock+0x90>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@.str = [162 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/mysar-binary-importer/src/lock.c\00"
@.str.1 = [32 x i8] c"Unable to remove broken lock %s\00"
@ESRCH = global i64 0
@.str.2 = [31 x i8] c"Unable to remove stale lock %s\00"
define i64 @MySAR_read_lock(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca [20 x i8]
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i64 0, i64* %2
br label %57
12: ; preds = %1
%13 = load i32, i32* %5
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%15 = call i64 @read(i32 %13, i8* %14, i32 20)
store i64 %15, i64* %6
%16 = load i32, i32* %5
%17 = call i32 @close(i32 %16)
%18 = load i64, i64* %6
%19 = icmp sle i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %12
store i64 0, i64* %2
br label %57
21: ; preds = %12
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 19
store i8 0, i8* %22
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%24 = call i64 @strtol(i8* %23, i32* null, i32 10)
store i64 %24, i64* %6
%25 = load i64, i64* %6
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i64, i64* @errno
%29 = load i64, i64* @ERANGE
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %39
31: ; preds = %27, %21
%32 = load i8*, i8** %3
%33 = call i64 @unlink(i8* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i8*, i8** %3
%37 = call i32 @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 94, i32 2, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %31
store i64 0, i64* %2
br label %57
39: ; preds = %27
%40 = load i64, i64* %6
%41 = call i64 @kill(i64 %40, i32 0)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %55
43: ; preds = %39
%44 = load i64, i64* @errno
%45 = load i64, i64* @ESRCH
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %55
47: ; preds = %43
%48 = load i8*, i8** %3
%49 = call i64 @unlink(i8* %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i8*, i8** %3
%53 = call i32 @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 104, i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %51, %47
store i64 0, i64* %2
br label %57
55: ; preds = %43, %39
%56 = load i64, i64* %6
store i64 %56, i64* %2
br label %57
57: ; preds = %55, %54, %38, %20, %11
%58 = load i64, i64* %2
ret i64 %58
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@.str = [162 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/coffnix/mysar-ng/refs/heads/master/bin/mysar-binary-importer/src/lock.c\00"
@.str.1 = [32 x i8] c"Unable to remove broken lock %s\00"
@ESRCH = global i64 0
@.str.2 = [31 x i8] c"Unable to remove stale lock %s\00"
define i64 @MySAR_read_lock(i8* %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %36, label %7
7: ; preds = %1
%8 = call i64 @read(i32 %5, i8* %3, i32 20)
%9 = tail call i32 @close(i32 %5)
%10 = icmp slt i64 %8, 1
br i1 %10, label %36, label %11
11: ; preds = %7
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 19
store i8 0, i8* %12
%13 = call i64 @strtol(i8* nocapture %3, i32* null, i32 10)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i64, i64* @errno
%17 = load i64, i64* @ERANGE
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15, %11
%20 = tail call i64 @unlink(i8* %0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = tail call i32 @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 94, i32 2, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %36
24: ; preds = %15
%25 = tail call i64 @kill(i64 %13, i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @ESRCH
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = tail call i64 @unlink(i8* %0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 @MySAR_macro_print_msg(i8* getelementptr inbounds ([162 x i8], [162 x i8]* @.str, i64 0, i64 0), i32 104, i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %36
36: ; preds = %24, %27, %31, %34, %19, %22, %7, %1
%37 = phi i64 [ 0, %1 ], [ 0, %7 ], [ 0, %22 ], [ 0, %19 ], [ 0, %34 ], [ 0, %31 ], [ %13, %27 ], [ %13, %24 ]
ret i64 %37
}