#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 pid_t;

/* Variables and functions */
scalar_t__ EACCES;
int atoi(char *);
int /*<<< orphan*/ checkInput(int, char **);
int /*<<< orphan*/ close(int);
int creat(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ execlp(char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ kill(int, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ unlink(char *);
int wait(int *);
int main(int argc, char *argv[]) {
  checkInput(argc, argv);
  char *fname;
  char *lockFileName = "lock";
  int childNumber = atoi(argv[4]);
  int fd, sleeptime, n_try, count = 0;
  pid_t pid;
  pid = getpid();
  srand((unsigned)pid);
  fname = argv[1];
  sleeptime = atoi(argv[2]);
  n_try = atoi(argv[3]);
  while ((fd = creat(lockFileName, 0)) == -1 && errno == EACCES) {
    if (++count < n_try) {
      sleep(rand() % sleeptime);
    } else {
      printf("\n Unable to obtain lock file\n");
      kill(pid, childNumber);
    }
  }
  close(fd);
  pid_t catStatus;
  if ((catStatus = fork()) == 0) {
    execlp("/bin/cat", "cat", fname, (char *)NULL);
    printf("\nExec failure. Could not exec /bin/cat in child #%d\n",
           childNumber);
    exit(1);
  } else if (catStatus == -1)
    printf("\nUnable to fork cat, unable to read the file.\n");
  pid_t waitStatus;
  int status;
  while ((waitStatus = wait(&status)) && waitStatus != -1)
    while (unlink(lockFileName) != 0) {
      if (++count < n_try)
        sleep(sleeptime);
      else {
        printf("\n Cannot release lock\n");
        kill(pid, childNumber);
      }
    }
  int exitValue = (int)(pid % 256);
  exit(exitValue);
}


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    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1090 <checkInput@plt>
lea    0xde7(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   1070 <srand@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   10b0 <creat@plt>
mov    %eax,%ecx
mov    %ecx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x49(%rbp)
jne    12aa <main+0xba>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    12ba <main+0xca>
jmp    1301 <main+0x111>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmp    -0x30(%rbp),%eax
jge    12e3 <main+0xf3>
mov    $0x0,%al
call   1110 <rand@plt>
cltd
idivl  -0x2c(%rbp)
mov    %edx,%edi
call   10e0 <sleep@plt>
jmp    12fc <main+0x10c>
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1080 <kill@plt>
jmp    1272 <main+0x82>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jne    1352 <main+0x162>
mov    -0x18(%rbp),%rdx
lea    0xcfc(%rip),%rdi
lea    0xcfa(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   10d0 <execlp@plt>
mov    -0x24(%rbp),%esi
lea    0xceb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0xffffffff,-0x3c(%rbp)
jne    136a <main+0x17a>
lea    0xcfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    136f <main+0x17f>
jmp    1374 <main+0x184>
lea    -0x44(%rbp),%rdi
call   10f0 <wait@plt>
mov    %eax,%ecx
mov    %ecx,-0x40(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x4a(%rbp)
je     139a <main+0x1aa>
cmpl   $0xffffffff,-0x40(%rbp)
setne  %al
mov    %al,-0x4a(%rbp)
mov    -0x4a(%rbp),%al
test   $0x1,%al
jne    13aa <main+0x1ba>
jmp    1404 <main+0x214>
jmp    13af <main+0x1bf>
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     13ff <main+0x20f>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
cmp    -0x30(%rbp),%eax
jge    13e1 <main+0x1f1>
mov    -0x2c(%rbp),%edi
call   10e0 <sleep@plt>
jmp    13fa <main+0x20a>
lea    0xca8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1080 <kill@plt>
jmp    13af <main+0x1bf>
jmp    1374 <main+0x184>
mov    -0x38(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %edx,-0x48(%rbp)
mov    -0x48(%rbp),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
call   10a0 <checkInput@plt>
mov    0x20(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,0x8(%rsp)
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1080 <srand@plt>
mov    0x8(%rbx),%rax
mov    %rax,0x10(%rsp)
mov    0x10(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r13d
mov    0x18(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
lea    0xdf1(%rip),%rdi
xor    %esi,%esi
call   10c0 <creat@plt>
cmp    $0xffffffff,%eax
jne    12e7 <main+0xe7>
mov    0x2d67(%rip),%rdx
mov    0x2d70(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,(%rdx)
jne    12e7 <main+0xe7>
mov    %rdx,%r12
lea    0xdc5(%rip),%rbp
xor    %r14d,%r14d
nopl   (%rax)
add    $0x1,%r14d
cmp    %ebx,%r14d
jge    12b0 <main+0xb0>
xor    %eax,%eax
call   1120 <rand@plt>
cltd
idiv   %r13d
mov    %edx,%edi
call   10f0 <sleep@plt>
jmp    12c8 <main+0xc8>
nopl   (%rax)
lea    0xdcb(%rip),%rdi
call   1040 <puts@plt>
mov    %r15d,%edi
mov    0x8(%rsp),%esi
call   1090 <kill@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   10c0 <creat@plt>
cmp    $0xffffffff,%eax
jne    12e7 <main+0xe7>
mov    0x2d0a(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,(%r12)
je     1290 <main+0x90>
mov    %eax,%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1110 <fork@plt>
cmp    $0xffffffff,%eax
je     1334 <main+0x134>
test   %eax,%eax
jne    1340 <main+0x140>
lea    0xcfb(%rip),%rdi
lea    0xcf9(%rip),%rsi
mov    0x10(%rsp),%rdx
xor    %ecx,%ecx
call   10e0 <execlp@plt>
lea    0xcea(%rip),%rdi
mov    0x8(%rsp),%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd19(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rdi
call   1100 <wait@plt>
add    $0xffffffff,%eax
cmp    $0xfffffffd,%eax
ja     13b6 <main+0x1b6>
lea    0xcf6(%rip),%rbp
lea    0xcde(%rip),%r12
jmp    1384 <main+0x184>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %r15d,%edi
mov    0x8(%rsp),%esi
call   1090 <kill@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     13a4 <main+0x1a4>
add    $0x1,%r14d
cmp    %ebx,%r14d
jge    1370 <main+0x170>
mov    %r13d,%edi
call   10f0 <sleep@plt>
jmp    1384 <main+0x184>
lea    0xc(%rsp),%rdi
call   1100 <wait@plt>
add    $0xffffffff,%eax
cmp    $0xfffffffd,%eax
jbe    1384 <main+0x184>
lea    0xff(%r15),%eax
test   %r15d,%r15d
cmovns %r15d,%eax
and    $0xffffff00,%eax
sub    %eax,%r15d
mov    %r15d,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"lock\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.1 = [30 x i8] c"\0A Unable to obtain lock file\0A\00"
@.str.2 = [9 x i8] c"/bin/cat\00"
@.str.3 = [4 x i8] c"cat\00"
@.str.4 = [53 x i8] c"\0AExec failure. Could not exec /bin/cat in child #%d\0A\00"
@.str.5 = [47 x i8] c"\0AUnable to fork cat, unable to read the file.\0A\00"
@.str.6 = [23 x i8] c"\0A Cannot release lock\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = load i8**, i8*** %5
%20 = call i32 @checkInput(i32 %18, i8** %19)
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %7
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 4
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %8
store i32 0, i32* %12
%25 = call i32 (...) @getpid()
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = call i32 @srand(i32 %26)
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %6
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %10
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 3
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
store i32 %38, i32* %11
br label %39
39: ; preds = %64, %2
%40 = load i8*, i8** %7
%41 = call i32 @creat(i8* %40, i32 0)
store i32 %41, i32* %9
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i64, i64* @errno
%45 = load i64, i64* @EACCES
%46 = icmp eq i64 %44, %45
br label %47
47: ; preds = %43, %39
%48 = phi i1 [ false, %39 ], [ %46, %43 ]
br i1 %48, label %49, label %65
49: ; preds = %47
%50 = load i32, i32* %12
%51 = add nsw i32 %50, 1
store i32 %51, i32* %12
%52 = load i32, i32* %11
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %59
54: ; preds = %49
%55 = call i32 (...) @rand()
%56 = load i32, i32* %10
%57 = srem i32 %55, %56
%58 = call i32 @sleep(i32 %57)
br label %64
59: ; preds = %49
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%61 = load i32, i32* %13
%62 = load i32, i32* %8
%63 = call i32 @kill(i32 %61, i32 %62)
br label %64
64: ; preds = %59, %54
br label %39
65: ; preds = %47
%66 = load i32, i32* %9
%67 = call i32 @close(i32 %66)
%68 = call i32 (...) @fork()
store i32 %68, i32* %14
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %76
70: ; preds = %65
%71 = load i8*, i8** %6
%72 = call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %71, i8* null)
%73 = load i32, i32* %8
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %65
%77 = load i32, i32* %14
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0))
br label %81
81: ; preds = %79, %76
br label %82
82: ; preds = %81
br label %83
83: ; preds = %110, %82
%84 = call i32 @wait(i32* %16)
store i32 %84, i32* %15
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = load i32, i32* %15
%88 = icmp ne i32 %87, -1
br label %89
89: ; preds = %86, %83
%90 = phi i1 [ false, %83 ], [ %88, %86 ]
br i1 %90, label %91, label %111
91: ; preds = %89
br label %92
92: ; preds = %109, %91
%93 = load i8*, i8** %7
%94 = call i64 @unlink(i8* %93)
%95 = icmp ne i64 %94, 0
br i1 %95, label %96, label %110
96: ; preds = %92
%97 = load i32, i32* %12
%98 = add nsw i32 %97, 1
store i32 %98, i32* %12
%99 = load i32, i32* %11
%100 = icmp slt i32 %98, %99
br i1 %100, label %101, label %104
101: ; preds = %96
%102 = load i32, i32* %10
%103 = call i32 @sleep(i32 %102)
br label %109
104: ; preds = %96
%105 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%106 = load i32, i32* %13
%107 = load i32, i32* %8
%108 = call i32 @kill(i32 %106, i32 %107)
br label %109
109: ; preds = %104, %101
br label %92
110: ; preds = %92
br label %83
111: ; preds = %89
%112 = load i32, i32* %13
%113 = srem i32 %112, 256
store i32 %113, i32* %17
%114 = load i32, i32* %17
%115 = call i32 @exit(i32 %114)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"lock\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.2 = [9 x i8] c"/bin/cat\00"
@.str.3 = [4 x i8] c"cat\00"
@.str.4 = [53 x i8] c"\0AExec failure. Could not exec /bin/cat in child #%d\0A\00"
@str = [22 x i8] c"\0A Cannot release lock\00"
@str.7 = [46 x i8] c"\0AUnable to fork cat, unable to read the file.\00"
@str.8 = [29 x i8] c"\0A Unable to obtain lock file\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = tail call i32 @checkInput(i32 %0, i8** %1)
%5 = getelementptr inbounds i8*, i8** %1, i64 4
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 @srand(i32 %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = tail call i32 @creat(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
%19 = icmp eq i32 %18, -1
%20 = load i64, i64* @errno
%21 = load i64, i64* @EACCES
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %24, label %42
24: ; preds = %2, %35
%25 = phi i32 [ %26, %35 ], [ 0, %2 ]
%26 = add nuw nsw i32 %25, 1
%27 = icmp slt i32 %26, %17
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = tail call i32 (...) @rand()
%30 = srem i32 %29, %14
%31 = tail call i32 @sleep(i32 %30)
br label %35
32: ; preds = %24
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.8, i64 0, i64 0))
%34 = tail call i32 @kill(i32 %8, i32 %7)
br label %35
35: ; preds = %32, %28
%36 = tail call i32 @creat(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 0)
%37 = icmp eq i32 %36, -1
%38 = load i64, i64* @errno
%39 = load i64, i64* @EACCES
%40 = icmp eq i64 %38, %39
%41 = select i1 %37, i1 %40, i1 false
br i1 %41, label %24, label %42
42: ; preds = %35, %2
%43 = phi i32 [ 0, %2 ], [ %26, %35 ]
%44 = phi i32 [ %18, %2 ], [ %36, %35 ]
%45 = tail call i32 @close(i32 %44)
%46 = tail call i32 (...) @fork()
switch i32 %46, label %53 [
i32 0, label %47
i32 -1, label %51
]
47: ; preds = %42
%48 = tail call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %11, i8* null)
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %7)
%50 = tail call i32 @exit(i32 1)
unreachable
51: ; preds = %42
%52 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.7, i64 0, i64 0))
br label %53
53: ; preds = %42, %51
%54 = bitcast i32* %3 to i8*
%55 = call i32 @wait(i32* %3)
%56 = add i32 %55, -1
%57 = icmp ult i32 %56, -2
br i1 %57, label %63, label %79
58: ; preds = %76, %63
%59 = phi i32 [ %64, %63 ], [ %69, %76 ]
%60 = call i32 @wait(i32* %3)
%61 = add i32 %60, -1
%62 = icmp ult i32 %61, -2
br i1 %62, label %63, label %79
63: ; preds = %53, %58
%64 = phi i32 [ %59, %58 ], [ %43, %53 ]
%65 = call i64 @unlink(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%66 = icmp eq i64 %65, 0
br i1 %66, label %58, label %67
67: ; preds = %63, %76
%68 = phi i32 [ %69, %76 ], [ %64, %63 ]
%69 = add nsw i32 %68, 1
%70 = icmp slt i32 %69, %17
br i1 %70, label %71, label %73
71: ; preds = %67
%72 = call i32 @sleep(i32 %14)
br label %76
73: ; preds = %67
%74 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%75 = call i32 @kill(i32 %8, i32 %7)
br label %76
76: ; preds = %73, %71
%77 = call i64 @unlink(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%78 = icmp eq i64 %77, 0
br i1 %78, label %58, label %67
79: ; preds = %58, %53
%80 = srem i32 %8, 256
%81 = call i32 @exit(i32 %80)
unreachable
}