#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*/ pidb;
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ lock_build(char const *, char *, int);
int /*<<< orphan*/ lock_create(char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ unlink(char *);
int lock_try(const char *devname) {
  int fd;
  int len;
  int pid = 0;
  char name[256];
  char pidb[21];
  lock_build(devname, name, sizeof(name));
  fd = open(name, O_RDONLY);
  if (fd >= 0) {
    len = read(fd, pidb, sizeof(pidb) - 1);
    if (len > 0) {
      pidb[len] = 0;
      len = strtol(pidb, NULL, 10);
      if (kill(len, 0) == 0)
        pid = len;
    }
    close(fd);
  }
  if (pid == 0) {
    unlink(name);
    lock_create(name);
  }
  return pid;
}


x86 assembly (clang, -O0):
<lock_try>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <lock_build@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1239 <lock_try+0xb9>
mov    -0xc(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x14,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1231 <lock_try+0xb1>
movslq -0x10(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1080 <kill@plt>
cmp    $0x0,%rax
jne    122c <lock_try+0xac>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1231 <lock_try+0xb1>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x14(%rbp)
jne    125b <lock_try+0xdb>
lea    -0x120(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x120(%rbp),%rdi
call   10a0 <lock_create@plt>
mov    -0x14(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_try>:
push   %rbp
push   %rbx
sub    $0x128,%rsp
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x100,%edx
call   1060 <lock_build@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     11f1 <lock_try+0x71>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x14,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11ea <lock_try+0x6a>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1080 <kill@plt>
test   %rax,%rax
je     1214 <lock_try+0x94>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
call   10a0 <lock_create@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x128,%rsp
pop    %rbx
pop    %rbp
ret
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
jne    1208 <lock_try+0x88>
jmp    11f1 <lock_try+0x71>

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @lock_try(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca [21 x i8]
store i8* %0, i8** %2
store i32 0, i32* %5
%8 = load i8*, i8** %2
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = call i32 @lock_build(i8* %8, i8* %9, i32 256)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %1
%17 = load i32, i32* %3
%18 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 20)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %16
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%27 = call i32 @strtol(i8* %26, i32* null, i32 10)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = call i64 @kill(i32 %28, i32 0)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = load i32, i32* %4
store i32 %32, i32* %5
br label %33
33: ; preds = %31, %22
br label %34
34: ; preds = %33, %16
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34, %1
%38 = load i32, i32* %5
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%42 = call i32 @unlink(i8* %41)
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%44 = call i32 @lock_create(i8* %43)
br label %45
45: ; preds = %40, %37
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @lock_try(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [21 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%6 = call i32 @lock_build(i8* %0, i8* %4, i32 256)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %4, i32 %7)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %24
10: ; preds = %1
%11 = call i32 @read(i32 %8, i8* %5, i32 20)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = zext i32 %11 to i64
%15 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @strtol(i8* nocapture %5, i32* null, i32 10)
%17 = call i64 @kill(i32 %16, i32 0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %10, %13
%20 = call i32 @close(i32 %8)
br label %24
21: ; preds = %13
%22 = call i32 @close(i32 %8)
%23 = icmp eq i32 %16, 0
br i1 %23, label %24, label %27
24: ; preds = %1, %19, %21
%25 = call i32 @unlink(i8* %4)
%26 = call i32 @lock_create(i8* %4)
br label %27
27: ; preds = %24, %21
%28 = phi i32 [ 0, %24 ], [ %16, %21 ]
ret i32 %28
}