#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 {
  scalar_t__ l_len;
  scalar_t__ l_start;
  void *l_whence;
  int /*<<< orphan*/ l_type;
};

/* Variables and functions */
int EDEADLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
void *SEEK_SET;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  struct flock cerrojo;
  int fd, i;
  if (argc != 2) {
    printf("Modo de uso: %s <programa> <archivo>\n\n", argv[0]);
    exit(1);
  } else {
    char *str_file = argv[1];
    if ((fd = open(str_file, O_RDWR)) == -1) {
      perror("Fallo al abrir el archivo");
      return 0;
    }
    cerrojo.l_type = F_WRLCK;
    cerrojo.l_whence = SEEK_SET;
    cerrojo.l_start = 0;
    cerrojo.l_len = 0;
    printf("Intentando bloquear %s\n", str_file);
    if (fcntl(fd, F_SETLKW, &cerrojo) == EDEADLK) {
      printf("%s ha dado un EDEADLK\n", str_file);
    }
    printf("Procesando el archivo %s\n", str_file);
    for (i = 0; i < 10; i++) {
      sleep(1);
    }
    cerrojo.l_type = F_UNLCK;
    cerrojo.l_whence = SEEK_SET;
    cerrojo.l_start = 0;
    cerrojo.l_len = 0;
    if (fcntl(fd, F_SETLKW, &cerrojo) == -1) {
      perror("Error al desbloquear el archivo");
    }
    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)
cmpl   $0x2,-0x8(%rbp)
je     119d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11df <main+0x7f>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e1 <main+0x181>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%eax
jne    124d <main+0xed>
mov    -0x40(%rbp),%rsi
lea    0xe11(%rip),%rdi
call   1040 <printf@plt>
mov    -0x40(%rbp),%rsi
lea    0xe18(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x38(%rbp)
cmpl   $0xa,-0x38(%rbp)
jge    1286 <main+0x126>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1264 <main+0x104>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12da <main+0x17a>
lea    0xdb5(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jne    12b8 <main+0x158>
mov    0x8(%rsi),%rbx
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     12a1 <main+0x141>
mov    %eax,%ebp
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e19(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
lea    0xe94(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2e14(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%eax
jne    11f3 <main+0x93>
lea    0xe7e(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xe86(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    (%r14),%rax
mov    %rax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    (%r15),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12aa <main+0x14a>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8f(%rip),%rdi
jmp    12b1 <main+0x151>
lea    0xde9(%rip),%rdi
call   1060 <perror@plt>
jmp    1294 <main+0x134>
mov    (%rsi),%rsi
lea    0xd4e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.flock = type { i64, i64, i8*, i32 }
@.str = [39 x i8] c"Modo de uso: %s <programa> <archivo>\0A\0A\00"
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"Fallo al abrir el archivo\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i8* null
@.str.2 = [24 x i8] c"Intentando bloquear %s\0A\00"
@F_SETLKW = global i32 0
@EDEADLK = global i32 0
@.str.3 = [23 x i8] c"%s ha dado un EDEADLK\0A\00"
@.str.4 = [26 x i8] c"Procesando el archivo %s\0A\00"
@F_UNLCK = global i32 0
@.str.5 = [32 x i8] c"Error al desbloquear el archivo\00"
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 i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %7
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %70
28: ; preds = %18
%29 = load i32, i32* @F_WRLCK
%30 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %29, i32* %30
%31 = load i8*, i8** @SEEK_SET
%32 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i8* %31, i8** %32
%33 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i64 0, i64* %33
%34 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 0, i64* %34
%35 = load i8*, i8** %9
%36 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %7
%38 = load i32, i32* @F_SETLKW
%39 = call i32 @fcntl(i32 %37, i32 %38, %struct.flock* %6)
%40 = load i32, i32* @EDEADLK
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %28
%43 = load i8*, i8** %9
%44 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %28
%46 = load i8*, i8** %9
%47 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %46)
store i32 0, i32* %8
br label %48
48: ; preds = %53, %45
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 10
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = call i32 @sleep(i32 1)
br label %53
53: ; preds = %51
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
br label %48
56: ; preds = %48
%57 = load i32, i32* @F_UNLCK
%58 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 3
store i32 %57, i32* %58
%59 = load i8*, i8** @SEEK_SET
%60 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 2
store i8* %59, i8** %60
%61 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 1
store i64 0, i64* %61
%62 = getelementptr inbounds %struct.flock, %struct.flock* %6, i32 0, i32 0
store i64 0, i64* %62
%63 = load i32, i32* %7
%64 = load i32, i32* @F_SETLKW
%65 = call i32 @fcntl(i32 %63, i32 %64, %struct.flock* %6)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %69
67: ; preds = %56
%68 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67, %56
store i32 0, i32* %3
br label %70
70: ; preds = %69, %26
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i8*, i32 }
@.str = [39 x i8] c"Modo de uso: %s <programa> <archivo>\0A\0A\00"
@O_RDWR = global i32 0
@.str.1 = [26 x i8] c"Fallo al abrir el archivo\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i8* null
@.str.2 = [24 x i8] c"Intentando bloquear %s\0A\00"
@F_SETLKW = global i32 0
@EDEADLK = global i32 0
@.str.3 = [23 x i8] c"%s ha dado un EDEADLK\0A\00"
@.str.4 = [26 x i8] c"Procesando el archivo %s\0A\00"
@F_UNLCK = global i32 0
@.str.5 = [32 x i8] c"Error al desbloquear el archivo\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %51
18: ; preds = %10
%19 = load i32, i32* @F_WRLCK
%20 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i32 %19, i32* %20
%21 = load i8*, i8** @SEEK_SET
%22 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %21, i8** %22
%23 = bitcast %struct.flock* %3 to i8*
%24 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%25 = load i32, i32* @F_SETLKW
%26 = call i32 @fcntl(i32 %14, i32 %25, %struct.flock* %3)
%27 = load i32, i32* @EDEADLK
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %18
%30 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %12)
br label %31
31: ; preds = %29, %18
%32 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%33 = call i32 @sleep(i32 1)
%34 = call i32 @sleep(i32 1)
%35 = call i32 @sleep(i32 1)
%36 = call i32 @sleep(i32 1)
%37 = call i32 @sleep(i32 1)
%38 = call i32 @sleep(i32 1)
%39 = call i32 @sleep(i32 1)
%40 = call i32 @sleep(i32 1)
%41 = call i32 @sleep(i32 1)
%42 = call i32 @sleep(i32 1)
%43 = load i32, i32* @F_UNLCK
store i32 %43, i32* %20
%44 = load i8*, i8** @SEEK_SET
store i8* %44, i8** %22
%45 = bitcast %struct.flock* %3 to i8*
%46 = load i32, i32* @F_SETLKW
%47 = call i32 @fcntl(i32 %14, i32 %46, %struct.flock* %3)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %31
%50 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %31, %49, %16
ret i32 0
}