#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_start;
  int l_len;
  int /*<<< orphan*/ l_pid;
  int /*<<< orphan*/ l_whence;
};
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_RDLCK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
void read_char(int fd, int offset) {
  struct flock check_lock;
  check_lock.l_type = F_WRLCK;
  check_lock.l_whence = SEEK_SET;
  off_t off = lseek(fd, offset, SEEK_SET);
  if (off == -1) {
    return;
  }
  check_lock.l_start = offset;
  off_t len = 1;
  check_lock.l_len = len;
  check_lock.l_pid = getpid();
  if (fcntl(fd, F_GETLK, &check_lock) == -1)
    return;
  if (check_lock.l_type == F_UNLCK) {
    printf("Free record\n");
  } else if (check_lock.l_type == F_WRLCK) {
    printf("Write lock is present\n");
  } else if (check_lock.l_type == F_RDLCK) {
    printf("Read lock is present");
  }
  char buff;
  read(fd, &buff, sizeof(char));
  printf("Read char: %c\n", buff);
  if (off == -1) {
    return;
  }
}


x86 assembly (clang, -O0):
<read_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    119e <read_char+0x4e>
jmp    128a <read_char+0x13a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11de <read_char+0x8e>
jmp    128a <read_char+0x13a>
mov    -0x20(%rbp),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <read_char+0xb5>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1258 <read_char+0x108>
mov    -0x20(%rbp),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jne    122c <read_char+0xdc>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1253 <read_char+0x103>
mov    -0x20(%rbp),%rax
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%rax
jne    124e <read_char+0xfe>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1253 <read_char+0x103>
jmp    1258 <read_char+0x108>
mov    -0x4(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl -0x29(%rbp),%esi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0xffffffff,-0x24(%rbp)
jne    128a <read_char+0x13a>
jmp    128a <read_char+0x13a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_char>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2e55(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x8(%rsp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    %edx,0x1c(%rsp)
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
je     1237 <read_char+0xd7>
mov    %ebp,0x10(%rsp)
movl   $0x1,0x14(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x18(%rsp)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1237 <read_char+0xd7>
mov    0x8(%rsp),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <read_char+0x86>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
jmp    1213 <read_char+0xb3>
cmp    (%r14),%rax
jne    11f9 <read_char+0x99>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
jmp    1213 <read_char+0xb3>
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jne    1213 <read_char+0xb3>
lea    0xdf4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
movsbl 0x7(%rsp),%esi
lea    0xde5(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32, i32, i32, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@.str = [13 x i8] c"Free record\0A\00"
@.str.1 = [23 x i8] c"Write lock is present\0A\00"
@F_RDLCK = global i64 0
@.str.2 = [21 x i8] c"Read lock is present\00"
@.str.3 = [15 x i8] c"Read char: %c\0A\00"
define void @read_char(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.flock
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i64, i64* @F_WRLCK
%10 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
store i64 %9, i64* %10
%11 = load i32, i32* @SEEK_SET
%12 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = load i32, i32* @SEEK_SET
%16 = call i32 @lseek(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
br label %64
20: ; preds = %2
%21 = load i32, i32* %4
%22 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 1
store i32 %21, i32* %22
store i32 1, i32* %7
%23 = load i32, i32* %7
%24 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 2
store i32 %23, i32* %24
%25 = call i32 (...) @getpid()
%26 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 3
store i32 %25, i32* %26
%27 = load i32, i32* %3
%28 = load i32, i32* @F_GETLK
%29 = call i32 @fcntl(i32 %27, i32 %28, %struct.flock* %5)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %20
br label %64
32: ; preds = %20
%33 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load i64, i64* @F_UNLCK
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %55
39: ; preds = %32
%40 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i64, i64* @F_WRLCK
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %54
46: ; preds = %39
%47 = getelementptr inbounds %struct.flock, %struct.flock* %5, i32 0, i32 0
%48 = load i64, i64* %47
%49 = load i64, i64* @F_RDLCK
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %53
53: ; preds = %51, %46
br label %54
54: ; preds = %53, %44
br label %55
55: ; preds = %54, %37
%56 = load i32, i32* %3
%57 = call i32 @read(i32 %56, i8* %8, i32 1)
%58 = load i8, i8* %8
%59 = sext i8 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %6
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %64
63: ; preds = %55
br label %64
64: ; preds = %19, %31, %63, %55
ret void
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i32, i32, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@F_UNLCK = global i64 0
@F_RDLCK = global i64 0
@.str.2 = [21 x i8] c"Read lock is present\00"
@.str.3 = [15 x i8] c"Read char: %c\0A\00"
@str = [22 x i8] c"Write lock is present\00"
@str.4 = [12 x i8] c"Free record\00"
define void @read_char(i32 %0, i32 %1) {
%3 = alloca %struct.flock
%4 = alloca i8
%5 = bitcast %struct.flock* %3 to i8*
%6 = load i64, i64* @F_WRLCK
%7 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i64 %6, i64* %7
%8 = load i32, i32* @SEEK_SET
%9 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = tail call i32 @lseek(i32 %0, i32 %1, i32 %8)
%11 = icmp eq i32 %10, -1
br i1 %11, label %41, label %12
12: ; preds = %2
%13 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %1, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 1, i32* %14
%15 = tail call i32 (...) @getpid()
%16 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %15, i32* %16
%17 = load i32, i32* @F_GETLK
%18 = call i32 @fcntl(i32 %0, i32 %17, %struct.flock* %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %41, label %20
20: ; preds = %12
%21 = load i64, i64* %7
%22 = load i64, i64* @F_UNLCK
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %36
26: ; preds = %20
%27 = load i64, i64* @F_WRLCK
%28 = icmp eq i64 %21, %27
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %36
31: ; preds = %26
%32 = load i64, i64* @F_RDLCK
%33 = icmp eq i64 %21, %32
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %29, %34, %31, %24
%37 = call i32 @read(i32 %0, i8* %4, i32 1)
%38 = load i8, i8* %4
%39 = sext i8 %38 to i32
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %36, %12, %2
ret void
}