#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_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ lock_set(int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int main(void) {
  int fd;
  fd = open("a.txt", O_RDWR | O_CREAT, 0666);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  lock_set(fd, F_WRLCK);
  getchar();
  lock_set(fd, F_UNLCK);
  getchar();
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a5 <main+0x55>
lea    0xe70(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1030 <lock_set@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    -0x8(%rbp),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
call   1030 <lock_set@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe96(%rip),%rdi
mov    $0x1b6,%edx
call   1050 <open@plt>
test   %eax,%eax
jns    118e <main+0x3e>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %eax,%ebx
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <lock_set@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <lock_set@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"a.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@F_WRLCK = global i32 0
@F_UNLCK = 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_CREAT
%5 = or i32 %3, %4
%6 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5, i32 438)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %2
%14 = load i32, i32* @F_WRLCK
%15 = call i32 @lock_set(i32 %13, i32 %14)
%16 = call i32 (...) @getchar()
%17 = load i32, i32* %2
%18 = load i32, i32* @F_UNLCK
%19 = call i32 @lock_set(i32 %17, i32 %18)
%20 = call i32 (...) @getchar()
%21 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"a.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@F_WRLCK = global i32 0
@F_UNLCK = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @F_WRLCK
%11 = tail call i32 @lock_set(i32 %4, i32 %10)
%12 = tail call i32 (...) @getchar()
%13 = load i32, i32* @F_UNLCK
%14 = tail call i32 @lock_set(i32 %4, i32 %13)
%15 = tail call i32 (...) @getchar()
%16 = tail call i32 @exit(i32 0)
unreachable
}