#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 */

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ flock(int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int write(int, char *, int);
int main() {
  int pid;
  int fd;
  int ret;
  char buff[1024] = {0};
  int i;
  if (!(pid = fork())) {
    fd = open("./test.log", O_WRONLY | O_CREAT, 0640);
    flock(fd, LOCK_EX);
    lseek(fd, 0, SEEK_END);
    for (i = 0; i < 10; i++) {
      ret = write(fd, "A", 1);
      sleep(1);
    }
    flock(fd, LOCK_UN);
    close(fd);
    return 0;
  }
  fd = open("./test.log", O_WRONLY | O_CREAT, 0640);
  flock(fd, LOCK_EX);
  lseek(fd, 0, SEEK_END);
  for (i = 0; i < 10; i++) {
    ret = write(fd, "B", 1);
    sleep(1);
  }
  flock(fd, LOCK_UN);
  wait(&pid);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    1283 <main+0xf3>
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
lea    0xe1f(%rip),%rdi
mov    $0x1a0,%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
mov    -0xc(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
movl   $0x0,-0x414(%rbp)
cmpl   $0xa,-0x414(%rbp)
jge    125e <main+0xce>
mov    -0xc(%rbp),%edi
lea    0xdd8(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    121c <main+0x8c>
mov    -0xc(%rbp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1342 <main+0x1b2>
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
mov    0x2d65(%rip),%rax
or     (%rax),%esi
lea    0xd64(%rip),%rdi
mov    $0x1a0,%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
mov    -0xc(%rbp),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
movl   $0x0,-0x414(%rbp)
cmpl   $0xa,-0x414(%rbp)
jge    1319 <main+0x189>
mov    -0xc(%rbp),%edi
lea    0xd1f(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    12d7 <main+0x147>
mov    -0xc(%rbp),%edi
mov    0x2ca5(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
lea    -0x8(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ebp
mov    %eax,0xc(%rsp)
mov    0x2e1c(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe52(%rip),%rdi
mov    $0x1a0,%edx
call   1060 <open@plt>
mov    %eax,%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <flock@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %ebp,%ebp
je     1304 <main+0x184>
lea    0xe22(%rip),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <flock@plt>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
jmp    1415 <main+0x295>
lea    0xd00(%rip),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    0x2bbc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <flock@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"./test.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@LOCK_EX = global i32 0
@SEEK_END = global i32 0
@.str.1 = [2 x i8] c"A\00"
@LOCK_UN = global i32 0
@.str.2 = [2 x i8] c"B\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [1024 x i8]* %5 to i8*
%8 = call i32 (...) @fork()
store i32 %8, i32* %2
%9 = icmp ne i32 %8, 0
br i1 %9, label %37, label %10
10: ; preds = %0
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %13, i32 416)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = load i32, i32* @LOCK_EX
%17 = call i32 @flock(i32 %15, i32 %16)
%18 = load i32, i32* %3
%19 = load i32, i32* @SEEK_END
%20 = call i32 @lseek(i32 %18, i32 0, i32 %19)
store i32 0, i32* %6
br label %21
21: ; preds = %28, %10
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 10
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i32, i32* %3
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 %26, i32* %4
%27 = call i32 @sleep(i32 1)
br label %28
28: ; preds = %24
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %21
31: ; preds = %21
%32 = load i32, i32* %3
%33 = load i32, i32* @LOCK_UN
%34 = call i32 @flock(i32 %32, i32 %33)
%35 = load i32, i32* %3
%36 = call i32 @close(i32 %35)
store i32 0, i32* %1
br label %65
37: ; preds = %0
%38 = load i32, i32* @O_WRONLY
%39 = load i32, i32* @O_CREAT
%40 = or i32 %38, %39
%41 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %40, i32 416)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = load i32, i32* @LOCK_EX
%44 = call i32 @flock(i32 %42, i32 %43)
%45 = load i32, i32* %3
%46 = load i32, i32* @SEEK_END
%47 = call i32 @lseek(i32 %45, i32 0, i32 %46)
store i32 0, i32* %6
br label %48
48: ; preds = %55, %37
%49 = load i32, i32* %6
%50 = icmp slt i32 %49, 10
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = load i32, i32* %3
%53 = call i32 @write(i32 %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
store i32 %53, i32* %4
%54 = call i32 @sleep(i32 1)
br label %55
55: ; preds = %51
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 1
store i32 %57, i32* %6
br label %48
58: ; preds = %48
%59 = load i32, i32* %3
%60 = load i32, i32* @LOCK_UN
%61 = call i32 @flock(i32 %59, i32 %60)
%62 = call i32 @wait(i32* %2)
%63 = load i32, i32* %3
%64 = call i32 @close(i32 %63)
store i32 0, i32* %1
br label %65
65: ; preds = %58, %31
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [11 x i8] c"./test.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@LOCK_EX = global i32 0
@SEEK_END = global i32 0
@.str.1 = [2 x i8] c"A\00"
@LOCK_UN = global i32 0
@.str.2 = [2 x i8] c"B\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
store i32 %3, i32* %1
%4 = icmp eq i32 %3, 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7, i32 416)
%9 = load i32, i32* @LOCK_EX
%10 = tail call i32 @flock(i32 %8, i32 %9)
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @lseek(i32 %8, i32 0, i32 %11)
br i1 %4, label %13, label %37
13: ; preds = %0
%14 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%15 = tail call i32 @sleep(i32 1)
%16 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%17 = tail call i32 @sleep(i32 1)
%18 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%19 = tail call i32 @sleep(i32 1)
%20 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%21 = tail call i32 @sleep(i32 1)
%22 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%23 = tail call i32 @sleep(i32 1)
%24 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%25 = tail call i32 @sleep(i32 1)
%26 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%27 = tail call i32 @sleep(i32 1)
%28 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%29 = tail call i32 @sleep(i32 1)
%30 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%33 = tail call i32 @sleep(i32 1)
%34 = load i32, i32* @LOCK_UN
%35 = tail call i32 @flock(i32 %8, i32 %34)
%36 = tail call i32 @close(i32 %8)
br label %62
37: ; preds = %0
%38 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%39 = tail call i32 @sleep(i32 1)
%40 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%41 = tail call i32 @sleep(i32 1)
%42 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%43 = tail call i32 @sleep(i32 1)
%44 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%45 = tail call i32 @sleep(i32 1)
%46 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%47 = tail call i32 @sleep(i32 1)
%48 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%49 = tail call i32 @sleep(i32 1)
%50 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%51 = tail call i32 @sleep(i32 1)
%52 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%53 = tail call i32 @sleep(i32 1)
%54 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%55 = tail call i32 @sleep(i32 1)
%56 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%57 = tail call i32 @sleep(i32 1)
%58 = load i32, i32* @LOCK_UN
%59 = tail call i32 @flock(i32 %8, i32 %58)
%60 = call i32 @wait(i32* %1)
%61 = call i32 @close(i32 %8)
br label %62
62: ; preds = %37, %13
ret i32 0
}