#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 {
  void *l_type;
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
};

/* Variables and functions */
void *F_RDLCK;
void *F_UNLCK;
void *F_WRLCK;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ lock_set(int, struct flock *);
scalar_t__ lock_test(int, struct flock *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int /*<<< orphan*/ my_err(char *, int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  int ret;
  struct flock lock;
  char read_buf[32];
  if ((fd = open("example_fc_lock", O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) ==
      -1) {
    my_err("open", 45);
  }
  if (write(fd, "test lock", 10) != 10) {
    my_err("write", 48);
  }
  memset(&lock, 0, sizeof(struct flock));
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  lock.l_type = F_RDLCK;
  if (lock_test(fd, &lock) == 0) {
    lock.l_type = F_RDLCK;
    lock_set(fd, &lock);
  }
  lseek(fd, 0, SEEK_SET);
  if ((ret = read(fd, read_buf, 10)) < 0) {
    my_err("read", 68);
  }
  read_buf[ret] = '\0';
  printf("%s\n", read_buf);
  getchar();
  lock.l_type = F_WRLCK;
  if (lock_test(fd, &lock) == 0) {
    lock.l_type = F_WRLCK;
    lock_set(fd, &lock);
  }
  lock.l_type = F_UNLCK;
  lock_set(fd, &lock);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
lea    0xe0f(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1213 <main+0x63>
lea    0xe07(%rip),%rdi
mov    $0x2d,%esi
call   10d0 <my_err@plt>
mov    -0x14(%rbp),%edi
lea    0xdf8(%rip),%rsi
mov    $0xa,%edx
call   1040 <write@plt>
cmp    $0xa,%eax
je     1241 <main+0x91>
lea    0xde8(%rip),%rdi
mov    $0x30,%esi
call   10d0 <my_err@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1030 <lock_test@plt>
cmp    $0x0,%rax
jne    12ab <main+0xfb>
mov    0x2d08(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1050 <lock_set@plt>
mov    -0x14(%rbp),%edi
mov    0x2d03(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12ec <main+0x13c>
lea    0xd43(%rip),%rdi
mov    $0x44,%esi
call   10d0 <my_err@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rsi
lea    0xd2a(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   10b0 <getchar@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1030 <lock_test@plt>
cmp    $0x0,%rax
jne    134a <main+0x19a>
mov    0x2c79(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1050 <lock_set@plt>
mov    0x2c77(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1050 <lock_set@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    0x2e37(%rip),%rax
mov    0x2e28(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dfd(%rip),%rax
or     (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
lea    0xe1b(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1202 <main+0x52>
lea    0xe18(%rip),%rdi
mov    $0x2d,%esi
call   10d0 <my_err@plt>
lea    0xe0c(%rip),%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1040 <write@plt>
cmp    $0xa,%eax
je     122b <main+0x7b>
lea    0xdfe(%rip),%rdi
mov    $0x30,%esi
call   10d0 <my_err@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    0x2d74(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2d46(%rip),%r15
mov    (%r15),%rax
mov    %rax,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <lock_test@plt>
test   %rax,%rax
jne    1281 <main+0xd1>
mov    (%r15),%rax
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
call   1050 <lock_set@plt>
mov    (%r14),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   10a0 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12b5 <main+0x105>
lea    0xd7a(%rip),%rdi
mov    $0x44,%esi
call   10d0 <my_err@plt>
movslq %ebp,%rax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xd66(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1060 <printf@plt>
xor    %eax,%eax
call   10b0 <getchar@plt>
mov    0x2cd4(%rip),%rbp
mov    0x0(%rbp),%rax
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
call   1030 <lock_test@plt>
test   %rax,%rax
jne    1305 <main+0x155>
mov    0x0(%rbp),%rax
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
call   1050 <lock_set@plt>
mov    0x2cbc(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
call   1050 <lock_set@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i8*, i64, i64, i32 }
@.str = [16 x i8] c"example_fc_lock\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [10 x i8] c"test lock\00"
@.str.3 = [6 x i8] c"write\00"
@SEEK_SET = global i32 0
@F_RDLCK = global i8* null
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@F_WRLCK = global i8* null
@F_UNLCK = global i8* null
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.flock
%9 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @O_RDWR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRWXU
%16 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @my_err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 45)
br label %20
20: ; preds = %18, %2
%21 = load i32, i32* %6
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 10)
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @my_err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 48)
br label %26
26: ; preds = %24, %20
%27 = call i32 @memset(%struct.flock* %8, i32 0, i32 32)
%28 = load i32, i32* @SEEK_SET
%29 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i64 0, i64* %31
%32 = load i8*, i8** @F_RDLCK
%33 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i8* %32, i8** %33
%34 = load i32, i32* %6
%35 = call i64 @lock_test(i32 %34, %struct.flock* %8)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %26
%38 = load i8*, i8** @F_RDLCK
%39 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i8* %38, i8** %39
%40 = load i32, i32* %6
%41 = call i32 @lock_set(i32 %40, %struct.flock* %8)
br label %42
42: ; preds = %37, %26
%43 = load i32, i32* %6
%44 = load i32, i32* @SEEK_SET
%45 = call i32 @lseek(i32 %43, i32 0, i32 %44)
%46 = load i32, i32* %6
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 10)
store i32 %48, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 @my_err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 68)
br label %52
52: ; preds = %50, %42
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 %54
store i8 0, i8* %55
%56 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%57 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %56)
%58 = call i32 (...) @getchar()
%59 = load i8*, i8** @F_WRLCK
%60 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i8* %59, i8** %60
%61 = load i32, i32* %6
%62 = call i64 @lock_test(i32 %61, %struct.flock* %8)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %52
%65 = load i8*, i8** @F_WRLCK
%66 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i8* %65, i8** %66
%67 = load i32, i32* %6
%68 = call i32 @lock_set(i32 %67, %struct.flock* %8)
br label %69
69: ; preds = %64, %52
%70 = load i8*, i8** @F_UNLCK
%71 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i8* %70, i8** %71
%72 = load i32, i32* %6
%73 = call i32 @lock_set(i32 %72, %struct.flock* %8)
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i8*, i64, i64, i32 }
@.str = [16 x i8] c"example_fc_lock\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [10 x i8] c"test lock\00"
@.str.3 = [6 x i8] c"write\00"
@SEEK_SET = global i32 0
@F_RDLCK = global i8* null
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@F_WRLCK = global i8* null
@F_UNLCK = global i8* null
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.flock
%4 = alloca [32 x i8]
%5 = bitcast %struct.flock* %3 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %8, %7
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRWXU
%13 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @my_err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 45)
br label %17
17: ; preds = %15, %2
%18 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 10)
%19 = icmp eq i32 %18, 10
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @my_err(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 48)
br label %22
22: ; preds = %20, %17
%23 = call i32 @memset(%struct.flock* %3, i32 0, i32 32)
%24 = load i32, i32* @SEEK_SET
%25 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%27 = bitcast i64* %26 to i8*
%28 = load i8*, i8** @F_RDLCK
%29 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i8* %28, i8** %29
%30 = call i64 @lock_test(i32 %13, %struct.flock* %3)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = load i8*, i8** @F_RDLCK
store i8* %33, i8** %29
%34 = call i32 @lock_set(i32 %13, %struct.flock* %3)
br label %35
35: ; preds = %32, %22
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @lseek(i32 %13, i32 0, i32 %36)
%38 = call i32 @read(i32 %13, i8* %6, i32 10)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @my_err(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 68)
br label %42
42: ; preds = %40, %35
%43 = sext i32 %38 to i64
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %43
store i8 0, i8* %44
%45 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%46 = call i32 (...) @getchar()
%47 = load i8*, i8** @F_WRLCK
store i8* %47, i8** %29
%48 = call i64 @lock_test(i32 %13, %struct.flock* %3)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = load i8*, i8** @F_WRLCK
store i8* %51, i8** %29
%52 = call i32 @lock_set(i32 %13, %struct.flock* %3)
br label %53
53: ; preds = %50, %42
%54 = load i8*, i8** @F_UNLCK
store i8* %54, i8** %29
%55 = call i32 @lock_set(i32 %13, %struct.flock* %3)
%56 = call i32 @close(i32 %13)
ret i32 0
}