#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*/ F_LOCK;
int /*<<< orphan*/ F_TEST;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int check_lockfile() {
  int fd = open("/tmp/tinywebserver.pid", O_RDWR | O_CREAT, S_IRWXU);
  if (lockf(fd, F_TEST, 0) == 0) {
    int lock = lockf(fd, F_LOCK, 0);
    printf("lock: %d\n", lock);
    return 0;
  } else {
    printf("file was locked\n");
  }
  return -1;
}


x86 assembly (clang, -O0):
<check_lockfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
mov    0x2eb0(%rip),%rax
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
lea    0xea6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
cmp    $0x0,%eax
jne    11b1 <check_lockfile+0x81>
mov    -0x8(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <lockf@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c6 <check_lockfile+0x96>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_lockfile>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e8e(%rip),%rax
mov    0x2ea7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e84(%rip),%rax
mov    (%rax),%edx
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
test   %eax,%eax
je     1197 <check_lockfile+0x57>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    11b9 <check_lockfile+0x79>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
lea    0xe67(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/tmp/tinywebserver.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@F_TEST = global i32 0
@F_LOCK = global i32 0
@.str.1 = [10 x i8] c"lock: %d\0A\00"
@.str.2 = [17 x i8] c"file was locked\0A\00"
define i32 @check_lockfile() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRWXU
%8 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = load i32, i32* @F_TEST
%11 = call i32 @lockf(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %0
%14 = load i32, i32* %2
%15 = load i32, i32* @F_LOCK
%16 = call i32 @lockf(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 0, i32* %1
br label %22
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19
store i32 -1, i32* %1
br label %22
22: ; preds = %21, %13
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [23 x i8] c"/tmp/tinywebserver.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@F_TEST = global i32 0
@F_LOCK = global i32 0
@.str.1 = [10 x i8] c"lock: %d\0A\00"
@str = [16 x i8] c"file was locked\00"
define i32 @check_lockfile() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRWXU
%5 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%6 = load i32, i32* @F_TEST
%7 = tail call i32 @lockf(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @F_LOCK
%11 = tail call i32 @lockf(i32 %5, i32 %10, i32 0)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %15
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = phi i32 [ 0, %9 ], [ -1, %13 ]
ret i32 %16
}