#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_len;
  int l_pid;
  void *l_type;
  scalar_t__ l_start;
  void *l_whence;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
int /*<<< orphan*/ F_SETLK;
void *F_UNLCK;
void *F_WRLCK;
int /*<<< orphan*/ O_RDWR;
void *SEEK_SET;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main() {
  int fd = open("letter", O_RDWR);
  if (fd == -1) {
    perror("letter");
    return 1;
  }
  printf("my pid=%d\n", getpid());
  printf("打开文件成功,fd=%d\n", fd);
  struct flock f;
  f.l_type = F_WRLCK;
  f.l_whence = SEEK_SET;
  f.l_start = 0;
  f.l_len = 10;
  f.l_pid = -1;
  if (fcntl(fd, F_SETLK, &f) == -1) {
    perror("F_SETLK");
    struct flock k;
    k.l_whence = SEEK_SET;
    k.l_start = 0;
    k.l_len = 10;
    k.l_type = F_WRLCK;
    fcntl(fd, F_GETLK, &k);
    printf("%d locked it\n", k.l_pid);
    return 0;
  }
  sleep(20);
  f.l_type = F_UNLCK;
  fcntl(fd, F_SETLK, &f);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
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+0x49>
lea    0xe58(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c9 <main+0x159>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe3e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe3a(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xa,-0x28(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1294 <main+0x124>
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xa,-0x48(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x44(%rbp),%esi
lea    0xdb1(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <main+0x159>
mov    $0x14,%edi
call   1090 <sleep@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     124e <main+0xde>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5e(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe5b(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    0x2df3(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
mov    0x2dec(%rip),%r15
mov    (%r15),%rax
mov    %rax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
movabs $0xffffffff0000000a,%rax
ff ff ff
mov    %rax,0x8(%rsp)
mov    0x2de5(%rip),%r12
mov    (%r12),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1261 <main+0xf1>
mov    $0x14,%edi
call   1090 <sleep@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    (%r12),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdab(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1242 <main+0xd2>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
mov    (%r15),%rax
mov    %rax,0x40(%rsp)
movq   $0x0,0x38(%rsp)
movl   $0xa,0x28(%rsp)
mov    (%r14),%rax
mov    %rax,0x30(%rsp)
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
lea    0x28(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    0x2c(%rsp),%esi
lea    0xd86(%rip),%rdi
call   1050 <printf@plt>
jmp    1240 <main+0xd0>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i8*, i64, i8* }
@.str = [7 x i8] c"letter\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"my pid=%d\0A\00"
@.str.2 = [26 x i8] c"\E6\89\93\E5\BC\80\E6\96\87\E4\BB\B6\E6\88\90\E5\8A\9F,fd=%d\0A\00"
@F_WRLCK = global i8* null
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.3 = [8 x i8] c"F_SETLK\00"
@F_GETLK = global i32 0
@.str.4 = [14 x i8] c"%d locked it\0A\00"
@F_UNLCK = global i8* null
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.flock
%4 = alloca %struct.flock
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %50
11: ; preds = %0
%12 = call i32 (...) @getpid()
%13 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %2
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** @F_WRLCK
%17 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i8* %16, i8** %17
%18 = load i8*, i8** @SEEK_SET
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 4
store i8* %18, i8** %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
store i64 0, i64* %20
%21 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i32 10, i32* %21
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
store i32 -1, i32* %22
%23 = load i32, i32* %2
%24 = load i32, i32* @F_SETLK
%25 = call i32 @fcntl(i32 %23, i32 %24, %struct.flock* %3)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %41
27: ; preds = %11
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%29 = load i8*, i8** @SEEK_SET
%30 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 4
store i8* %29, i8** %30
%31 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 3
store i64 0, i64* %31
%32 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 0
store i32 10, i32* %32
%33 = load i8*, i8** @F_WRLCK
%34 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 2
store i8* %33, i8** %34
%35 = load i32, i32* %2
%36 = load i32, i32* @F_GETLK
%37 = call i32 @fcntl(i32 %35, i32 %36, %struct.flock* %4)
%38 = getelementptr inbounds %struct.flock, %struct.flock* %4, i32 0, i32 1
%39 = load i32, i32* %38
%40 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %39)
store i32 0, i32* %1
br label %50
41: ; preds = %11
%42 = call i32 @sleep(i32 20)
%43 = load i8*, i8** @F_UNLCK
%44 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i8* %43, i8** %44
%45 = load i32, i32* %2
%46 = load i32, i32* @F_SETLK
%47 = call i32 @fcntl(i32 %45, i32 %46, %struct.flock* %3)
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %41, %27, %9
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i8*, i64, i8* }
@.str = [7 x i8] c"letter\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"my pid=%d\0A\00"
@.str.2 = [26 x i8] c"\E6\89\93\E5\BC\80\E6\96\87\E4\BB\B6\E6\88\90\E5\8A\9F,fd=%d\0A\00"
@F_WRLCK = global i8* null
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.3 = [8 x i8] c"F_SETLK\00"
@F_GETLK = global i32 0
@.str.4 = [14 x i8] c"%d locked it\0A\00"
@F_UNLCK = global i8* null
define i32 @main() {
%1 = alloca %struct.flock
%2 = alloca %struct.flock
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %44
8: ; preds = %0
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%12 = bitcast %struct.flock* %1 to i8*
%13 = load i8*, i8** @F_WRLCK
%14 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
store i8* %13, i8** %14
%15 = load i8*, i8** @SEEK_SET
%16 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 4
store i8* %15, i8** %16
%17 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
store i32 10, i32* %18
%19 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
store i32 -1, i32* %19
%20 = load i32, i32* @F_SETLK
%21 = call i32 @fcntl(i32 %4, i32 %20, %struct.flock* %1)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %37
23: ; preds = %8
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%25 = bitcast %struct.flock* %2 to i8*
%26 = load i8*, i8** @SEEK_SET
%27 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 4
store i8* %26, i8** %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 3
store i64 0, i64* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 0
store i32 10, i32* %29
%30 = load i8*, i8** @F_WRLCK
%31 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 2
store i8* %30, i8** %31
%32 = load i32, i32* @F_GETLK
%33 = call i32 @fcntl(i32 %4, i32 %32, %struct.flock* %2)
%34 = getelementptr inbounds %struct.flock, %struct.flock* %2, i64 0, i32 1
%35 = load i32, i32* %34
%36 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %35)
br label %43
37: ; preds = %8
%38 = call i32 @sleep(i32 20)
%39 = load i8*, i8** @F_UNLCK
store i8* %39, i8** %14
%40 = load i32, i32* @F_SETLK
%41 = call i32 @fcntl(i32 %4, i32 %40, %struct.flock* %1)
%42 = call i32 @close(i32 %4)
br label %43
43: ; preds = %37, %23
br label %44
44: ; preds = %43, %6
%45 = phi i32 [ 1, %6 ], [ 0, %43 ]
ret i32 %45
}