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

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_SETLK;
void *F_WRLCK;
int /*<<< orphan*/ O_RDWR;
void *SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, const char *argv[]) {
  if (argc != 3) {
    fprintf(stderr, "expecting exactly two arguments\n");
    return 2;
  }
  int fd1 = -1;
  int fd2 = -1;
  int result = 2;
  fd1 = open(argv[1], O_RDWR);
  if (fd1 == -1) {
    perror("failed to open the first file");
    goto exit;
  }
  fd2 = open(argv[2], O_RDWR);
  if (fd2 == -1) {
    perror("failed to open the second file");
    goto exit;
  }
  struct flock lock;
  memset(&lock, 0, sizeof(lock));
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  if (fcntl(fd1, F_SETLK, &lock) == -1) {
    perror("fcntl F_SETLK on the first file failed");
    goto exit;
  }
  memset(&lock, 0, sizeof(lock));
  lock.l_type = F_WRLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  if (fcntl(fd2, F_SETLK, &lock) == -1) {
    if (errno == EACCES || errno == EAGAIN) {
      result = 0;
      goto exit;
    } else {
      perror("fcntl F_SETLK on the second file failed");
      goto exit;
    }
  } else {
    result = 1;
    goto exit;
  }
exit:
  close(fd1);
  close(fd2);
  return result;
}


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   $0x3,-0x8(%rbp)
je     11a1 <main+0x41>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1351 <main+0x1f1>
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ea <main+0x8a>
lea    0xe41(%rip),%rdi
call   1080 <perror@plt>
jmp    133b <main+0x1db>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    121e <main+0xbe>
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
jmp    133b <main+0x1db>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1289 <main+0x129>
lea    0xddf(%rip),%rdi
call   1080 <perror@plt>
jmp    133b <main+0x1db>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1334 <main+0x1d4>
mov    0x2cd6(%rip),%rax
mov    (%rax),%rax
mov    0x2ce4(%rip),%rcx
cmp    (%rcx),%rax
je     1317 <main+0x1b7>
mov    0x2cbc(%rip),%rax
mov    (%rax),%rax
mov    0x2c92(%rip),%rcx
cmp    (%rcx),%rax
jne    1323 <main+0x1c3>
movl   $0x0,-0x1c(%rbp)
jmp    133b <main+0x1db>
lea    0xd5b(%rip),%rdi
call   1080 <perror@plt>
jmp    133b <main+0x1db>
movl   $0x1,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%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    $0x3,%edi
jne    126f <main+0x10f>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e63(%rip),%rbp
mov    0x0(%rbp),%esi
call   1070 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     128b <main+0x12b>
mov    0x10(%rbx),%rdi
mov    0x0(%rbp),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1294 <main+0x134>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2de7(%rip),%r15
mov    (%r15),%rax
mov    %rax,0x18(%rsp)
mov    0x2de0(%rip),%r12
mov    (%r12),%rax
mov    %rax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2df1(%rip),%r13
mov    0x0(%r13),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12ac <main+0x14c>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    (%r15),%rax
mov    %rax,0x18(%rsp)
mov    (%r12),%rax
mov    %rax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <fcntl@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
jne    12c6 <main+0x166>
mov    0x2d70(%rip),%rax
mov    (%rax),%rax
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%rax
je     126b <main+0x10b>
mov    0x2d3a(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <main+0x155>
xor    %ebx,%ebx
jmp    12c6 <main+0x166>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%ebx
jmp    12d5 <main+0x175>
lea    0xd9f(%rip),%rdi
jmp    129b <main+0x13b>
lea    0xdb4(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%ebx
mov    $0xffffffff,%ebp
jmp    12c6 <main+0x166>
lea    0xdbb(%rip),%rdi
jmp    12bc <main+0x15c>
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%ebx
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%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, i8*, i8* }
@stderr = global i32 0
@.str = [33 x i8] c"expecting exactly two arguments\0A\00"
@O_RDWR = global i32 0
@.str.1 = [30 x i8] c"failed to open the first file\00"
@.str.2 = [31 x i8] c"failed to open the second file\00"
@F_WRLCK = global i8* null
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.3 = [39 x i8] c"fcntl F_SETLK on the first file failed\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.4 = [40 x i8] c"fcntl F_SETLK on the second file failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.flock
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, 3
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %3
br label %79
15: ; preds = %2
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 2, i32* %8
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %73
25: ; preds = %15
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDWR
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %73
35: ; preds = %25
%36 = call i32 @memset(%struct.flock* %9, i32 0, i32 32)
%37 = load i8*, i8** @F_WRLCK
%38 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 3
store i8* %37, i8** %38
%39 = load i8*, i8** @SEEK_SET
%40 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i8* %39, i8** %40
%41 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
store i64 0, i64* %41
%42 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i64 0, i64* %42
%43 = load i32, i32* %6
%44 = load i32, i32* @F_SETLK
%45 = call i32 @fcntl(i32 %43, i32 %44, %struct.flock* %9)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %35
%48 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %73
49: ; preds = %35
%50 = call i32 @memset(%struct.flock* %9, i32 0, i32 32)
%51 = load i8*, i8** @F_WRLCK
%52 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 3
store i8* %51, i8** %52
%53 = load i8*, i8** @SEEK_SET
%54 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 2
store i8* %53, i8** %54
%55 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 1
store i64 0, i64* %55
%56 = getelementptr inbounds %struct.flock, %struct.flock* %9, i32 0, i32 0
store i64 0, i64* %56
%57 = load i32, i32* %7
%58 = load i32, i32* @F_SETLK
%59 = call i32 @fcntl(i32 %57, i32 %58, %struct.flock* %9)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %72
61: ; preds = %49
%62 = load i64, i64* @errno
%63 = load i64, i64* @EACCES
%64 = icmp eq i64 %62, %63
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = load i64, i64* @errno
%67 = load i64, i64* @EAGAIN
%68 = icmp eq i64 %66, %67
br i1 %68, label %69, label %70
69: ; preds = %65, %61
store i32 0, i32* %8
br label %73
70: ; preds = %65
%71 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %73
72: ; preds = %49
store i32 1, i32* %8
br label %73
73: ; preds = %72, %70, %69, %47, %33, %23
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %7
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %8
store i32 %78, i32* %3
br label %79
79: ; preds = %73, %12
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i8*, i8* }
@stderr = global i32 0
@.str = [33 x i8] c"expecting exactly two arguments\0A\00"
@O_RDWR = global i32 0
@.str.1 = [30 x i8] c"failed to open the first file\00"
@.str.2 = [31 x i8] c"failed to open the second file\00"
@F_WRLCK = global i8* null
@SEEK_SET = global i8* null
@F_SETLK = global i32 0
@.str.3 = [39 x i8] c"fcntl F_SETLK on the first file failed\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.4 = [40 x i8] c"fcntl F_SETLK on the second file failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.flock
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %58
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %53
16: ; preds = %8
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %53
24: ; preds = %16
%25 = call i32 @memset(%struct.flock* %3, i32 0, i32 32)
%26 = load i8*, i8** @F_WRLCK
%27 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
store i8* %26, i8** %27
%28 = load i8*, i8** @SEEK_SET
%29 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
store i8* %28, i8** %29
%30 = bitcast %struct.flock* %3 to i8*
%31 = load i32, i32* @F_SETLK
%32 = call i32 @fcntl(i32 %12, i32 %31, %struct.flock* %3)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %53
36: ; preds = %24
%37 = call i32 @memset(%struct.flock* %3, i32 0, i32 32)
%38 = load i8*, i8** @F_WRLCK
store i8* %38, i8** %27
%39 = load i8*, i8** @SEEK_SET
store i8* %39, i8** %29
%40 = bitcast %struct.flock* %3 to i8*
%41 = load i32, i32* @F_SETLK
%42 = call i32 @fcntl(i32 %20, i32 %41, %struct.flock* %3)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %53
44: ; preds = %36
%45 = load i64, i64* @errno
%46 = load i64, i64* @EACCES
%47 = icmp eq i64 %45, %46
%48 = load i64, i64* @EAGAIN
%49 = icmp eq i64 %45, %48
%50 = select i1 %47, i1 true, i1 %49
br i1 %50, label %53, label %51
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %36, %44, %51, %34, %22, %14
%54 = phi i32 [ -1, %14 ], [ -1, %22 ], [ %20, %34 ], [ %20, %51 ], [ %20, %44 ], [ %20, %36 ]
%55 = phi i32 [ 2, %14 ], [ 2, %22 ], [ 2, %34 ], [ 2, %51 ], [ 0, %44 ], [ 1, %36 ]
%56 = call i32 @close(i32 %12)
%57 = call i32 @close(i32 %54)
br label %58
58: ; preds = %53, %5
%59 = phi i32 [ 2, %5 ], [ %55, %53 ]
ret i32 %59
}