#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 flock {
  int /*<<< orphan*/ l_type;
};
typedef int /*<<< orphan*/ lock;

/* Variables and functions */
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  char *path = argv[1];
  int fd;
  struct flock lock;
  printf("opening file: %s...\n", path);
  fd = open(path, O_WRONLY);
  printf("locking...\n");
  memset(&lock, 0, sizeof(lock));
  lock.l_type = F_WRLCK;
  fcntl(fd, F_SETLKW, &lock);
  printf("locked, hit Enter to unlock...\n");
  getchar();
  printf("unlocking...\n");
  lock.l_type = F_UNLCK;
  fcntl(fd, F_SETLKW, &lock);
  close(fd);
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x8(%rsi),%rbx
lea    0xe7c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e13(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.flock = type { i32 }
@.str = [21 x i8] c"opening file: %s...\0A\00"
@O_WRONLY = global i32 0
@.str.1 = [12 x i8] c"locking...\0A\00"
@F_WRLCK = global i32 0
@F_SETLKW = global i32 0
@.str.2 = [32 x i8] c"locked, hit Enter to unlock...\0A\00"
@.str.3 = [14 x i8] c"unlocking...\0A\00"
@F_UNLCK = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.flock
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i8*, i8** %6
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %7
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @memset(%struct.flock* %8, i32 0, i32 4)
%19 = load i32, i32* @F_WRLCK
%20 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %7
%22 = load i32, i32* @F_SETLKW
%23 = call i32 @fcntl(i32 %21, i32 %22, %struct.flock* %8)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 (...) @getchar()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* @F_UNLCK
%28 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %7
%30 = load i32, i32* @F_SETLKW
%31 = call i32 @fcntl(i32 %29, i32 %30, %struct.flock* %8)
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i32 }
@.str = [21 x i8] c"opening file: %s...\0A\00"
@O_WRONLY = global i32 0
@F_WRLCK = global i32 0
@F_SETLKW = global i32 0
@F_UNLCK = global i32 0
@str = [11 x i8] c"locking...\00"
@str.4 = [31 x i8] c"locked, hit Enter to unlock...\00"
@str.5 = [13 x i8] c"unlocking...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = bitcast %struct.flock* %3 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %5)
%8 = load i32, i32* @O_WRONLY
%9 = tail call i32 @open(i8* %5, i32 %8)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%11 = call i32 @memset(%struct.flock* %3, i32 0, i32 4)
%12 = load i32, i32* @F_WRLCK
%13 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @F_SETLKW
%15 = call i32 @fcntl(i32 %9, i32 %14, %struct.flock* %3)
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0))
%17 = call i32 (...) @getchar()
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
%19 = load i32, i32* @F_UNLCK
store i32 %19, i32* %13
%20 = load i32, i32* @F_SETLKW
%21 = call i32 @fcntl(i32 %9, i32 %20, %struct.flock* %3)
%22 = call i32 @close(i32 %9)
ret i32 0
}