#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*/ exit(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 *file = argv[1];
  int fd;
  struct flock lock;
  if (argc != 2) {
    printf("Usage: %s <file>\n", argv[0]);
    exit(1);
  }
  printf("opening %s\n", file);
  fd = open(file, 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...");
  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)
cmpl   $0x2,-0x8(%rbp)
je     11bb <main+0x4b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d8e(%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
cmp    $0x2,%edi
jne    1249 <main+0xc9>
mov    0x8(%rsi),%rbx
lea    0xe75(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dfa(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dbd(%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
mov    (%rsi),%rsi
lea    0xdad(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32 }
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [12 x i8] c"opening %s\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [9 x i8] c"locking\0A\00"
@F_WRLCK = global i32 0
@F_SETLKW = global i32 0
@.str.3 = [31 x i8] c"locked; hit Enter to unlock...\00"
@.str.4 = [11 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 i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8*, i8** %6
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_WRONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @memset(%struct.flock* %8, i32 0, i32 4)
%28 = load i32, i32* @F_WRLCK
%29 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %7
%31 = load i32, i32* @F_SETLKW
%32 = call i32 @fcntl(i32 %30, i32 %31, %struct.flock* %8)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 (...) @getchar()
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* @F_UNLCK
%37 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %7
%39 = load i32, i32* @F_SETLKW
%40 = call i32 @fcntl(i32 %38, i32 %39, %struct.flock* %8)
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i32 }
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [12 x i8] c"opening %s\0A\00"
@O_WRONLY = global i32 0
@F_WRLCK = global i32 0
@F_SETLKW = global i32 0
@.str.3 = [31 x i8] c"locked; hit Enter to unlock...\00"
@F_UNLCK = global i32 0
@str = [8 x i8] c"locking\00"
@str.5 = [10 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 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%14 = load i32, i32* @O_WRONLY
%15 = tail call i32 @open(i8* %5, i32 %14)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%17 = call i32 @memset(%struct.flock* %3, i32 0, i32 4)
%18 = load i32, i32* @F_WRLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @F_SETLKW
%21 = call i32 @fcntl(i32 %15, i32 %20, %struct.flock* %3)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 (...) @getchar()
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.5, i64 0, i64 0))
%25 = load i32, i32* @F_UNLCK
store i32 %25, i32* %19
%26 = load i32, i32* @F_SETLKW
%27 = call i32 @fcntl(i32 %15, i32 %26, %struct.flock* %3)
%28 = call i32 @close(i32 %15)
ret i32 0
}