#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 l_pid;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int fd;
  struct flock lk = {0};
  int r;
  fd = open("stu.dat", O_RDWR);
  if (fd == -1) {
    printf("open error:%m\n");
    exit(-1);
  }
  r = fcntl(fd, F_GETLK, &lk);
  if (r == 0) {
    printf("得到鎖成功\n");
  }
  if (lk.l_type == F_WRLCK) {
    printf("寫鎖\n");
  }
  printf("start : %d\tlen : %d\n", lk.l_start, lk.l_len);
  printf("PID : %d\n", lk.l_pid);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b9 <main+0x59>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11e9 <main+0x89>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
jne    120b <main+0xab>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1207 <main+0xa7>
mov    %eax,%ebx
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %eax,%eax
jne    11b4 <main+0x54>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
mov    (%rsp),%rax
mov    0x2e11(%rip),%rcx
cmp    (%rcx),%rax
jne    11d0 <main+0x70>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe38(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%esi
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdfa(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i32, i32, i32, i32 }
@.str = [8 x i8] c"stu.dat\00"
@O_RDWR = global i32 0
@.str.1 = [15 x i8] c"open error:%m\0A\00"
@F_GETLK = global i32 0
@.str.2 = [17 x i8] c"\E5\BE\97\E5\88\B0\E9\8E\96\E6\88\90\E5\8A\9F\0A\00"
@F_WRLCK = global i64 0
@.str.3 = [8 x i8] c"\E5\AF\AB\E9\8E\96\0A\00"
@.str.4 = [21 x i8] c"start : %d\09len : %d\0A\00"
@.str.5 = [10 x i8] c"PID : %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.flock
%4 = alloca i32
store i32 0, i32* %1
%5 = bitcast %struct.flock* %3 to i8*
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %2
%15 = load i32, i32* @F_GETLK
%16 = call i32 @fcntl(i32 %14, i32 %15, %struct.flock* %3)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, 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 %28
26: ; preds = %21
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32 %32)
%34 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
%35 = load i32, i32* %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i32, i32, i32 }
@.str = [8 x i8] c"stu.dat\00"
@O_RDWR = global i32 0
@.str.1 = [15 x i8] c"open error:%m\0A\00"
@F_GETLK = global i32 0
@F_WRLCK = global i64 0
@.str.4 = [21 x i8] c"start : %d\09len : %d\0A\00"
@.str.5 = [10 x i8] c"PID : %d\0A\00"
@str = [7 x i8] c"\E5\AF\AB\E9\8E\96\00"
@str.6 = [16 x i8] c"\E5\BE\97\E5\88\B0\E9\8E\96\E6\88\90\E5\8A\9F\00"
define i32 @main() {
%1 = alloca %struct.flock
%2 = bitcast %struct.flock* %1 to i8*
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @F_GETLK
%11 = call i32 @fcntl(i32 %4, i32 %10, %struct.flock* %1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @F_WRLCK
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
%26 = load i32, i32* %25
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %24, i32 %26)
%28 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
%29 = load i32, i32* %28
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = call i32 @close(i32 %4)
ret i32 0
}