#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;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
char getchar();
int /*<<< orphan*/ getpid();
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ write(int, char *, int);
void write_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 = off;
  off_t len = 1;
  check_lock.l_len = len;
  check_lock.l_pid = getpid();
  errno = 0;
  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");
  }
  printf("Input the char: ");
  char buff = getchar();
  getchar();
  write(fd, &buff, sizeof(char));
  printf("Wrote char: %c\n", buff);
  if (off == -1) {
    return;
  }
}


x86 assembly (clang, -O0):
<write_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
call   1070 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ae <write_char+0x4e>
jmp    12c7 <write_char+0x167>
mov    -0x24(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dfe(%rip),%rax
movq   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11fc <write_char+0x9c>
jmp    12c7 <write_char+0x167>
mov    -0x20(%rbp),%rax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%rax
jne    1223 <write_char+0xc3>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1276 <write_char+0x116>
mov    -0x20(%rbp),%rax
mov    0x2d92(%rip),%rcx
cmp    (%rcx),%rax
jne    124a <write_char+0xea>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1271 <write_char+0x111>
mov    -0x20(%rbp),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
jne    126c <write_char+0x10c>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1271 <write_char+0x111>
jmp    1276 <write_char+0x116>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %al,-0x29(%rbp)
mov    $0x0,%al
call   1080 <getchar@plt>
mov    -0x4(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movsbl -0x29(%rbp),%esi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0xffffffff,-0x24(%rbp)
jne    12c7 <write_char+0x167>
jmp    12c7 <write_char+0x167>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_char>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebp
mov    0x2e41(%rip),%rbx
mov    (%rbx),%rax
mov    %rax,0x10(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %edx,0x24(%rsp)
call   1080 <lseek@plt>
cmp    $0xffffffff,%eax
je     1275 <write_char+0x105>
mov    %eax,0x18(%rsp)
movl   $0x1,0x1c(%rsp)
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,0x20(%rsp)
mov    0x2e10(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1275 <write_char+0x105>
mov    0x10(%rsp),%rax
mov    0x2def(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <write_char+0x94>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
jmp    1231 <write_char+0xc1>
cmp    (%rbx),%rax
jne    1217 <write_char+0xa7>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
jmp    1231 <write_char+0xc1>
mov    0x2d92(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <write_char+0xc1>
lea    0xdd6(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xddd(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %eax,%ebx
mov    %al,0xf(%rsp)
xor    %eax,%eax
call   1090 <getchar@plt>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1050 <write@plt>
movsbl %bl,%esi
lea    0xdb8(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i32, i32, i32, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@errno = global i64 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 = [17 x i8] c"Input the char: \00"
@.str.4 = [16 x i8] c"Wrote char: %c\0A\00"
define void @write_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 %67
20: ; preds = %2
%21 = load i32, i32* %6
%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
store i64 0, i64* @errno
%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 %67
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 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%57 = call signext i8 (...) @getchar()
store i8 %57, i8* %8
%58 = call signext i8 (...) @getchar()
%59 = load i32, i32* %3
%60 = call i32 @write(i32 %59, i8* %8, i32 1)
%61 = load i8, i8* %8
%62 = sext i8 %61 to i32
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %6
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %67
66: ; preds = %55
br label %67
67: ; preds = %19, %31, %66, %55
ret void
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i32, i32, i32 }
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@errno = global i64 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 = [17 x i8] c"Input the char: \00"
@.str.4 = [16 x i8] c"Wrote char: %c\0A\00"
@str = [22 x i8] c"Write lock is present\00"
@str.5 = [12 x i8] c"Free record\00"
define void @write_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 %43, label %12
12: ; preds = %2
%13 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %10, 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
store i64 0, i64* @errno
%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 %43, 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.5, 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%38 = call signext i8 (...) @getchar()
store i8 %38, i8* %4
%39 = call signext i8 (...) @getchar()
%40 = call i32 @write(i32 %0, i8* %4, i32 1)
%41 = sext i8 %38 to i32
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %36, %12, %2
ret void
}