#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_RDWR;
int O_SYNC;
int /*<<< orphan*/ exit(int);
scalar_t__ flock(int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main() {
  int fd = open("xx", O_RDWR | O_SYNC);
  if (fd < 0) {
    exit(-1);
  }
  printf("%d\n", fd);
  if (flock(fd, LOCK_EX) < 0)
    exit(-2);
  sleep(10);
  if (flock(fd, LOCK_UN) < 0)
    exit(-2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e71(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1194 <main+0x44>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1050 <flock@plt>
cmp    $0x0,%rax
jge    11c8 <main+0x78>
mov    $0xfffffffe,%edi
call   1060 <exit@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
mov    -0x8(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   1050 <flock@plt>
cmp    $0x0,%rax
jge    11f7 <main+0xa7>
mov    $0xfffffffe,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2e81(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe96(%rip),%rdi
call   1040 <open@plt>
test   %eax,%eax
js     11c5 <main+0x75>
mov    %eax,%ebx
lea    0xe87(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <flock@plt>
test   %rax,%rax
js     11bb <main+0x6b>
mov    $0xa,%edi
call   1070 <sleep@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <flock@plt>
test   %rax,%rax
js     11bb <main+0x6b>
xor    %eax,%eax
pop    %rbx
ret
mov    $0xfffffffe,%edi
call   1060 <exit@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"xx\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@LOCK_EX = global i32 0
@LOCK_UN = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %3, %4
%6 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %2
%15 = load i32, i32* @LOCK_EX
%16 = call i64 @flock(i32 %14, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @exit(i32 -2)
unreachable
20: ; preds = %11
%21 = call i32 @sleep(i32 10)
%22 = load i32, i32* %2
%23 = load i32, i32* @LOCK_UN
%24 = call i64 @flock(i32 %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @exit(i32 -2)
unreachable
28: ; preds = %20
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"xx\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@LOCK_EX = global i32 0
@LOCK_UN = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_SYNC
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%10 = load i32, i32* @LOCK_EX
%11 = tail call i64 @flock(i32 %4, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @exit(i32 -2)
unreachable
15: ; preds = %8
%16 = tail call i32 @sleep(i32 10)
%17 = load i32, i32* @LOCK_UN
%18 = tail call i64 @flock(i32 %4, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @exit(i32 -2)
unreachable
22: ; preds = %15
ret i32 0
}