#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*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getpid();
int /*<<< orphan*/ lock_reg(int, int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/, long, long);
int /*<<< orphan*/ lseek(int, long, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t strlen(char *);
int write(int, char *, int);
int main(int argc, char *args[]) {
  if (argc < 3) {
    fprintf(stderr, "useage error: %s\n", strerror(errno));
    printf("useage:%s <pathname> append_content\n", args[0]);
    exit(1);
  }
  int fd = open(args[1], O_WRONLY);
  lock_reg(fd, F_SETLKW, F_WRLCK, SEEK_SET, 0L, 0L);
  lseek(fd, 0L, SEEK_END);
  size_t c = strlen(args[2]);
  int i = 0;
  for (; i < c; i++) {
    if (write(fd, &args[2][i], 1) != 1) {
      fprintf(stderr, "write error:%s\n", strerror(errno));
      exit(1);
    }
    printf("%d(pid) write:%c\n", getpid(), args[2][i]);
    sleep(1);
  }
  close(fd);
  lock_reg(fd, F_SETLKW, F_UNLCK, SEEK_SET, 0L, 0L);
  printf("%d write finished\n", getpid());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    122b <main+0x6b>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xdf9(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
mov    0x2d58(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1050 <lock_reg@plt>
mov    -0x14(%rbp),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <lseek@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1344 <main+0x184>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1305 <main+0x145>
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xd41(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    129c <main+0xdc>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x14(%rbp),%edi
mov    0x2c8a(%rip),%rax
mov    (%rax),%esi
mov    0x2c79(%rip),%rax
mov    (%rax),%edx
mov    0x2c50(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1050 <lock_reg@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xcd2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    133f <main+0x17f>
mov    0x8(%rbx),%rdi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,%r14d
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    0x2db3(%rip),%r13
mov    0x0(%r13),%ecx
mov    %r14d,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <lock_reg@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1080 <lseek@plt>
mov    0x10(%rbx),%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
je     12eb <main+0x12b>
mov    %rax,%rbp
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    12b9 <main+0xf9>
mov    $0x1,%r12d
lea    0xde3(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    0x10(%rbx),%rcx
movsbl -0x1(%rcx,%r12,1),%edx
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10e0 <sleep@plt>
cmp    %r12,%rbp
je     12eb <main+0x12b>
mov    0x10(%rbx),%rsi
add    %r12,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x1,%r12
cmp    $0x1,%eax
je     1270 <main+0xb0>
mov    0x2d08(%rip),%rax
mov    (%rax),%ebx
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd60(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %r14d,%edi
call   1090 <close@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
mov    0x2cd5(%rip),%rax
mov    (%rax),%edx
mov    0x0(%r13),%ecx
mov    %r14d,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <lock_reg@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xd34(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c82(%rip),%rax
mov    (%rax),%ebp
mov    0x2c71(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xca3(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    (%rbx),%rsi
lea    0xca1(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"useage error: %s\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"useage:%s <pathname> append_content\0A\00"
@O_WRONLY = global i32 0
@F_SETLKW = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.2 = [16 x i8] c"write error:%s\0A\00"
@.str.3 = [18 x i8] c"%d(pid) write:%c\0A\00"
@F_UNLCK = global i32 0
@.str.4 = [19 x i8] c"%d write finished\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %21
11: ; preds = %2
%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 ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = load i32, i32* @F_SETLKW
%29 = load i32, i32* @F_WRLCK
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lock_reg(i32 %27, i32 %28, i32 %29, i32 %30, i64 0, i64 0)
%32 = load i32, i32* %6
%33 = load i32, i32* @SEEK_END
%34 = call i32 @lseek(i32 %32, i64 0, i32 %33)
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i64 @strlen(i8* %37)
store i64 %38, i64* %7
store i32 0, i32* %8
br label %39
39: ; preds = %72, %21
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = load i64, i64* %7
%43 = icmp ult i64 %41, %42
br i1 %43, label %44, label %75
44: ; preds = %39
%45 = load i32, i32* %6
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = call i32 @write(i32 %45, i8* %51, i32 1)
%53 = icmp ne i32 %52, 1
br i1 %53, label %54, label %60
54: ; preds = %44
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %44
%61 = call i32 (...) @getpid()
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 2
%64 = load i8*, i8** %63
%65 = load i32, i32* %8
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %61, i32 %69)
%71 = call i32 @sleep(i32 1)
br label %72
72: ; preds = %60
%73 = load i32, i32* %8
%74 = add nsw i32 %73, 1
store i32 %74, i32* %8
br label %39
75: ; preds = %39
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %6
%79 = load i32, i32* @F_SETLKW
%80 = load i32, i32* @F_UNLCK
%81 = load i32, i32* @SEEK_SET
%82 = call i32 @lock_reg(i32 %78, i32 %79, i32 %80, i32 %81, i64 0, i64 0)
%83 = call i32 (...) @getpid()
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %83)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"useage error: %s\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"useage:%s <pathname> append_content\0A\00"
@O_WRONLY = global i32 0
@F_SETLKW = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
@.str.2 = [16 x i8] c"write error:%s\0A\00"
@.str.3 = [18 x i8] c"%d(pid) write:%c\0A\00"
@F_UNLCK = global i32 0
@.str.4 = [19 x i8] c"%d write finished\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %12
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_WRONLY
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = load i32, i32* @F_SETLKW
%18 = load i32, i32* @F_WRLCK
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @lock_reg(i32 %16, i32 %17, i32 %18, i32 %19, i64 0, i64 0)
%21 = load i32, i32* @SEEK_END
%22 = tail call i32 @lseek(i32 %16, i64 0, i32 %21)
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = tail call i64 @strlen(i8* dereferenceable(1) %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %52, label %27
27: ; preds = %12
%28 = tail call i32 @write(i32 %16, i8* %24, i32 1)
%29 = icmp eq i32 %28, 1
br i1 %29, label %36, label %30
30: ; preds = %47, %27
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = tail call i8* @strerror(i32 %32)
%34 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %27, %47
%37 = phi i64 [ %45, %47 ], [ 0, %27 ]
%38 = tail call i32 (...) @getpid()
%39 = load i8*, i8** %23
%40 = getelementptr inbounds i8, i8* %39, i64 %37
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %38, i32 %42)
%44 = tail call i32 @sleep(i32 1)
%45 = add nuw i64 %37, 1
%46 = icmp eq i64 %45, %25
br i1 %46, label %52, label %47
47: ; preds = %36
%48 = load i8*, i8** %23
%49 = getelementptr inbounds i8, i8* %48, i64 %45
%50 = tail call i32 @write(i32 %16, i8* %49, i32 1)
%51 = icmp eq i32 %50, 1
br i1 %51, label %36, label %30
52: ; preds = %36, %12
%53 = tail call i32 @close(i32 %16)
%54 = load i32, i32* @F_SETLKW
%55 = load i32, i32* @F_UNLCK
%56 = load i32, i32* @SEEK_SET
%57 = tail call i32 @lock_reg(i32 %16, i32 %54, i32 %55, i32 %56, i64 0, i64 0)
%58 = tail call i32 (...) @getpid()
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %58)
ret i32 0
}