#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_type;
  int l_pid;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_RDLCK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int lock_test(int fd, struct flock *lock) {
  if (fcntl(fd, F_GETLK, lock) == 0) {
    if (lock->l_type == F_UNLCK) {
      printf("lock can be set in fd\n");
      return 0;
    } else {
      if (lock->l_type == F_RDLCK) {
        printf("can't set lock, read lock has been set by: %d\n", lock->l_pid);
      } else if (lock->l_type == F_WRLCK) {
        printf("can't set lock, write lock has been set by: %d\n", lock->l_pid);
      }
      return -2;
    }
  } else {
    perror("get incompatible locks fail!\n");
    return -1;
  }
}


x86 assembly (clang, -O0):
<lock_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jne    11fd <lock_test+0xcd>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e74(%rip),%rcx
cmp    (%rcx),%rax
jne    118f <lock_test+0x5f>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1210 <lock_test+0xe0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <lock_test+0x90>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f1 <lock_test+0xc1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <lock_test+0xbc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f1 <lock_test+0xc1>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1210 <lock_test+0xe0>
lea    0xe72(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_test>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
call   1040 <fcntl@plt>
test   %rax,%rax
jne    11c3 <lock_test+0x83>
mov    (%rbx),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
jne    117b <lock_test+0x3b>
lea    0xf0b(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
jmp    11d4 <lock_test+0x94>
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <lock_test+0x5f>
mov    0x8(%rbx),%esi
lea    0xe6f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xfffffffe,%ebp
jmp    11d4 <lock_test+0x94>
mov    0x2e2a(%rip),%rcx
mov    $0xfffffffe,%ebp
cmp    (%rcx),%rax
jne    11d4 <lock_test+0x94>
mov    0x8(%rbx),%esi
lea    0xe75(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d4 <lock_test+0x94>
lea    0xe95(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32 }
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@.str = [23 x i8] c"lock can be set in fd\0A\00"
@F_RDLCK = global i64 0
@.str.1 = [47 x i8] c"can't set lock, read lock has been set by: %d\0A\00"
@F_WRLCK = global i64 0
@.str.2 = [48 x i8] c"can't set lock, write lock has been set by: %d\0A\00"
@.str.3 = [30 x i8] c"get incompatible locks fail!\0A\00"
define i32 @lock_test(i32 %0, %struct.flock* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock*
store i32 %0, i32* %4
store %struct.flock* %1, %struct.flock** %5
%6 = load i32, i32* %4
%7 = load i32, i32* @F_GETLK
%8 = load %struct.flock*, %struct.flock** %5
%9 = call i64 @fcntl(i32 %6, i32 %7, %struct.flock* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %43
11: ; preds = %2
%12 = load %struct.flock*, %struct.flock** %5
%13 = getelementptr inbounds %struct.flock, %struct.flock* %12, i32 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @F_UNLCK
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %45
19: ; preds = %11
%20 = load %struct.flock*, %struct.flock** %5
%21 = getelementptr inbounds %struct.flock, %struct.flock* %20, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load i64, i64* @F_RDLCK
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = load %struct.flock*, %struct.flock** %5
%27 = getelementptr inbounds %struct.flock, %struct.flock* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %28)
br label %42
30: ; preds = %19
%31 = load %struct.flock*, %struct.flock** %5
%32 = getelementptr inbounds %struct.flock, %struct.flock* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @F_WRLCK
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = load %struct.flock*, %struct.flock** %5
%38 = getelementptr inbounds %struct.flock, %struct.flock* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %36, %30
br label %42
42: ; preds = %41, %25
store i32 -2, i32* %3
br label %45
43: ; preds = %2
%44 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
45: ; preds = %43, %42, %17
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.flock = type { i64, i32 }
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@F_RDLCK = global i64 0
@.str.1 = [47 x i8] c"can't set lock, read lock has been set by: %d\0A\00"
@F_WRLCK = global i64 0
@.str.2 = [48 x i8] c"can't set lock, write lock has been set by: %d\0A\00"
@.str.3 = [30 x i8] c"get incompatible locks fail!\0A\00"
@str = [22 x i8] c"lock can be set in fd\00"
define i32 @lock_test(i32 %0, %struct.flock* %1) {
%3 = load i32, i32* @F_GETLK
%4 = tail call i64 @fcntl(i32 %0, i32 %3, %struct.flock* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %27
6: ; preds = %2
%7 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @F_UNLCK
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %29
13: ; preds = %6
%14 = load i64, i64* @F_RDLCK
%15 = icmp eq i64 %8, %14
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
%18 = load i32, i32* %17
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %29
20: ; preds = %13
%21 = load i64, i64* @F_WRLCK
%22 = icmp eq i64 %8, %21
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
%25 = load i32, i32* %24
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %29
27: ; preds = %2
%28 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %16, %23, %20, %27, %11
%30 = phi i32 [ 0, %11 ], [ -1, %27 ], [ -2, %20 ], [ -2, %23 ], [ -2, %16 ]
ret i32 %30
}