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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int /*<<< orphan*/);
pid_t lock_pid(char *filename) {
  if (filename == NULL)
    return -1;
  struct flock flockstr = {
      .l_start = 0,
      .l_len = 0,
      .l_whence = SEEK_SET,
      .l_type = F_WRLCK,
  };
  int fd = open(filename, O_RDONLY);
  if (fd == -1)
    return -1;
  if (fcntl(fd, F_GETLK, &flockstr) == -1) {
    close(fd);
    return -1;
  }
  if (flockstr.l_pid > 0) {
    close(fd);
    return flockstr.l_pid;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<lock_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1153 <lock_pid+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <lock_pid+0xdc>
movq   $0x0,-0x28(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11ac <lock_pid+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <lock_pid+0xdc>
mov    -0x2c(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11de <lock_pid+0xae>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <lock_pid+0xdc>
cmpq   $0x0,-0x28(%rbp)
jle    11fd <lock_pid+0xcd>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    120c <lock_pid+0xdc>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_pid>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
test   %rdi,%rdi
je     11ad <lock_pid+0x7d>
movq   $0x0,(%rsp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11bf <lock_pid+0x8f>
mov    %eax,%ebp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11b4 <lock_pid+0x84>
mov    (%rsp),%rbx
mov    %ebp,%edi
call   1040 <close@plt>
test   %rbx,%rbx
jle    11bd <lock_pid+0x8d>
mov    (%rsp),%ebx
jmp    11bf <lock_pid+0x8f>
mov    $0xffffffff,%ebx
jmp    11bf <lock_pid+0x8f>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11bf <lock_pid+0x8f>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDONLY = global i32 0
@F_GETLK = global i32 0
define i32 @lock_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.flock
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %44
9: ; preds = %1
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 1
%12 = load i32, i32* @F_WRLCK
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 2
%14 = load i32, i32* @SEEK_SET
store i32 %14, i32* %13
%15 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 3
store i32 0, i32* %15
%16 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 4
store i32 0, i32* %16
%17 = load i8*, i8** %3
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %9
store i32 -1, i32* %2
br label %44
23: ; preds = %9
%24 = load i32, i32* %5
%25 = load i32, i32* @F_GETLK
%26 = call i32 @fcntl(i32 %24, i32 %25, %struct.flock* %4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %2
br label %44
31: ; preds = %23
%32 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
%33 = load i64, i64* %32
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
store i32 %40, i32* %2
br label %44
41: ; preds = %31
%42 = load i32, i32* %5
%43 = call i32 @close(i32 %42)
store i32 0, i32* %2
br label %44
44: ; preds = %41, %35, %28, %22, %8
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@O_RDONLY = global i32 0
@F_GETLK = global i32 0
define i32 @lock_pid(i8* %0) {
%2 = alloca %struct.flock
%3 = icmp eq i8* %0, null
br i1 %3, label %31, label %4
4: ; preds = %1
%5 = bitcast %struct.flock* %2 to i8*
%6 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%8 = load i32, i32* @F_WRLCK
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
%10 = load i32, i32* @SEEK_SET
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 4
store i32 0, i32* %12
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %29, label %16
16: ; preds = %4
%17 = load i32, i32* @F_GETLK
%18 = call i32 @fcntl(i32 %14, i32 %17, %struct.flock* %2)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @close(i32 %14)
br label %29
22: ; preds = %16
%23 = load i64, i64* %6
%24 = icmp sgt i64 %23, 0
%25 = call i32 @close(i32 %14)
br i1 %24, label %26, label %29
26: ; preds = %22
%27 = load i64, i64* %6
%28 = trunc i64 %27 to i32
br label %29
29: ; preds = %22, %4, %26, %20
%30 = phi i32 [ -1, %20 ], [ %28, %26 ], [ -1, %4 ], [ 0, %22 ]
br label %31
31: ; preds = %1, %29
%32 = phi i32 [ %30, %29 ], [ -1, %1 ]
ret i32 %32
}