#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 flock {
  int l_len;
  scalar_t__ l_pid;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int /*<<< orphan*/ flock;

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, char const *, char *);
char *strerror(int /*<<< orphan*/);
int vcfiled_is_running(const char *filename) {
  int ret;
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    ret = 0;
  } else {
    struct flock flock;
    memset(&flock, 0, sizeof(flock));
    flock.l_type = F_WRLCK;
    flock.l_whence = SEEK_SET;
    flock.l_start = 0;
    flock.l_len = 1;
    int rc = fcntl(fd, F_GETLK, &flock);
    if (rc != 0) {
      printf("%s: Could not access lockfile %s: %s\n", "vmcs_main", filename,
             strerror(errno));
      ret = 0;
    } else if (flock.l_pid == 0) {
      ret = 0;
    } else {
      ret = 1;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<vcfiled_is_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1187 <vcfiled_is_running+0x37>
movl   $0x0,-0xc(%rbp)
jmp    1244 <vcfiled_is_running+0xf4>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     121c <vcfiled_is_running+0xcc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfc(%rip),%rdi
lea    0xe1b(%rip),%rsi
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
jmp    123f <vcfiled_is_running+0xef>
cmpq   $0x0,-0x28(%rbp)
jne    1233 <vcfiled_is_running+0xe3>
movl   $0x0,-0xc(%rbp)
jmp    123a <vcfiled_is_running+0xea>
movl   $0x1,-0xc(%rbp)
jmp    123f <vcfiled_is_running+0xef>
jmp    1244 <vcfiled_is_running+0xf4>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vcfiled_is_running>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11f6 <vcfiled_is_running+0xa6>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
je     11fa <vcfiled_is_running+0xaa>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe1e(%rip),%rdi
lea    0xe3d(%rip),%rsi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <printf@plt>
jmp    1205 <vcfiled_is_running+0xb5>
xor    %ebx,%ebx
jmp    1205 <vcfiled_is_running+0xb5>
xor    %ebx,%ebx
cmpq   $0x0,0x10(%rsp)
setne  %bl
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i32, i32 }
@O_RDONLY = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str = [38 x i8] c"%s: Could not access lockfile %s: %s\0A\00"
@.str.1 = [10 x i8] c"vmcs_main\00"
@errno = global i32 0
define i32 @vcfiled_is_running(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* %3
br label %39
13: ; preds = %1
%14 = call i32 @memset(%struct.flock* %5, i32 0, i32 32)
%15 = load i32, i32* @F_WRLCK
%16 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 4
store i32 %15, i32* %16
%17 = load i32, i32* @SEEK_SET
%18 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i64 0, i64* %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i32 1, i32* %20
%21 = load i32, i32* %4
%22 = load i32, i32* @F_GETLK
%23 = call i32 @fcntl(i32 %21, i32 %22, %struct.flock* %5)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %13
%27 = load i8*, i8** %2
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %29)
store i32 0, i32* %3
br label %38
31: ; preds = %13
%32 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
%33 = load i64, i64* %32
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 0, i32* %3
br label %37
36: ; preds = %31
store i32 1, i32* %3
br label %37
37: ; preds = %36, %35
br label %38
38: ; preds = %37, %26
br label %39
39: ; preds = %38, %12
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32, i32 }
@O_RDONLY = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str = [38 x i8] c"%s: Could not access lockfile %s: %s\0A\00"
@.str.1 = [10 x i8] c"vmcs_main\00"
@errno = global i32 0
define i32 @vcfiled_is_running(i8* %0) {
%2 = alloca %struct.flock
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %29, label %6
6: ; preds = %1
%7 = bitcast %struct.flock* %2 to i8*
%8 = call i32 @memset(%struct.flock* %2, i32 0, i32 32)
%9 = load i32, i32* @F_WRLCK
%10 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SEEK_SET
%12 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i32 1, i32* %14
%15 = load i32, i32* @F_GETLK
%16 = call i32 @fcntl(i32 %4, i32 %15, %struct.flock* %2)
%17 = icmp eq i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %6
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %20)
br label %27
22: ; preds = %6
%23 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%24 = load i64, i64* %23
%25 = icmp ne i64 %24, 0
%26 = zext i1 %25 to i32
br label %27
27: ; preds = %22, %18
%28 = phi i32 [ 0, %18 ], [ %26, %22 ]
br label %29
29: ; preds = %1, %27
%30 = phi i32 [ %28, %27 ], [ 0, %1 ]
ret i32 %30
}