#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 fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  char *file = argv[1];
  int fd;
  struct flock lock;
  int ret;
  printf("opening %s\n", file);
  fd = open(file, O_WRONLY);
  printf("locking\n");
  memset(&lock, 0, sizeof(lock));
  lock.l_type = F_WRLCK;
  ret = fcntl(fd, F_SETLKW, &lock);
  if (ret == 0)
    printf("locked; hit Enter to unlock... ");
  else
    perror("!!");
  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    $0x30,%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    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1218 <main+0xa8>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1224 <main+0xb4>
lea    0xe0d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d99(%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    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x8(%rsi),%rbx
lea    0xe6c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
lea    0xe79(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e03(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
test   %eax,%eax
jne    1246 <main+0xc6>
lea    0xe0f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
lea    0xe1d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xddf(%rip),%rdi
call   10a0 <perror@plt>
jmp    1204 <main+0x84>

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

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