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

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ 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, char *);
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_SETLKW, &lock) == -1) {
    do {
      perror("fcntl");
      exit(-1);
    } while (0);
  }
  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    0x2dbc(%rip),%rax
mov    (%rax),%rax
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%rax
jne    122f <main+0x8f>
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2d79(%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    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0xa,-0x20(%rbp)
movl   $0x28,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12ad <main+0x10d>
jmp    1297 <main+0xf7>
lea    0xd6d(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   10b0 <curTime@plt>
mov    -0x24(%rbp),%esi
mov    %rax,%rdx
lea    0xd46(%rip),%rdi
call   1050 <printf@plt>
mov    $0x8,%edi
call   10c0 <sleep@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ce0(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    131f <main+0x17f>
jmp    1309 <main+0x169>
lea    0xcfb(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   10b0 <curTime@plt>
mov    -0x28(%rbp),%esi
mov    %rax,%rdx
lea    0xcf2(%rip),%rdi
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   %rbx
sub    $0x18,%rsp
mov    0x2e43(%rip),%rbp
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
lea    0xe55(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1294 <main+0x104>
movl   $0x0,0x10(%rsp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
movabs $0x280000000a,%rax
mov    %rax,(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12d7 <main+0x147>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <curTime@plt>
lea    0xde7(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    $0x8,%edi
call   10b0 <sleep@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12d7 <main+0x147>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <curTime@plt>
lea    0xdb5(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd65(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%rax
mov    0x2d07(%rip),%rcx
cmp    (%rcx),%rax
jne    12e3 <main+0x153>
mov    0x2cd3(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
lea    0xd3c(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%ebx
jmp    11c7 <main+0x37>
lea    0xd2d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { 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 i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [30 x i8] c"pid:%d set write lock file %s\00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.4 = [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 i32, i32* @F_WRLCK
%26 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @SEEK_SET
%28 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 3
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 0
store i32 10, i32* %29
%30 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 1
store i32 40, i32* %30
%31 = load i32, i32* %2
%32 = load i32, i32* @F_SETLKW
%33 = call i32 @fcntl(i32 %31, i32 %32, %struct.flock* %3)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %23
br label %36
36: ; preds = %35
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; No predecessors!
br label %40
40: ; preds = %39, %23
%41 = call i32 (...) @getpid()
%42 = call i8* (...) @curTime()
%43 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %41, i8* %42)
%44 = call i32 @sleep(i32 8)
%45 = load i32, i32* @F_UNLCK
%46 = getelementptr inbounds %struct.flock, %struct.flock* %3, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* %2
%48 = load i32, i32* @F_SETLK
%49 = call i32 @fcntl(i32 %47, i32 %48, %struct.flock* %3)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %56
51: ; preds = %40
br label %52
52: ; preds = %51
%53 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%54 = call i32 @exit(i32 -1)
unreachable
55: ; No predecessors!
br label %56
56: ; preds = %55, %40
%57 = call i32 (...) @getpid()
%58 = call i8* (...) @curTime()
%59 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %57, i8* %58)
%60 = load i32, i32* %2
%61 = call i32 @close(i32 %60)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { 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 i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [30 x i8] c"pid:%d set write lock file %s\00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.4 = [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 4
store i32 0, i32* %22
%23 = load i32, i32* @F_WRLCK
%24 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @SEEK_SET
%26 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 3
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 0
store i32 10, i32* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %1, i64 0, i32 1
store i32 40, i32* %28
%29 = load i32, i32* @F_SETLKW
%30 = call i32 @fcntl(i32 %20, i32 %29, %struct.flock* %1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %19
%36 = call i32 (...) @getpid()
%37 = call i8* (...) @curTime()
%38 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %36, i8* %37)
%39 = call i32 @sleep(i32 8)
%40 = load i32, i32* @F_UNLCK
store i32 %40, i32* %24
%41 = load i32, i32* @F_SETLK
%42 = call i32 @fcntl(i32 %20, i32 %41, %struct.flock* %1)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %35
%45 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %35
%48 = call i32 (...) @getpid()
%49 = call i8* (...) @curTime()
%50 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %48, i8* %49)
%51 = call i32 @close(i32 %20)
ret i32 0
}