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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *, char *, char *);
int /*<<< orphan*/ printd(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int CheckLock(char *name) {
  FILE *lock_fd;
  int retu = -1;
  FILE *proc_fd;
  char buff1[10], buff2[20], buff3[20];
  char buff[30];
  pid_t pid;
  if ((lock_fd = fopen(name, "r")) != NULL) {
    printf("LOCKFILE %s exists\n", name);
    fscanf(lock_fd, "%08s %18s %18s", buff1, buff2, buff3);
    pid = atoi(buff1);
    if (pid > 0) {
      printf("  file carries: %s %s %s\n", buff1, buff2, buff3);
      sprintf(buff, "/proc/%d/comm", pid);
      printf("  searching inside of /proc for: %s\n", buff);
    }
    if ((proc_fd = fopen(buff, "r")) != NULL) {
      fprintf(stderr, "%s with PID:%d already exists, started by user %s\n",
              buff2, pid, buff3);
      retu = pid;
    } else {
      printf("  PID:%d does not exist, LOCKFILE can be deleted\n", pid);
      retu = 0;
    }
  } else {
    printd("LOCKFILE [%s] does not exists\n", name);
    retu = 0;
  }
  return retu;
}


x86 assembly (clang, -O0):
<CheckLock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     129f <CheckLock+0x12f>
mov    -0x8(%rbp),%rsi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x2a(%rbp),%rdx
lea    -0x40(%rbp),%rcx
lea    -0x60(%rbp),%r8
lea    0xe49(%rip),%rsi
call   1050 <fscanf@plt>
lea    -0x2a(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jle    1230 <CheckLock+0xc0>
lea    -0x2a(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x60(%rbp),%rcx
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    -0x84(%rbp),%edx
lea    0xe26(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x80(%rbp),%rsi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x80(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     127f <CheckLock+0x10f>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rdx
mov    -0x84(%rbp),%ecx
lea    -0x60(%rbp),%r8
lea    0xe06(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x84(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    129a <CheckLock+0x12a>
mov    -0x84(%rbp),%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
jmp    12b6 <CheckLock+0x146>
mov    -0x8(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1030 <printd@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CheckLock>:
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rdi,%rbx
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     124f <CheckLock+0xdf>
mov    %rax,%r14
lea    0xe69(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe6c(%rip),%rsi
lea    0x6(%rsp),%rbx
lea    0x30(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %r14,%rdi
mov    %rbx,%rdx
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1216 <CheckLock+0xa6>
lea    0xe4c(%rip),%rdi
lea    0x6(%rsp),%rsi
lea    0x30(%rsp),%rdx
lea    0x10(%rsp),%rcx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe49(%rip),%rsi
lea    0x50(%rsp),%r14
mov    %r14,%rdi
mov    %ebx,%edx
call   1090 <sprintf@plt>
lea    0xe41(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xde3(%rip),%rsi
lea    0x50(%rsp),%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1260 <CheckLock+0xf0>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
lea    0x30(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    %ebx,%ecx
call   1060 <fprintf@plt>
jmp    1272 <CheckLock+0x102>
lea    0xe81(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printd@plt>
jmp    1270 <CheckLock+0x100>
lea    0xe3e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"LOCKFILE %s exists\0A\00"
@.str.2 = [15 x i8] c"%08s %18s %18s\00"
@.str.3 = [26 x i8] c" file carries: %s %s %s\0A\00"
@.str.4 = [14 x i8] c"/proc/%d/comm\00"
@.str.5 = [37 x i8] c" searching inside of /proc for: %s\0A\00"
@stderr = global i32 0
@.str.6 = [51 x i8] c"%s with PID:%d already exists, started by user %s\0A\00"
@.str.7 = [50 x i8] c" PID:%d does not exist, LOCKFILE can be deleted\0A\00"
@.str.8 = [31 x i8] c"LOCKFILE [%s] does not exists\0A\00"
define i32 @CheckLock(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [10 x i8]
%7 = alloca [20 x i8]
%8 = alloca [20 x i8]
%9 = alloca [30 x i8]
%10 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %4
%11 = load i8*, i8** %2
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %51
14: ; preds = %1
%15 = load i8*, i8** %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32*, i32** %3
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%21 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %18, i8* %19, i8* %20)
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %14
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %27, i8* %28, i8* %29)
%31 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%32 = load i32, i32* %10
%33 = call i32 @sprintf(i8* %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %26, %14
%37 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %38, i32** %5
%39 = icmp ne i32* %38, null
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%43 = load i32, i32* %10
%44 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%45 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0), i8* %42, i32 %43, i8* %44)
%46 = load i32, i32* %10
store i32 %46, i32* %4
br label %50
47: ; preds = %36
%48 = load i32, i32* %10
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0), i32 %48)
store i32 0, i32* %4
br label %50
50: ; preds = %47, %40
br label %54
51: ; preds = %1
%52 = load i8*, i8** %2
%53 = call i32 @printd(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %52)
store i32 0, i32* %4
br label %54
54: ; preds = %51, %50
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [20 x i8] c"LOCKFILE %s exists\0A\00"
@.str.2 = [15 x i8] c"%08s %18s %18s\00"
@.str.3 = [26 x i8] c" file carries: %s %s %s\0A\00"
@.str.4 = [14 x i8] c"/proc/%d/comm\00"
@.str.5 = [37 x i8] c" searching inside of /proc for: %s\0A\00"
@stderr = global i32 0
@.str.6 = [51 x i8] c"%s with PID:%d already exists, started by user %s\0A\00"
@.str.7 = [50 x i8] c" PID:%d does not exist, LOCKFILE can be deleted\0A\00"
@.str.8 = [31 x i8] c"LOCKFILE [%s] does not exists\0A\00"
define i32 @CheckLock(i8* %0) {
%2 = alloca [10 x i8]
%3 = alloca [20 x i8]
%4 = alloca [20 x i8]
%5 = alloca [30 x i8]
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %5, i64 0, i64 0
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %29, label %12
12: ; preds = %1
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %6, i8* %7, i8* %8)
%15 = call i32 @atoi(i8* %6)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %7, i8* %8)
%19 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %9)
br label %21
21: ; preds = %17, %12
%22 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.6, i64 0, i64 0), i8* %7, i32 %15, i8* %8)
br label %31
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.7, i64 0, i64 0), i32 %15)
br label %31
29: ; preds = %1
%30 = tail call i32 @printd(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %24, %27, %29
%32 = phi i32 [ %15, %24 ], [ 0, %27 ], [ 0, %29 ]
ret i32 %32
}