#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_RDWR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ flock(int, int /*<<< orphan*/);
int open(char *, int, int);
int read(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  assert(argc >= 2);
  int fd = open(argv[1], O_RDWR | O_CREAT, 0600);
  assert(fd != -1);
  flock(fd, LOCK_EX);
  assert(write(1, "OK\n", 3) == 3);
  char in;
  assert(read(0, &in, 1) == 1);
  flock(fd, LOCK_UN);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x14(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
mov    $0x1,%edi
lea    0xe34(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
cmp    $0x3,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %edi,%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x14(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
cmp    $0x2,%edi
setge  %al
mov    %eax,%edi
call   1050 <assert@plt>
mov    0x8(%rbx),%rdi
mov    0x2e6e(%rip),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1050 <assert@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <flock@plt>
lea    0xe52(%rip),%rsi
mov    $0x1,%edi
mov    $0x3,%edx
call   1030 <write@plt>
xor    %edi,%edi
cmp    $0x3,%eax
sete   %dil
call   1050 <assert@plt>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <flock@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@LOCK_EX = global i32 0
@.str = [4 x i8] c"OK\0A\00"
@LOCK_UN = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sge i32 %8, 2
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17, i32 384)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, -1
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32, i32* %6
%24 = load i32, i32* @LOCK_EX
%25 = call i32 @flock(i32 %23, i32 %24)
%26 = call i32 @write(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%27 = icmp eq i32 %26, 3
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = call i32 @read(i32 0, i8* %7, i32 1)
%31 = icmp eq i32 %30, 1
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %6
%35 = load i32, i32* @LOCK_UN
%36 = call i32 @flock(i32 %34, i32 %35)
ret i32 0
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@LOCK_EX = global i32 0
@.str = [4 x i8] c"OK\0A\00"
@LOCK_UN = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp sgt i32 %0, 1
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %8, i32 %11, i32 384)
%13 = icmp ne i32 %12, -1
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = load i32, i32* @LOCK_EX
%17 = tail call i32 @flock(i32 %12, i32 %16)
%18 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%19 = icmp eq i32 %18, 3
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
%22 = call i32 @read(i32 0, i8* %3, i32 1)
%23 = icmp eq i32 %22, 1
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
%26 = load i32, i32* @LOCK_UN
%27 = tail call i32 @flock(i32 %12, i32 %26)
ret i32 0
}