#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int R_OK;
int W_OK;
scalar_t__ access(char *, int);
int /*<<< orphan*/ close(int);
int get_lockname(char *, char *);
scalar_t__ kill(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ unlink(char *);
int write_lockfile(char *, char *);
int lock_tty(char *name, char *caller) {
  char lockname[256], buf[256];
  int fd, len, pid = 0;
  if (access("/var/lock", R_OK | W_OK) < 0)
    return 0;
  if (get_lockname(name, lockname) == -1)
    return -1;
  fd = open(lockname, O_RDONLY);
  if (fd < 0)
    return write_lockfile(lockname, caller);
  len = read(fd, buf, 256 - 1);
  close(fd);
  if (len < 0)
    return -1;
  buf[len] = 0;
  sscanf(buf, "%d", &pid);
  if (pid != 0) {
    if (kill(pid, 0) >= 0)
      return -1;
  }
  unlink(lockname);
  return write_lockfile(lockname, caller);
}


x86 assembly (clang, -O0):
<lock_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x22c(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
or     (%rax),%esi
lea    0xe3a(%rip),%rdi
call   10b0 <access@plt>
cmp    $0x0,%rax
jge    11e1 <lock_tty+0x51>
movl   $0x0,-0x4(%rbp)
jmp    1304 <lock_tty+0x174>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1050 <get_lockname@plt>
cmp    $0xffffffff,%eax
jne    1206 <lock_tty+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <lock_tty+0x174>
lea    -0x120(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x224(%rbp)
cmpl   $0x0,-0x224(%rbp)
jge    1246 <lock_tty+0xb6>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <write_lockfile@plt>
mov    %eax,-0x4(%rbp)
jmp    1304 <lock_tty+0x174>
mov    -0x224(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x228(%rbp)
mov    -0x224(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x228(%rbp)
jge    1287 <lock_tty+0xf7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <lock_tty+0x174>
movslq -0x228(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
lea    0xd66(%rip),%rsi
lea    -0x22c(%rbp),%rdx
call   1090 <sscanf@plt>
cmpl   $0x0,-0x22c(%rbp)
je     12e5 <lock_tty+0x155>
mov    -0x22c(%rbp),%edi
xor    %esi,%esi
call   1070 <kill@plt>
cmp    $0x0,%rax
jl     12e0 <lock_tty+0x150>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <lock_tty+0x174>
jmp    12e5 <lock_tty+0x155>
lea    -0x120(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <write_lockfile@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_tty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
movl   $0x0,0xc(%rsp)
mov    0x2e1e(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3c(%rip),%rdi
call   10b0 <access@plt>
test   %rax,%rax
js     127c <lock_tty+0xec>
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <get_lockname@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     127e <lock_tty+0xee>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     126b <lock_tty+0xdb>
mov    %eax,%ebx
lea    0x110(%rsp),%rsi
mov    %eax,%edi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1040 <close@plt>
test   %r15d,%r15d
js     127e <lock_tty+0xee>
mov    %r15d,%eax
movb   $0x0,0x110(%rsp,%rax,1)
lea    0xdcf(%rip),%rsi
lea    0x110(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1090 <sscanf@plt>
mov    0xc(%rsp),%edi
test   %edi,%edi
je     1261 <lock_tty+0xd1>
xor    %esi,%esi
call   1070 <kill@plt>
test   %rax,%rax
jns    127e <lock_tty+0xee>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
call   1080 <write_lockfile@plt>
mov    %eax,%ebp
jmp    127e <lock_tty+0xee>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/var/lock\00"
@R_OK = global i32 0
@W_OK = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @lock_tty(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %10
%11 = load i32, i32* @R_OK
%12 = load i32, i32* @W_OK
%13 = or i32 %11, %12
%14 = call i64 @access(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 0, i32* %3
br label %62
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%20 = call i32 @get_lockname(i8* %18, i8* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 -1, i32* %3
br label %62
23: ; preds = %17
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%31 = load i8*, i8** %5
%32 = call i32 @write_lockfile(i8* %30, i8* %31)
store i32 %32, i32* %3
br label %62
33: ; preds = %23
%34 = load i32, i32* %8
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 255)
store i32 %36, i32* %9
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %33
store i32 -1, i32* %3
br label %62
42: ; preds = %33
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %44
store i8 0, i8* %45
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%47 = call i32 @sscanf(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %10)
%48 = load i32, i32* %10
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %42
%51 = load i32, i32* %10
%52 = call i64 @kill(i32 %51, i32 0)
%53 = icmp sge i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
store i32 -1, i32* %3
br label %62
55: ; preds = %50
br label %56
56: ; preds = %55, %42
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%58 = call i32 @unlink(i8* %57)
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%60 = load i8*, i8** %5
%61 = call i32 @write_lockfile(i8* %59, i8* %60)
store i32 %61, i32* %3
br label %62
62: ; preds = %56, %54, %41, %29, %22, %16
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [10 x i8] c"/var/lock\00"
@R_OK = global i32 0
@W_OK = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"%d\00"
define i32 @lock_tty(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%9 = load i32, i32* @R_OK
%10 = load i32, i32* @W_OK
%11 = or i32 %10, %9
%12 = tail call i64 @access(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %38, label %14
14: ; preds = %2
%15 = call i32 @get_lockname(i8* %0, i8* %6)
%16 = icmp eq i32 %15, -1
br i1 %16, label %38, label %17
17: ; preds = %14
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %6, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %36, label %21
21: ; preds = %17
%22 = call i32 @read(i32 %19, i8* %7, i32 255)
%23 = call i32 @close(i32 %19)
%24 = icmp slt i32 %22, 0
br i1 %24, label %38, label %25
25: ; preds = %21
%26 = zext i32 %22 to i64
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %26
store i8 0, i8* %27
%28 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %25
%32 = call i64 @kill(i32 %29, i32 0)
%33 = icmp sgt i64 %32, -1
br i1 %33, label %38, label %34
34: ; preds = %31, %25
%35 = call i32 @unlink(i8* %6)
br label %36
36: ; preds = %17, %34
%37 = call i32 @write_lockfile(i8* %6, i8* %1)
br label %38
38: ; preds = %36, %31, %21, %14, %2
%39 = phi i32 [ 0, %2 ], [ -1, %14 ], [ -1, %21 ], [ -1, %31 ], [ %37, %36 ]
ret i32 %39
}