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

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int F_UNLCK;
int F_WRLCK;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getchar();
int getpid();
int /*<<< orphan*/ printf(char *, int);
void lock_set(int fd, int type) {
  struct flock lock;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  while (1) {
    lock.l_type = type;
    if (0 == (fcntl(fd, F_SETLK, &lock))) {
      if (lock.l_type == F_RDLCK) {
        printf("read lock set by %d\n", getpid());
      } else if (lock.l_type == F_WRLCK) {
        printf("write lock set by %d\n", getpid());
      } else if (lock.l_type == F_UNLCK) {
        printf("release lock by %d\n", getpid());
      }
      return;
    }
    fcntl(fd, F_GETLK, &lock);
    if (lock.l_type != F_UNLCK) {
      if (lock.l_type == F_RDLCK) {
        printf("read lock already set by %d\n", lock.l_pid);
      } else if (lock.l_type == F_WRLCK) {
        printf("write lock already set by %d\n", lock.l_pid);
      }
      getchar();
    }
  }
}


x86 assembly (clang, -O0):
<lock_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1222 <lock_set+0xe2>
mov    -0x28(%rbp),%eax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%eax
jne    11bf <lock_set+0x7f>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe4b(%rip),%rdi
call   1050 <printf@plt>
jmp    121c <lock_set+0xdc>
mov    -0x28(%rbp),%eax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%eax
jne    11eb <lock_set+0xab>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe34(%rip),%rdi
call   1050 <printf@plt>
jmp    1217 <lock_set+0xd7>
mov    -0x28(%rbp),%eax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%eax
jne    1212 <lock_set+0xd2>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe1e(%rip),%rdi
call   1050 <printf@plt>
jmp    1217 <lock_set+0xd7>
jmp    121c <lock_set+0xdc>
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x4(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x28(%rbp),%eax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%eax
je     129c <lock_set+0x15c>
mov    -0x28(%rbp),%eax
mov    0x2d5d(%rip),%rcx
cmp    (%rcx),%eax
jne    126f <lock_set+0x12f>
mov    -0x24(%rbp),%esi
lea    0xdda(%rip),%rdi
call   1050 <printf@plt>
jmp    1295 <lock_set+0x155>
mov    -0x28(%rbp),%eax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%eax
jne    1290 <lock_set+0x150>
mov    -0x24(%rbp),%esi
lea    0xdd1(%rip),%rdi
call   1050 <printf@plt>
jmp    1295 <lock_set+0x155>
mov    $0x0,%al
call   1060 <getchar@plt>
jmp    116a <lock_set+0x2a>

x86 assembly (clang, -O2):
<lock_set>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    %esi,0x8(%rsp)
mov    0x2e75(%rip),%r15
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
je     1205 <lock_set+0xc5>
mov    0x2e50(%rip),%r12
lea    0x8(%rsp),%rbx
mov    0x2e3c(%rip),%r13
jmp    11c7 <lock_set+0x87>
xchg   %ax,%ax
mov    0xc(%rsp),%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %r14d,0x8(%rsp)
mov    (%r15),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
je     1205 <lock_set+0xc5>
mov    (%r12),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
mov    0x8(%rsp),%eax
cmp    0x0(%r13),%eax
je     11b0 <lock_set+0x70>
lea    0xe59(%rip),%rdi
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%eax
je     11a0 <lock_set+0x60>
lea    0xe64(%rip),%rdi
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%eax
je     11a0 <lock_set+0x60>
jmp    11a9 <lock_set+0x69>
mov    0x8(%rsp),%eax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%eax
jne    121d <lock_set+0xdd>
lea    0xde5(%rip),%rbx
jmp    1243 <lock_set+0x103>
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%eax
jne    1231 <lock_set+0xf1>
lea    0xde6(%rip),%rbx
jmp    1243 <lock_set+0x103>
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%eax
jne    1254 <lock_set+0x114>
lea    0xde8(%rip),%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.flock = type { i32, i32, i64, i64, i32 }
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@F_RDLCK = global i32 0
@.str = [21 x i8] c"read lock set by %d\0A\00"
@F_WRLCK = global i32 0
@.str.1 = [22 x i8] c"write lock set by %d\0A\00"
@F_UNLCK = global i32 0
@.str.2 = [20 x i8] c"release lock by %d\0A\00"
@F_GETLK = global i32 0
@.str.3 = [29 x i8] c"read lock already set by %d\0A\00"
@.str.4 = [30 x i8] c"write lock already set by %d\0A\00"
define void @lock_set(i32 %0, i32 %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = load i32, i32* @SEEK_SET
%6 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
%9 = bitcast i64* %7 to i8*
store i32 %1, i32* %8
%10 = load i32, i32* @F_SETLK
%11 = call i64 @fcntl(i32 %0, i32 %10, %struct.flock* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
br label %30
15: ; preds = %48, %2
%16 = load i32, i32* %8
%17 = load i32, i32* @F_RDLCK
%18 = icmp eq i32 %16, %17
br i1 %18, label %25, label %19
19: ; preds = %15
%20 = load i32, i32* @F_WRLCK
%21 = icmp eq i32 %16, %20
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* @F_UNLCK
%24 = icmp eq i32 %16, %23
br i1 %24, label %25, label %29
25: ; preds = %22, %19, %15
%26 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), %15 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %19 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %22 ]
%27 = call i32 (...) @getpid()
%28 = call i32 @printf(i8* %26, i32 %27)
br label %29
29: ; preds = %25, %22
ret void
30: ; preds = %13, %48
%31 = load i32, i32* @F_GETLK
%32 = call i64 @fcntl(i32 %0, i32 %31, %struct.flock* %3)
%33 = load i32, i32* %8
%34 = load i32, i32* @F_UNLCK
%35 = icmp eq i32 %33, %34
br i1 %35, label %48, label %36
36: ; preds = %30
%37 = load i32, i32* @F_RDLCK
%38 = icmp eq i32 %33, %37
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = load i32, i32* @F_WRLCK
%41 = icmp eq i32 %33, %40
br i1 %41, label %42, label %46
42: ; preds = %39, %36
%43 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), %36 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), %39 ]
%44 = load i32, i32* %14
%45 = call i32 @printf(i8* %43, i32 %44)
br label %46
46: ; preds = %42, %39
%47 = call i32 (...) @getchar()
br label %48
48: ; preds = %46, %30
store i32 %1, i32* %8
%49 = load i32, i32* @F_SETLK
%50 = call i64 @fcntl(i32 %0, i32 %49, %struct.flock* %3)
%51 = icmp eq i64 %50, 0
br i1 %51, label %15, label %30
}