#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct flock lock;
  int fd = 0;
  char *file = argv[1];
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <file>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  printf("Open file to lock: %s\n", file);
  fd = open(file, O_WRONLY);
  if (fd == -1) {
    fprintf(stderr, "Err. Open file failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  puts("File is lock");
  memset(&lock, 0, sizeof(lock));
  lock.l_type = F_WRLCK;
  if (fcntl(fd, F_SETLKW, &lock) < 0) {
    fprintf(stderr, "Err. fcntl() failed: %s\n", strerror(errno));
    return 1;
  }
  puts("Locked: [enter to unlock]");
  getchar();
  puts("Unlock");
  lock.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLKW, &lock) < 0) {
    fprintf(stderr, "Err. fcntl() failed: %s\n", strerror(errno));
    return 1;
  }
  close(fd);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fd <main+0x5d>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe16(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xe0a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1266 <main+0xc6>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd6(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xdd7(%rip),%rdi
call   1030 <puts@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    12e5 <main+0x145>
mov    0x2d1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xd7d(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    137b <main+0x1db>
lea    0xd7e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
lea    0xd85(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cce(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
jge    1367 <main+0x1c7>
mov    0x2c9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2c86(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xcfb(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    137b <main+0x1db>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    0x2c3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    12b0 <main+0x110>
mov    0x8(%rsi),%rbx
lea    0xe55(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12c5 <main+0x125>
mov    %eax,%ebx
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dd3(%rip),%r15
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <fcntl@plt>
test   %rax,%rax
js     1279 <main+0xd9>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
js     1279 <main+0xd9>
mov    %ebx,%edi
call   1070 <close@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
jmp    12a6 <main+0x106>
mov    0x2d50(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdba(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd3d(%rip),%rsi
jmp    12e8 <main+0x148>
mov    0x2d04(%rip),%rax
mov    (%rax),%ebx
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd46(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [23 x i8] c"Open file to lock: %s\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [27 x i8] c"Err. Open file failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [13 x i8] c"File is lock\00"
@F_WRLCK = global i32 0
@F_SETLKW = global i32 0
@.str.4 = [25 x i8] c"Err. fcntl() failed: %s\0A\00"
@.str.5 = [26 x i8] c"Locked: [enter to unlock]\00"
@.str.6 = [7 x i8] c"Unlock\00"
@F_UNLCK = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.flock
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
store i8* %11, i8** %8
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %22
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i8*, i8** %8
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %8
%26 = load i32, i32* @O_WRONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %37
30: ; preds = %22
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %22
%38 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @memset(%struct.flock* %6, i32 0, i32 4)
%40 = load i32, i32* @F_WRLCK
%41 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %7
%43 = load i32, i32* @F_SETLKW
%44 = call i64 @fcntl(i32 %42, i32 %43, %struct.flock* %6)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %37
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %49)
store i32 1, i32* %3
br label %70
51: ; preds = %37
%52 = call i32 @puts(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%53 = call i32 (...) @getchar()
%54 = call i32 @puts(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%55 = load i32, i32* @F_UNLCK
%56 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 %55, i32* %56
%57 = load i32, i32* %7
%58 = load i32, i32* @F_SETLKW
%59 = call i64 @fcntl(i32 %57, i32 %58, %struct.flock* %6)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %51
%62 = load i32, i32* @stderr
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %64)
store i32 1, i32* %3
br label %70
66: ; preds = %51
%67 = load i32, i32* %7
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* @EXIT_SUCCESS
store i32 %69, i32* %3
br label %70
70: ; preds = %66, %61, %46
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.flock = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [23 x i8] c"Open file to lock: %s\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [27 x i8] c"Err. Open file failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [13 x i8] c"File is lock\00"
@F_WRLCK = global i32 0
@F_SETLKW = global i32 0
@.str.4 = [25 x i8] c"Err. fcntl() failed: %s\0A\00"
@.str.5 = [26 x i8] c"Locked: [enter to unlock]\00"
@.str.6 = [7 x i8] c"Unlock\00"
@F_UNLCK = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i32 %0, 2
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%16 = load i32, i32* @O_WRONLY
%17 = tail call i32 @open(i8* %6, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @memset(%struct.flock* %3, i32 0, i32 4)
%29 = load i32, i32* @F_WRLCK
%30 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* @F_SETLKW
%32 = call i64 @fcntl(i32 %17, i32 %31, %struct.flock* %3)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %37)
br label %55
39: ; preds = %26
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%41 = call i32 (...) @getchar()
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%43 = load i32, i32* @F_UNLCK
store i32 %43, i32* %30
%44 = load i32, i32* @F_SETLKW
%45 = call i64 @fcntl(i32 %17, i32 %44, %struct.flock* %3)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %39
%48 = load i32, i32* @stderr
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %55
52: ; preds = %39
%53 = call i32 @close(i32 %17)
%54 = load i32, i32* @EXIT_SUCCESS
br label %55
55: ; preds = %52, %47, %34
%56 = phi i32 [ 1, %34 ], [ 1, %47 ], [ %54, %52 ]
ret i32 %56
}