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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ memset(struct flock *, int, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(void) {
  int fd;
  struct flock lock;
  memset(&lock, 0x00, sizeof(lock));
  fd = open("test.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
  if (fd == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day04/lock.c",
              24, "open", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  lock.l_type = F_RDLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  if (fcntl(fd, F_SETLKW, &lock) == 0)
    printf("加锁成功!\n");
  else
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day04/lock.c",
              34, "fcntl", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  printf("按任意键解锁!\n");
  getchar();
  lock.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLK, &lock) == 0)
    printf("解锁成功!\n");
  else
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day04/lock.c",
              43, "fcntl", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
lea    0xe2f(%rip),%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    123a <main+0xaa>
jmp    11ed <main+0x5d>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%r9
lea    0xdf5(%rip),%rsi
lea    0xdfe(%rip),%rdx
mov    $0x18,%ecx
lea    0xe85(%rip),%r8
call   1080 <fprintf@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jne    1292 <main+0x102>
lea    0xe29(%rip),%rdi
call   1040 <printf@plt>
jmp    12e4 <main+0x154>
jmp    1297 <main+0x107>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%r9
lea    0xd4b(%rip),%rsi
lea    0xd54(%rip),%rdx
mov    $0x22,%ecx
lea    0xdef(%rip),%r8
call   1080 <fprintf@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xddb(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    0x2cc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cbb(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jne    1333 <main+0x1a3>
lea    0xdb2(%rip),%rdi
call   1040 <printf@plt>
jmp    1385 <main+0x1f5>
jmp    1338 <main+0x1a8>
mov    0x2c71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%r9
lea    0xcaa(%rip),%rsi
lea    0xcb3(%rip),%rdx
mov    $0x2b,%ecx
lea    0xd4e(%rip),%r8
call   1080 <fprintf@plt>
mov    0x2c1a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2e2d(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
lea    0xe3a(%rip),%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     127a <main+0xea>
mov    %eax,%ebx
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
jne    12af <main+0x11f>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
jne    12e4 <main+0x154>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
mov    0x2d2f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd71(%rip),%rsi
lea    0xd7a(%rip),%rdx
lea    0xe06(%rip),%r8
mov    %ebx,%edi
mov    $0x18,%ecx
jmp    1317 <main+0x187>
mov    0x2cfa(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd3c(%rip),%rsi
lea    0xd45(%rip),%rdx
lea    0xdd6(%rip),%r8
mov    %ebx,%edi
mov    $0x22,%ecx
jmp    1317 <main+0x187>
mov    0x2cc5(%rip),%rax
mov    (%rax),%ebx
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd07(%rip),%rsi
lea    0xd10(%rip),%rdx
lea    0xda1(%rip),%r8
mov    %ebx,%edi
mov    $0x2b,%ecx
mov    %rax,%r9
call   1080 <fprintf@plt>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i64, i64, i32 }
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.2 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day04/lock.c\00"
@.str.3 = [5 x i8] c"open\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.4 = [15 x i8] c"\E5\8A\A0\E9\94\81\E6\88\90\E5\8A\9F!\0A\00"
@.str.5 = [6 x i8] c"fcntl\00"
@.str.6 = [21 x i8] c"\E6\8C\89\E4\BB\BB\E6\84\8F\E9\94\AE\E8\A7\A3\E9\94\81!\0A\00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.7 = [15 x i8] c"\E8\A7\A3\E9\94\81\E6\88\90\E5\8A\9F!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.flock
store i32 0, i32* %1
%4 = call i32 @memset(%struct.flock* %3, i32 0, i32 32)
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9, i32 420)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %22
13: ; preds = %0
br label %14
14: ; preds = %13
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; No predecessors!
br label %22
22: ; preds = %21, %0
%23 = load i32, i32* @F_RDLCK
%24 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @SEEK_SET
%26 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
store i64 0, i64* %28
%29 = load i32, i32* %2
%30 = load i32, i32* @F_SETLKW
%31 = call i64 @fcntl(i32 %29, i32 %30, %struct.flock* %3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %44
35: ; preds = %22
br label %36
36: ; preds = %35
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; No predecessors!
br label %44
44: ; preds = %43, %33
%45 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%46 = call i32 (...) @getchar()
%47 = load i32, i32* @F_UNLCK
%48 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i32 %47, i32* %48
%49 = load i32, i32* %2
%50 = load i32, i32* @F_SETLK
%51 = call i64 @fcntl(i32 %49, i32 %50, %struct.flock* %3)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %64
55: ; preds = %44
br label %56
56: ; preds = %55
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; No predecessors!
br label %64
64: ; preds = %63, %53
%65 = load i32, i32* %2
%66 = call i32 @close(i32 %65)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i32, i64, i64, i32 }
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.2 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day04/lock.c\00"
@.str.3 = [5 x i8] c"open\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.5 = [6 x i8] c"fcntl\00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@str = [14 x i8] c"\E5\8A\A0\E9\94\81\E6\88\90\E5\8A\9F!\00"
@str.8 = [20 x i8] c"\E6\8C\89\E4\BB\BB\E6\84\8F\E9\94\AE\E8\A7\A3\E9\94\81!\00"
@str.9 = [14 x i8] c"\E8\A7\A3\E9\94\81\E6\88\90\E5\8A\9F!\00"
define i32 @main() {
%1 = alloca %struct.flock
%2 = bitcast %struct.flock* %1 to i8*
%3 = call i32 @memset(%struct.flock* %1, i32 0, i32 32)
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8, i32 420)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %18
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = load i32, i32* @F_RDLCK
%20 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @SEEK_SET
%22 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
%24 = bitcast i64* %23 to i8*
%25 = load i32, i32* @F_SETLKW
%26 = call i64 @fcntl(i32 %9, i32 %25, %struct.flock* %1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %18
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
%31 = call i32 (...) @getchar()
%32 = load i32, i32* @F_UNLCK
store i32 %32, i32* %20
%33 = load i32, i32* @F_SETLK
%34 = call i64 @fcntl(i32 %9, i32 %33, %struct.flock* %1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %43, label %46
36: ; preds = %18
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %28
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%45 = call i32 @close(i32 %9)
ret i32 0
46: ; preds = %28
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.2, i64 0, i64 0), i32 43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
}