#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;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};

/* Variables and functions */
scalar_t__ EDEADLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  char nada[5];
  struct flock cerrojo;
  int i, fd;
  for (i = 1; i < argc; i++) {
    if ((fd = open(argv[i], O_RDWR)) == -1) {
      perror("open fallo");
      continue;
    }
    cerrojo.l_type = F_WRLCK;
    cerrojo.l_whence = SEEK_SET;
    cerrojo.l_start = 0;
    cerrojo.l_len = 0;
    if ((fcntl(fd, F_SETLKW, &cerrojo)) == -1) {
      if (errno == EDEADLK)
        printf("ha detectado interbloqeo EDEADLK\n");
    }
    printf("cerrojo puesto sobre el archivo %s\n", argv[i]);
    printf("pulse cualquier tecla\n");
    scanf("%s", nada);
    sleep(1);
  }
  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)
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1283 <main+0x123>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    11c0 <main+0x60>
lea    0xe4a(%rip),%rdi
call   1070 <perror@plt>
jmp    1275 <main+0x115>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1233 <main+0xd3>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%rax
jne    122e <main+0xce>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1233 <main+0xd3>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x15(%rbp),%rsi
lea    0xe02(%rip),%rdi
call   1050 <scanf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    117d <main+0x1d>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jl     127c <main+0x10c>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    $0x1,%r13d
lea    0xea8(%rip),%rbp
lea    0xe9e(%rip),%r14
lea    0xb(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    (%rbx,%r13,8),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     126e <main+0xfe>
mov    0x2de6(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x24(%rsp)
mov    0x2de1(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x20(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2dec(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
lea    0x10(%rsp),%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1230 <main+0xc0>
mov    0x2dba(%rip),%rax
mov    (%rax),%rax
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%rax
jne    1230 <main+0xc0>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
nopl   0x0(%rax)
mov    (%rbx,%r13,8),%rsi
lea    0xde0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <scanf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0x1,%r13
cmp    %r13,%r15
jne    11b0 <main+0x40>
jmp    127c <main+0x10c>
lea    0xd9b(%rip),%rdi
call   1080 <perror@plt>
jmp    125f <main+0xef>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@.str = [11 x i8] c"open fallo\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EDEADLK = global i64 0
@.str.1 = [34 x i8] c"ha detectado interbloqeo EDEADLK\0A\00"
@.str.2 = [36 x i8] c"cerrojo puesto sobre el archivo %s\0A\00"
@.str.3 = [23 x i8] c"pulse cualquier tecla\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [5 x i8]
%7 = alloca %struct.flock
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
br label %10
10: ; preds = %54, %2
%11 = load i32, i32* %8
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %57
14: ; preds = %10
%15 = load i8**, i8*** %5
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %15, i64 %17
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %9
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %54
25: ; preds = %14
%26 = load i32, i32* @F_WRLCK
%27 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 3
store i32 %26, i32* %27
%28 = load i32, i32* @SEEK_SET
%29 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 2
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i64 0, i64* %31
%32 = load i32, i32* %9
%33 = load i32, i32* @F_SETLKW
%34 = call i32 @fcntl(i32 %32, i32 %33, %struct.flock* %7)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %43
36: ; preds = %25
%37 = load i64, i64* @errno
%38 = load i64, i64* @EDEADLK
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
br label %43
43: ; preds = %42, %25
%44 = load i8**, i8*** %5
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
%48 = load i8*, i8** %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%51 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%52 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = call i32 @sleep(i32 1)
br label %54
54: ; preds = %43, %23
%55 = load i32, i32* %8
%56 = add nsw i32 %55, 1
store i32 %56, i32* %8
br label %10
57: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@.str = [11 x i8] c"open fallo\00"
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EDEADLK = global i64 0
@.str.2 = [36 x i8] c"cerrojo puesto sobre el archivo %s\0A\00"
@.str.4 = [3 x i8] c"%s\00"
@str = [22 x i8] c"pulse cualquier tecla\00"
@str.5 = [33 x i8] c"ha detectado interbloqeo EDEADLK\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [5 x i8]
%4 = alloca %struct.flock
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.flock* %4 to i8*
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %43
8: ; preds = %2
%9 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 3
%10 = getelementptr inbounds %struct.flock, %struct.flock* %4, i64 0, i32 2
%11 = zext i32 %0 to i64
%12 = bitcast %struct.flock* %4 to i8*
br label %13
13: ; preds = %8, %40
%14 = phi i64 [ 1, %8 ], [ %41, %40 ]
%15 = getelementptr inbounds i8*, i8** %1, i64 %14
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %40
22: ; preds = %13
%23 = load i32, i32* @F_WRLCK
store i32 %23, i32* %9
%24 = load i32, i32* @SEEK_SET
store i32 %24, i32* %10
%25 = load i32, i32* @F_SETLKW
%26 = call i32 @fcntl(i32 %18, i32 %25, %struct.flock* %4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i64, i64* @errno
%30 = load i64, i64* @EDEADLK
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.5, i64 0, i64 0))
br label %34
34: ; preds = %28, %32, %22
%35 = load i8*, i8** %15
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%38 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%39 = call i32 @sleep(i32 1)
br label %40
40: ; preds = %34, %20
%41 = add nuw nsw i64 %14, 1
%42 = icmp eq i64 %41, %11
br i1 %42, label %43, label %13
43: ; preds = %40, %2
ret i32 0
}