#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 */
typedef int /*<<< orphan*/ time_t;
struct flock {
  scalar_t__ l_type;
  int /*<<< orphan*/ l_pid;
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETLK;
scalar_t__ F_RDLCK;
int /*<<< orphan*/ F_SETLK;
scalar_t__ F_UNLCK;
scalar_t__ F_WRLCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
char *ctime(int /*<<< orphan*/ *);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ getpid();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  int fd;
  struct flock fl;
  fd = open(argv[1], O_CREAT | O_RDWR, 0777);
  if (fd == -1) {
    perror("Error de open.");
  }
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;
  fl.l_pid = getpid();
  if (fcntl(fd, F_GETLK, &fl) < 0) {
    perror("Error fcntl.\n");
  }
  if (fl.l_type == F_RDLCK)
    printf("READ LOCK.\n");
  else if (fl.l_type == F_WRLCK)
    printf("WRITE LOCK.\n");
  else {
    fl.l_type = F_WRLCK;
    if (fcntl(fd, F_SETLK, &fl) < 0) {
      perror("Error al poner cerrojo de escritura.");
    }
    time_t t = time(NULL);
    printf("Hora actual: %s", ctime(&t));
    sleep(30);
    fl.l_type = F_UNLCK;
    if (fcntl(fd, F_SETLK, &fl) < 0) {
      perror("Error al liberar el cerrojo.");
    }
    printf("Libero el cerrojo\n");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e3 <main+0x53>
lea    0xe22(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1242 <main+0xb2>
lea    0xdd2(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x40(%rbp),%rax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%rax
jne    1269 <main+0xd9>
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1345 <main+0x1b5>
mov    -0x40(%rbp),%rax
mov    0x2d3c(%rip),%rcx
cmp    (%rcx),%rax
jne    1290 <main+0x100>
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1340 <main+0x1b0>
mov    0x2d19(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    12c9 <main+0x139>
lea    0xd72(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x44(%rbp)
lea    -0x44(%rbp),%rdi
call   1050 <ctime@plt>
mov    %rax,%rsi
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1e,%edi
call   10b0 <sleep@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cc7(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1332 <main+0x1a2>
lea    0xd3e(%rip),%rdi
call   10a0 <perror@plt>
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1345 <main+0x1b5>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e46(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1ff,%edx
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12e1 <main+0x141>
mov    0x2dd7(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x18(%rsp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
js     12f2 <main+0x152>
mov    0x10(%rsp),%rax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%rax
jne    123c <main+0x9c>
lea    0xe56(%rip),%rdi
jmp    12cb <main+0x12b>
mov    (%r14),%rcx
cmp    %rcx,%rax
jne    124d <main+0xad>
lea    0xe36(%rip),%rdi
jmp    12cb <main+0x12b>
mov    %rcx,0x10(%rsp)
mov    0x2d7f(%rip),%r14
mov    (%r14),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
js     1303 <main+0x163>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1060 <ctime@plt>
lea    0xdb5(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1e,%edi
call   10c0 <sleep@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    (%r14),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1040 <fcntl@plt>
test   %rax,%rax
js     1314 <main+0x174>
lea    0xda4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd18(%rip),%rdi
call   10b0 <perror@plt>
jmp    11d2 <main+0x32>
lea    0xd16(%rip),%rdi
call   10b0 <perror@plt>
jmp    121f <main+0x7f>
lea    0xd13(%rip),%rdi
call   10b0 <perror@plt>
jmp    1271 <main+0xd1>
lea    0xd37(%rip),%rdi
call   10b0 <perror@plt>
jmp    12c4 <main+0x124>

LLVM IR (-O0):
%struct.flock = type { i64, i32, i64, i64, i32 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [15 x i8] c"Error de open.\00"
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str.1 = [14 x i8] c"Error fcntl.\0A\00"
@F_RDLCK = global i64 0
@.str.2 = [12 x i8] c"READ LOCK.\0A\00"
@.str.3 = [13 x i8] c"WRITE LOCK.\0A\00"
@F_SETLK = global i32 0
@.str.4 = [37 x i8] c"Error al poner cerrojo de escritura.\00"
@.str.5 = [16 x i8] c"Hora actual: %s\00"
@F_UNLCK = global i64 0
@.str.6 = [29 x i8] c"Error al liberar el cerrojo.\00"
@.str.7 = [19 x i8] c"Libero el cerrojo\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.flock
%8 = alloca i32
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
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_RDWR
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %11, i32 %14, i32 511)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = load i64, i64* @F_WRLCK
%22 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i64 %21, i64* %22
%23 = load i32, i32* @SEEK_SET
%24 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 4
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i64 0, i64* %26
%27 = call i32 (...) @getpid()
%28 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* %6
%30 = load i32, i32* @F_GETLK
%31 = call i64 @fcntl(i32 %29, i32 %30, %struct.flock* %7)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %20
%36 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
%37 = load i64, i64* %36
%38 = load i64, i64* @F_RDLCK
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %74
42: ; preds = %35
%43 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
%44 = load i64, i64* %43
%45 = load i64, i64* @F_WRLCK
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %73
49: ; preds = %42
%50 = load i64, i64* @F_WRLCK
%51 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i64 %50, i64* %51
%52 = load i32, i32* %6
%53 = load i32, i32* @F_SETLK
%54 = call i64 @fcntl(i32 %52, i32 %53, %struct.flock* %7)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %49
%57 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %56, %49
%59 = call i32 @time(i32* null)
store i32 %59, i32* %8
%60 = call i8* @ctime(i32* %8)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %60)
%62 = call i32 @sleep(i32 30)
%63 = load i64, i64* @F_UNLCK
%64 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i64 %63, i64* %64
%65 = load i32, i32* %6
%66 = load i32, i32* @F_SETLK
%67 = call i64 @fcntl(i32 %65, i32 %66, %struct.flock* %7)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %58
%70 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %71
71: ; preds = %69, %58
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
br label %73
73: ; preds = %71, %47
br label %74
74: ; preds = %73, %40
%75 = load i32, i32* %6
%76 = call i32 @close(i32 %75)
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i64, i32, i64, i64, i32 }
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [15 x i8] c"Error de open.\00"
@F_WRLCK = global i64 0
@SEEK_SET = global i32 0
@F_GETLK = global i32 0
@.str.1 = [14 x i8] c"Error fcntl.\0A\00"
@F_RDLCK = global i64 0
@F_SETLK = global i32 0
@.str.4 = [37 x i8] c"Error al poner cerrojo de escritura.\00"
@.str.5 = [16 x i8] c"Hora actual: %s\00"
@F_UNLCK = global i64 0
@.str.6 = [29 x i8] c"Error al liberar el cerrojo.\00"
@str = [18 x i8] c"Libero el cerrojo\00"
@str.8 = [12 x i8] c"WRITE LOCK.\00"
@str.9 = [11 x i8] c"READ LOCK.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = alloca i32
%5 = bitcast %struct.flock* %3 to i8*
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_RDWR
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %7, i32 %10, i32 511)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i64, i64* @F_WRLCK
%17 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i64 %16, i64* %17
%18 = load i32, i32* @SEEK_SET
%19 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 4
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%21 = bitcast i64* %20 to i8*
%22 = tail call i32 (...) @getpid()
%23 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @F_GETLK
%25 = call i64 @fcntl(i32 %11, i32 %24, %struct.flock* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %15
%30 = load i64, i64* %17
%31 = load i64, i64* @F_RDLCK
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0))
br label %60
35: ; preds = %29
%36 = load i64, i64* @F_WRLCK
%37 = icmp eq i64 %30, %36
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0))
br label %60
40: ; preds = %35
store i64 %36, i64* %17
%41 = load i32, i32* @F_SETLK
%42 = call i64 @fcntl(i32 %11, i32 %41, %struct.flock* %3)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %40
%47 = bitcast i32* %4 to i8*
%48 = call i32 @time(i32* null)
store i32 %48, i32* %4
%49 = call i8* @ctime(i32* %4)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = call i32 @sleep(i32 30)
%52 = load i64, i64* @F_UNLCK
store i64 %52, i64* %17
%53 = load i32, i32* @F_SETLK
%54 = call i64 @fcntl(i32 %11, i32 %53, %struct.flock* %3)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %46
%57 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %58
58: ; preds = %56, %46
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %60
60: ; preds = %38, %58, %33
%61 = call i32 @close(i32 %11)
ret i32 0
}