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

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
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 r;
  int fd;
  struct flock lk;
  fd = open("stu.dat", O_RDWR);
  if (fd == -1) {
    printf("open error:%m\n");
    exit(-1);
  }
  lk.l_type = F_WRLCK;
  lk.l_whence = SEEK_SET;
  lk.l_start = 5;
  lk.l_len = 10;
  r = fcntl(fd, F_SETLK, &lk);
  if (r == 0) {
    printf("加鎖成功\n");
  } else {
    printf("加鎖失敗\n");
  }
  while (1)
    ;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1187 <main+0x47>
lea    0xe90(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x5,-0x20(%rbp)
movl   $0xa,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11e0 <main+0xa0>
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
jmp    11ec <main+0xac>
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
jmp    11f1 <main+0xb1>
jmp    11f1 <main+0xb1>

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d2 <main+0x82>
mov    0x2e53(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x14(%rsp)
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x10(%rsp)
movabs $0xa00000005,%rcx
mov    %rcx,0x8(%rsp)
mov    0x2e42(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1040 <fcntl@plt>
test   %eax,%eax
lea    0xe6f(%rip),%rax
lea    0xe5b(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
nop
jmp    11d0 <main+0x80>
lea    0xe2f(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { 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_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@.str.2 = [14 x i8] c"\E5\8A\A0\E9\8E\96\E6\88\90\E5\8A\9F\0A\00"
@.str.3 = [14 x i8] c"\E5\8A\A0\E9\8E\96\E5\A4\B1\E6\95\97\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.flock
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @F_WRLCK
%14 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @SEEK_SET
%16 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
store i32 5, i32* %17
%18 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 1
store i32 10, i32* %18
%19 = load i32, i32* %3
%20 = load i32, i32* @F_SETLK
%21 = call i32 @fcntl(i32 %19, i32 %20, %struct.flock* %4)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %28
26: ; preds = %12
%27 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %24
br label %29
29: ; preds = %28, %29
br label %29
}

LLVM IR (-O2):
%struct.flock = type { 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_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@str = [13 x i8] c"\E5\8A\A0\E9\8E\96\E5\A4\B1\E6\95\97\00"
@str.4 = [13 x i8] c"\E5\8A\A0\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 @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_WRLCK
%11 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @SEEK_SET
%13 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
store i32 5, i32* %14
%15 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
store i32 10, i32* %15
%16 = load i32, i32* @F_SETLK
%17 = call i32 @fcntl(i32 %4, i32 %16, %struct.flock* %1)
%18 = icmp eq i32 %17, 0
%19 = select i1 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.4, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0)
%20 = call i32 @puts(i8* dereferenceable(1) %19)
br label %21
21: ; preds = %21, %9
br label %21
}