#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 stat {
  int /*<<< orphan*/ st_size;
};
struct flock {
  int /*<<< orphan*/ l_type;
  int /*<<< orphan*/ l_pid;
  int /*<<< orphan*/ l_len;
  int /*<<< orphan*/ l_whence;
  scalar_t__ l_start;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int rand();
int /*<<< orphan*/ sendfile(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  if (argc == 3) {
    int fdFile2 = open(argv[2], O_WRONLY);
    int fdFile1 = open(argv[1], O_RDONLY);
    struct flock flock;
    struct stat stat_buf;
    fstat(fdFile1, &stat_buf);
    flock.l_type = F_WRLCK;
    flock.l_start = 0;
    flock.l_whence = SEEK_SET;
    flock.l_len = stat_buf.st_size;
    flock.l_pid = getpid();
    fcntl(fdFile2, F_SETLKW, &flock);
    printf("locked\n");
    sendfile(fdFile2, fdFile1, NULL, stat_buf.st_size);
    srand(time(NULL));
    int r = rand() % 10 + 1;
    sleep(r);
    flock.l_type = F_UNLCK;
    fcntl(fdFile2, F_SETLKW, &flock);
    printf("unlocked\n");
    r = rand() % 10 + 1;
    sleep(r);
    return 0;
  } else {
    printf("wrong number of arguments!");
    return -1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    12e4 <main+0x144>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10b0 <fstat@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
lea    0xdb8(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x38(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%edx
call   1080 <sendfile@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x0,%al
call   10c0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   10a0 <sleep@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
lea    0xd4a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10c0 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   10a0 <sleep@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f7 <main+0x157>
lea    0xd1f(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x3,%edi
jne    1303 <main+0x153>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%r12d
mov    0x8(%rbx),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x8(%rsp),%ebp
mov    %ebp,0x18(%rsp)
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x14(%rsp)
mov    0x2da1(%rip),%r13
mov    0x0(%r13),%esi
lea    0x10(%rsp),%r14
mov    %r12d,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
mov    %r12d,%edi
mov    %ebx,%esi
xor    %edx,%edx
mov    %ebp,%ecx
call   1090 <sendfile@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
xor    %eax,%eax
call   10d0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x0(%r13),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
lea    0xd4e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10d0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1315 <main+0x165>
lea    0xcf6(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32, i64 }
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_RDONLY = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str = [8 x i8] c"locked\0A\00"
@F_UNLCK = global i32 0
@.str.1 = [10 x i8] c"unlocked\0A\00"
@.str.2 = [27 x i8] c"wrong number of arguments!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.flock
%9 = alloca %struct.stat
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 3
br i1 %12, label %13, label %63
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 2
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_WRONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = call i32 @fstat(i32 %24, %struct.stat* %9)
%26 = load i32, i32* @F_WRLCK
%27 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 4
store i64 0, i64* %28
%29 = load i32, i32* @SEEK_SET
%30 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
store i32 %32, i32* %33
%34 = call i32 (...) @getpid()
%35 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i32 %34, i32* %35
%36 = load i32, i32* %6
%37 = load i32, i32* @F_SETLKW
%38 = call i32 @fcntl(i32 %36, i32 %37, %struct.flock* %8)
%39 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @sendfile(i32 %40, i32 %41, i32* null, i32 %43)
%45 = call i32 @time(i32* null)
%46 = call i32 @srand(i32 %45)
%47 = call i32 (...) @rand()
%48 = srem i32 %47, 10
%49 = add nsw i32 %48, 1
store i32 %49, i32* %10
%50 = load i32, i32* %10
%51 = call i32 @sleep(i32 %50)
%52 = load i32, i32* @F_UNLCK
%53 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %52, i32* %53
%54 = load i32, i32* %6
%55 = load i32, i32* @F_SETLKW
%56 = call i32 @fcntl(i32 %54, i32 %55, %struct.flock* %8)
%57 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%58 = call i32 (...) @rand()
%59 = srem i32 %58, 10
%60 = add nsw i32 %59, 1
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = call i32 @sleep(i32 %61)
store i32 0, i32* %3
br label %65
63: ; preds = %2
%64 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
65: ; preds = %63, %13
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32, i64 }
%struct.stat = type { i32 }
@O_WRONLY = global i32 0
@O_RDONLY = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@.str.2 = [27 x i8] c"wrong number of arguments!\00"
@str = [7 x i8] c"locked\00"
@str.3 = [9 x i8] c"unlocked\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = alloca %struct.stat
%5 = icmp eq i32 %0, 3
br i1 %5, label %6, label %46
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_WRONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = bitcast %struct.flock* %3 to i8*
%16 = bitcast %struct.stat* %4 to i8*
%17 = call i32 @fstat(i32 %14, %struct.stat* %4)
%18 = load i32, i32* @F_WRLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i64 0, i64* %20
%21 = load i32, i32* @SEEK_SET
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = tail call i32 (...) @getpid()
%27 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @F_SETLKW
%29 = call i32 @fcntl(i32 %10, i32 %28, %struct.flock* %3)
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%31 = call i32 @sendfile(i32 %10, i32 %14, i32* null, i32 %24)
%32 = call i32 @time(i32* null)
%33 = call i32 @srand(i32 %32)
%34 = call i32 (...) @rand()
%35 = srem i32 %34, 10
%36 = add nsw i32 %35, 1
%37 = call i32 @sleep(i32 %36)
%38 = load i32, i32* @F_UNLCK
store i32 %38, i32* %19
%39 = load i32, i32* @F_SETLKW
%40 = call i32 @fcntl(i32 %10, i32 %39, %struct.flock* %3)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.3, i64 0, i64 0))
%42 = call i32 (...) @rand()
%43 = srem i32 %42, 10
%44 = add nsw i32 %43, 1
%45 = call i32 @sleep(i32 %44)
br label %48
46: ; preds = %2
%47 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %6
%49 = phi i32 [ 0, %6 ], [ -1, %46 ]
ret i32 %49
}