#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;
};

/* Variables and functions */
scalar_t__ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int lock_set(int fd, struct flock *lock) {
  if (fcntl(fd, F_SETLK, lock) == 0) {
    if (lock->l_type == F_RDLCK) {
      printf("set read lock, pid:%d\n", getpid());
    } else if (lock->l_type == F_WRLCK) {
      printf("set write lock pid:%d\n", getpid());
    } else if (lock->l_type == F_UNLCK) {
      printf("release lock, pid:%d\n", getpid());
    }
  } else {
    perror("lock operation fail\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<lock_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jne    120b <lock_set+0xcb>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <lock_set+0x5f>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe6b(%rip),%rdi
call   1050 <printf@plt>
jmp    1206 <lock_set+0xc6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11d0 <lock_set+0x90>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
jmp    1201 <lock_set+0xc1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11fc <lock_set+0xbc>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe37(%rip),%rdi
call   1050 <printf@plt>
jmp    1201 <lock_set+0xc1>
jmp    1206 <lock_set+0xc6>
jmp    1223 <lock_set+0xe3>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <lock_set+0xea>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_set>:
push   %rbx
mov    %rsi,%rbx
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
test   %rax,%rax
jne    11c2 <lock_set+0x82>
mov    (%rbx),%rax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jne    117b <lock_set+0x3b>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe87(%rip),%rdi
jmp    11b7 <lock_set+0x77>
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <lock_set+0x59>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe80(%rip),%rdi
jmp    11b7 <lock_set+0x77>
mov    0x2e40(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
jne    11be <lock_set+0x7e>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe77(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe7b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11be <lock_set+0x7e>

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

LLVM IR (-O2):
%struct.flock = type { i64 }
@F_SETLK = global i32 0
@F_RDLCK = global i64 0
@.str = [23 x i8] c"set read lock, pid:%d\0A\00"
@F_WRLCK = global i64 0
@.str.1 = [23 x i8] c"set write lock pid:%d\0A\00"
@F_UNLCK = global i64 0
@.str.2 = [22 x i8] c"release lock, pid:%d\0A\00"
@.str.3 = [21 x i8] c"lock operation fail\0A\00"
define i32 @lock_set(i32 %0, %struct.flock* %1) {
%3 = load i32, i32* @F_SETLK
%4 = tail call i64 @fcntl(i32 %0, i32 %3, %struct.flock* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %26
6: ; preds = %2
%7 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @F_RDLCK
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %28
14: ; preds = %6
%15 = load i64, i64* @F_WRLCK
%16 = icmp eq i64 %8, %15
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %28
20: ; preds = %14
%21 = load i64, i64* @F_UNLCK
%22 = icmp eq i64 %8, %21
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %28
26: ; preds = %2
%27 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %17, %23, %20, %11, %26
%29 = phi i32 [ -1, %26 ], [ 0, %11 ], [ 0, %20 ], [ 0, %23 ], [ 0, %17 ]
ret i32 %29
}