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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
#define F_RDLCK 130
#define F_UNLCK 129
#define F_WRLCK 128
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int lock_status(int fd) {
  struct flock st_flock;
  if (fcntl(fd, F_GETLK, &st_flock) < 0) {
    perror("fcntl");
    goto err;
  }
  switch (st_flock.l_type) {
  case F_UNLCK:
    printf("it's unlock\n");
    break;
  case F_RDLCK:
    printf("it's read lock by %d\n", st_flock.l_pid);
    break;
  case F_WRLCK:
    printf("it's write lock by %d\n", st_flock.l_pid);
    break;
  }
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<lock_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    116b <lock_status+0x3b>
lea    0xe9f(%rip),%rdi
call   1050 <perror@plt>
jmp    11ed <lock_status+0xbd>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0x80,%eax
je     11d0 <lock_status+0xa0>
jmp    1181 <lock_status+0x51>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
je     11a7 <lock_status+0x77>
jmp    1194 <lock_status+0x64>
mov    -0x14(%rbp),%eax
sub    $0x82,%eax
je     11ba <lock_status+0x8a>
jmp    11e1 <lock_status+0xb1>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e1 <lock_status+0xb1>
mov    -0xc(%rbp),%esi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11e1 <lock_status+0xb1>
mov    -0xc(%rbp),%esi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <lock_status+0xc4>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_status>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <fcntl@plt>
test   %rax,%rax
js     11af <lock_status+0x6f>
mov    0x8(%rsp),%eax
cmp    $0x80,%eax
je     1186 <lock_status+0x46>
cmp    $0x82,%eax
je     1193 <lock_status+0x53>
xor    %ebx,%ebx
cmp    $0x81,%eax
jne    11a7 <lock_status+0x67>
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
jmp    11a7 <lock_status+0x67>
mov    0xc(%rsp),%esi
lea    0xe8b(%rip),%rdi
jmp    119e <lock_status+0x5e>
mov    0xc(%rsp),%esi
lea    0xe68(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11a7 <lock_status+0x67>

LLVM IR (-O0):
%struct.flock = type { i32, i32 }
@F_GETLK = global i32 0
@.str = [6 x i8] c"fcntl\00"
@.str.1 = [13 x i8] c"it's unlock\0A\00"
@.str.2 = [22 x i8] c"it's read lock by %d\0A\00"
@.str.3 = [23 x i8] c"it's write lock by %d\0A\00"
define i32 @lock_status(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.flock
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @F_GETLK
%7 = call i64 @fcntl(i32 %5, i32 %6, %struct.flock* %4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %25
11: ; preds = %1
%12 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
%13 = load i32, i32* %12
switch i32 %13, label %24 [
i32 129, label %14
i32 130, label %16
i32 128, label %20
]
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %24
16: ; preds = %11
%17 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %24
20: ; preds = %11
%21 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %11, %20, %16, %14
store i32 0, i32* %2
br label %26
25: ; preds = %9
store i32 -1, i32* %2
br label %26
26: ; preds = %25, %24
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.flock = type { i32, i32 }
@F_GETLK = global i32 0
@.str = [6 x i8] c"fcntl\00"
@.str.2 = [22 x i8] c"it's read lock by %d\0A\00"
@.str.3 = [23 x i8] c"it's write lock by %d\0A\00"
@str = [12 x i8] c"it's unlock\00"
define i32 @lock_status(i32 %0) {
%2 = alloca %struct.flock
%3 = bitcast %struct.flock* %2 to i8*
%4 = load i32, i32* @F_GETLK
%5 = call i64 @fcntl(i32 %0, i32 %4, %struct.flock* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %22
9: ; preds = %1
%10 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
%11 = load i32, i32* %10
switch i32 %11, label %22 [
i32 129, label %12
i32 130, label %14
i32 128, label %18
]
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %22
14: ; preds = %9
%15 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %22
18: ; preds = %9
%19 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %12, %14, %18, %9, %7
%23 = phi i32 [ -1, %7 ], [ 0, %9 ], [ 0, %18 ], [ 0, %14 ], [ 0, %12 ]
ret i32 %23
}