#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 S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int flock(int, int /*<<< orphan*/);
int /*<<< orphan*/ getchar();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  int fd;
  int ret;
  fd = open("locked", O_RDWR | O_CREAT, S_IWUSR | S_IRUSR | S_IROTH | S_IRGRP);
  if (fd == -1) {
    perror("locked");
    return 1;
  } else
    printf("File 'locked' opened\n");
  ret = flock(fd, LOCK_EX);
  if (ret == -1)
    perror("flock");
  (void)getchar();
  ret = flock(fd, LOCK_UN);
  if (ret == -1)
    perror("flock");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    0x2e1f(%rip),%rax
or     (%rax),%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
mov    0x2e1d(%rip),%rax
or     (%rax),%edx
lea    0xe54(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d6 <main+0x76>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124c <main+0xec>
lea    0xe2a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    120c <main+0xac>
lea    0xe16(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    -0x8(%rbp),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    123d <main+0xdd>
lea    0xde5(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e76(%rip),%rax
mov    0x2e87(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e54(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
lea    0xe60(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11fb <main+0x9b>
mov    %eax,%ebx
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <flock@plt>
cmp    $0xffffffff,%eax
je     120e <main+0xae>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <getchar@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <flock@plt>
cmp    $0xffffffff,%eax
je     121c <main+0xbc>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebp
jmp    11f2 <main+0x92>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
jmp    11cd <main+0x6d>
lea    0xde4(%rip),%rdi
call   1070 <perror@plt>
jmp    11eb <main+0x8b>

LLVM IR (-O0):
@.str = [7 x i8] c"locked\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IROTH = global i32 0
@S_IRGRP = global i32 0
@.str.1 = [22 x i8] c"File 'locked' opened\0A\00"
@LOCK_EX = global i32 0
@.str.2 = [6 x i8] c"flock\00"
@LOCK_UN = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IWUSR
%8 = load i32, i32* @S_IRUSR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %41
19: ; preds = %0
%20 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19
%22 = load i32, i32* %2
%23 = load i32, i32* @LOCK_EX
%24 = call i32 @flock(i32 %22, i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = call i32 (...) @getchar()
%31 = load i32, i32* %2
%32 = load i32, i32* @LOCK_UN
%33 = call i32 @flock(i32 %31, i32 %32)
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %29
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
store i32 0, i32* %1
br label %41
41: ; preds = %38, %17
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str = [7 x i8] c"locked\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IROTH = global i32 0
@S_IRGRP = global i32 0
@LOCK_EX = global i32 0
@.str.2 = [6 x i8] c"flock\00"
@LOCK_UN = global i32 0
@str = [21 x i8] c"File 'locked' opened\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IWUSR
%5 = load i32, i32* @S_IRUSR
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IROTH
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%17 = load i32, i32* @LOCK_EX
%18 = tail call i32 @flock(i32 %11, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = tail call i32 (...) @getchar()
%24 = load i32, i32* @LOCK_UN
%25 = tail call i32 @flock(i32 %11, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %22
%30 = tail call i32 @close(i32 %11)
br label %31
31: ; preds = %29, %13
%32 = phi i32 [ 1, %13 ], [ 0, %29 ]
ret i32 %32
}