#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;
  int /*<<< orphan*/ l_pid;
  int /*<<< orphan*/ member_4;
  int /*<<< orphan*/ member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
  int fd;
  fl.l_pid = getpid();
  if (argc > 1)
    fl.l_type = F_RDLCK;
  if ((fd = open("1.txt", O_RDWR)) == -1) {
    perror("open");
    exit(1);
  }
  printf("Press <RETURN> to try to get lock: ");
  getchar();
  printf("Trying to get lock...");
  if (fcntl(fd, F_SETLKW, &fl) == -1) {
    perror("fcntl");
    exit(1);
  }
  printf("got lock\n");
  printf("Press <RETURN> to release lock: ");
  getchar();
  fl.l_type = F_UNLCK;
  if (fcntl(fd, F_SETLK, &fl) == -1) {
    perror("fcntl");
    exit(1);
  }
  printf("Unlocked.\n");
  close(fd);
  return 0;
}


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)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11f1 <main+0x71>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xdff(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    1228 <main+0xa8>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xddc(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    127b <main+0xfb>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdc9(%rip),%rdi
call   1050 <printf@plt>
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <getchar@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12da <main+0x15a>
lea    0xd7a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd95(%rip),%rdi
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
movl   $0x0,0x18(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x4(%rsp)
cmp    $0x2,%ebx
jl     11cf <main+0x3f>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe21(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     128d <main+0xfd>
mov    %eax,%ebx
lea    0xe15(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
lea    0xe26(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1284 <main+0xf4>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe12(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1284 <main+0xf4>
lea    0xe05(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdba(%rip),%rdi
jmp    1294 <main+0x104>
lea    0xd72(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i32, i32, i32, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_RDLCK = global i32 0
@.str = [6 x i8] c"1.txt\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [36 x i8] c"Press <RETURN> to try to get lock: \00"
@.str.3 = [22 x i8] c"Trying to get lock...\00"
@F_SETLKW = global i32 0
@.str.4 = [6 x i8] c"fcntl\00"
@.str.5 = [10 x i8] c"got lock\0A\00"
@.str.6 = [33 x i8] c"Press <RETURN> to release lock: \00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@.str.7 = [11 x i8] c"Unlocked.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.flock
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
%9 = load i32, i32* @F_WRLCK
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
%11 = load i32, i32* @SEEK_SET
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 4
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 5
store i32 0, i32* %15
%16 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 6
store i32 0, i32* %16
%17 = call i32 (...) @getpid()
%18 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = load i32, i32* @F_RDLCK
%23 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 %22, i32* %23
br label %24
24: ; preds = %21, %2
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 %26, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 (...) @getchar()
%34 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* %7
%36 = load i32, i32* @F_SETLKW
%37 = call i32 @fcntl(i32 %35, i32 %36, %struct.flock* %6)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %31
%43 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%45 = call i32 (...) @getchar()
%46 = load i32, i32* @F_UNLCK
%47 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i32 %46, i32* %47
%48 = load i32, i32* %7
%49 = load i32, i32* @F_SETLK
%50 = call i32 @fcntl(i32 %48, i32 %49, %struct.flock* %6)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %42
%53 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %42
%56 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
%57 = load i32, i32* %7
%58 = call i32 @close(i32 %57)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i32, i32, i32, i32, i32, i32, i32 }
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_RDLCK = global i32 0
@.str = [6 x i8] c"1.txt\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [36 x i8] c"Press <RETURN> to try to get lock: \00"
@.str.3 = [22 x i8] c"Trying to get lock...\00"
@F_SETLKW = global i32 0
@.str.4 = [6 x i8] c"fcntl\00"
@.str.6 = [33 x i8] c"Press <RETURN> to release lock: \00"
@F_UNLCK = global i32 0
@F_SETLK = global i32 0
@str = [9 x i8] c"got lock\00"
@str.8 = [10 x i8] c"Unlocked.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
%6 = load i32, i32* @F_WRLCK
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%9 = bitcast i32* %8 to i8*
%10 = tail call i32 (...) @getpid()
store i32 %10, i32* %7
%11 = icmp sgt i32 %0, 1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* @F_RDLCK
store i32 %13, i32* %5
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 (...) @getchar()
%24 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @F_SETLKW
%26 = call i32 @fcntl(i32 %16, i32 %25, %struct.flock* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%33 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%34 = call i32 (...) @getchar()
%35 = load i32, i32* @F_UNLCK
store i32 %35, i32* %5
%36 = load i32, i32* @F_SETLK
%37 = call i32 @fcntl(i32 %16, i32 %36, %struct.flock* %3)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %31
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.8, i64 0, i64 0))
%44 = call i32 @close(i32 %16)
ret i32 0
}