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

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
scalar_t__ EEXIST;
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int O_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
char *curTime();
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int getpid();
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  int fd = open("1.txt", O_RDWR | O_CREAT, 0666);
  if (fd == -1) {
    perror("open 1.txt");
    if (errno == EEXIST) {
      fd = open("1.txt", O_RDWR | O_APPEND);
    } else {
      exit(-1);
    }
  }
  struct flock lock = {0};
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 10;
  lock.l_len = 40;
  if (fcntl(fd, F_SETLK, &lock) == -1) {
    if (errno == EACCES || errno == EAGAIN) {
      fcntl(fd, F_GETLK, &lock);
      if (lock.l_type == F_RDLCK) {
        printf("read lock by pid:%d", lock.l_pid);
      } else {
        printf("write lock by pid:%d", lock.l_pid);
      }
    }
    exit(-1);
  }
  printf("pid:%d set write lock file %s", getpid(), curTime());
  sleep(8);
  lock.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLK, &lock) == -1) {
    do {
      perror("fcntl");
      exit(-1);
    } while (0);
  }
  printf("pid:%d set write unlock %s", getpid(), curTime());
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
lea    0xe3d(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    123e <main+0x9e>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%rax
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
jne    122f <main+0x8f>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2d71(%rip),%rax
or     (%rax),%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
jmp    1239 <main+0x99>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    123e <main+0x9e>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0xa,-0x20(%rbp)
movl   $0x28,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1327 <main+0x187>
mov    0x2d0d(%rip),%rax
mov    (%rax),%rax
mov    0x2d1b(%rip),%rcx
cmp    (%rcx),%rax
je     12c8 <main+0x128>
mov    0x2cf3(%rip),%rax
mov    (%rax),%rax
mov    0x2cc1(%rip),%rcx
cmp    (%rcx),%rax
jne    131d <main+0x17d>
mov    -0x8(%rbp),%edi
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x28(%rbp),%rax
mov    0x2c90(%rip),%rcx
cmp    (%rcx),%rax
jne    1307 <main+0x167>
mov    -0x18(%rbp),%esi
lea    0xd10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1318 <main+0x178>
mov    -0x18(%rbp),%esi
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    131d <main+0x17d>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   10b0 <curTime@plt>
mov    -0x2c(%rbp),%esi
mov    %rax,%rdx
lea    0xcef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x8,%edi
call   10c0 <sleep@plt>
mov    0x2c6b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2c6a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    139d <main+0x1fd>
jmp    1387 <main+0x1e7>
lea    0xcc4(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   10b0 <curTime@plt>
mov    -0x30(%rbp),%esi
mov    %rax,%rdx
lea    0xc9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e41(%rip),%rbp
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     129e <main+0x10e>
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movabs $0x280000000a,%rax
mov    %rax,0x8(%rsp)
mov    0x2dd6(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12e5 <main+0x155>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <curTime@plt>
lea    0xe05(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x8,%edi
call   10b0 <sleep@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    (%r14),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     134f <main+0x1bf>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <curTime@plt>
lea    0xdd8(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd5b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%rax
mov    0x2cf5(%rip),%rcx
cmp    (%rcx),%rax
jne    1345 <main+0x1b5>
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
lea    0xd2e(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
jmp    11c9 <main+0x39>
mov    0x2cbc(%rip),%rax
mov    (%rax),%rax
mov    0x2cca(%rip),%rcx
cmp    (%rcx),%rax
je     1307 <main+0x177>
mov    0x2c7e(%rip),%rcx
cmp    (%rcx),%rax
jne    1345 <main+0x1b5>
mov    0x2cc2(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    (%rsp),%rax
mov    0x2c53(%rip),%rcx
cmp    (%rcx),%rax
mov    0x10(%rsp),%esi
lea    0xcd8(%rip),%rax
lea    0xce5(%rip),%rdi
cmove  %rax,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xcfc(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i32, i32, i32, i32, i32 }
@.str = [6 x i8] c"1.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [11 x i8] c"open 1.txt\00"
@errno = global i64 0
@EEXIST = global i64 0
@O_APPEND = global i32 0
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@F_GETLK = global i32 0
@F_RDLCK = global i64 0
@.str.2 = [20 x i8] c"read lock by pid:%d\00"
@.str.3 = [21 x i8] c"write lock by pid:%d\00"
@.str.4 = [30 x i8] c"pid:%d set write lock file %s\00"
@F_UNLCK = global i64 0
@.str.5 = [6 x i8] c"fcntl\00"
@.str.6 = [27 x i8] c"pid:%d set write unlock %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.flock
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %23
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%12 = load i64, i64* @errno
%13 = load i64, i64* @EEXIST
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_APPEND
%18 = or i32 %16, %17
%19 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %2
br label %22
20: ; preds = %10
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %15
br label %23
23: ; preds = %22, %0
%24 = bitcast %struct.flock* %3 to i8*
%25 = load i64, i64* @F_WRLCK
%26 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i64 %25, i64* %26
%27 = load i32, i32* @SEEK_SET
%28 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 4
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
store i32 10, i32* %29
%30 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i32 40, i32* %30
%31 = load i32, i32* %2
%32 = load i32, i32* @F_SETLK
%33 = call i32 @fcntl(i32 %31, i32 %32, %struct.flock* %3)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %62
35: ; preds = %23
%36 = load i64, i64* @errno
%37 = load i64, i64* @EACCES
%38 = icmp eq i64 %36, %37
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i64, i64* @errno
%41 = load i64, i64* @EAGAIN
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %60
43: ; preds = %39, %35
%44 = load i32, i32* %2
%45 = load i32, i32* @F_GETLK
%46 = call i32 @fcntl(i32 %44, i32 %45, %struct.flock* %3)
%47 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
%48 = load i64, i64* %47
%49 = load i64, i64* @F_RDLCK
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %55
51: ; preds = %43
%52 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
%53 = load i32, i32* %52
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %53)
br label %59
55: ; preds = %43
%56 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
%57 = load i32, i32* %56
%58 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %55, %51
br label %60
60: ; preds = %59, %39
%61 = call i32 @exit(i32 -1)
unreachable
62: ; preds = %23
%63 = call i32 (...) @getpid()
%64 = call i8* (...) @curTime()
%65 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %63, i8* %64)
%66 = call i32 @sleep(i32 8)
%67 = load i64, i64* @F_UNLCK
%68 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i64 %67, i64* %68
%69 = load i32, i32* %2
%70 = load i32, i32* @F_SETLK
%71 = call i32 @fcntl(i32 %69, i32 %70, %struct.flock* %3)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %78
73: ; preds = %62
br label %74
74: ; preds = %73
%75 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%76 = call i32 @exit(i32 -1)
unreachable
77: ; No predecessors!
br label %78
78: ; preds = %77, %62
%79 = call i32 (...) @getpid()
%80 = call i8* (...) @curTime()
%81 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %79, i8* %80)
%82 = load i32, i32* %2
%83 = call i32 @close(i32 %82)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i32, i32, i32, i32 }
@.str = [6 x i8] c"1.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [11 x i8] c"open 1.txt\00"
@errno = global i64 0
@EEXIST = global i64 0
@O_APPEND = global i32 0
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@F_GETLK = global i32 0
@F_RDLCK = global i64 0
@.str.2 = [20 x i8] c"read lock by pid:%d\00"
@.str.3 = [21 x i8] c"write lock by pid:%d\00"
@.str.4 = [30 x i8] c"pid:%d set write lock file %s\00"
@F_UNLCK = global i64 0
@.str.5 = [6 x i8] c"fcntl\00"
@.str.6 = [27 x i8] c"pid:%d set write unlock %s\00"
define i32 @main() {
%1 = alloca %struct.flock
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4, i32 438)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %19
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%9 = load i64, i64* @errno
%10 = load i64, i64* @EEXIST
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_APPEND
%15 = or i32 %14, %13
%16 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %19
17: ; preds = %7
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %12, %0
%20 = phi i32 [ %16, %12 ], [ %5, %0 ]
%21 = bitcast %struct.flock* %1 to i8*
%22 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
%23 = bitcast i32* %22 to i8*
%24 = load i64, i64* @F_WRLCK
%25 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
store i64 %24, i64* %25
%26 = load i32, i32* @SEEK_SET
%27 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 4
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
store i32 10, i32* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
store i32 40, i32* %29
%30 = load i32, i32* @F_SETLK
%31 = call i32 @fcntl(i32 %20, i32 %30, %struct.flock* %1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %52
33: ; preds = %19
%34 = load i64, i64* @errno
%35 = load i64, i64* @EACCES
%36 = icmp eq i64 %34, %35
%37 = load i64, i64* @EAGAIN
%38 = icmp eq i64 %34, %37
%39 = select i1 %36, i1 true, i1 %38
br i1 %39, label %40, label %50
40: ; preds = %33
%41 = load i32, i32* @F_GETLK
%42 = call i32 @fcntl(i32 %20, i32 %41, %struct.flock* %1)
%43 = load i64, i64* %25
%44 = load i64, i64* @F_RDLCK
%45 = icmp eq i64 %43, %44
%46 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
%47 = load i32, i32* %46
%48 = select i1 %45, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0)
%49 = call i32 (i8*, i32, ...) @printf(i8* %48, i32 %47)
br label %50
50: ; preds = %40, %33
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %19
%53 = call i32 (...) @getpid()
%54 = call i8* (...) @curTime()
%55 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %53, i8* %54)
%56 = call i32 @sleep(i32 8)
%57 = load i64, i64* @F_UNLCK
store i64 %57, i64* %25
%58 = load i32, i32* @F_SETLK
%59 = call i32 @fcntl(i32 %20, i32 %58, %struct.flock* %1)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %52
%62 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%63 = call i32 @exit(i32 -1)
unreachable
64: ; preds = %52
%65 = call i32 (...) @getpid()
%66 = call i8* (...) @curTime()
%67 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %65, i8* %66)
%68 = call i32 @close(i32 %20)
ret i32 0
}