#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*/ FILE;

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int LOCK_UN;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ flock(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  FILE *batrak;
  int zoloto;
  batrak = fopen("/home/arthur/labs/Eltexlabs/CLabs/gold.txt", "r");
  fscanf(batrak, "%d", &zoloto);
  fclose(batrak);
  while (zoloto > 0) {
    if (flock(batrak, LOCK_EX | LOCK_NB)) {
      batrak = fopen("/home/arthur/labs/Eltexlabs/CLabs/gold.txt", "r");
      fscanf(batrak, "%d", &zoloto);
      zoloto = zoloto - 5;
      if (zoloto >= 0) {
        batrak = fopen("/home/arthur/labs/Eltexlabs/CLabs/gold.txt", "w+");
        fprintf(batrak, "%d", zoloto);
        fclose(batrak);
        printf("Раб под номером: %d отправился в шахту, Золото в руднике после "
               "работы = %d\n",
               getpid(), zoloto);
        sleep(2);
      }
      if (zoloto == 0) {
        printf("Раб под номером: %d отправился в шахту, Рудник обрушился\n",
               getpid());
        exit(0);
      }
    }
    flock(batrak, LOCK_UN);
    sleep(2);
  }
  return (zoloto);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe65(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    12ec <main+0x15c>
mov    -0x18(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
call   1090 <flock@plt>
cmp    $0x0,%rax
je     12cb <main+0x13b>
lea    0xdf0(%rip),%rdi
lea    0xe14(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe02(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x1c(%rbp),%eax
sub    $0x5,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     129e <main+0x10e>
lea    0xdb2(%rip),%rdi
lea    0xddb(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xdc1(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x1c(%rbp),%edx
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12c6 <main+0x136>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    12cb <main+0x13b>
mov    -0x18(%rbp),%rdi
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
jmp    11da <main+0x4a>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe5e(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
lea    0xe75(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%eax
test   %eax,%eax
jle    12de <main+0x14e>
mov    0x2e08(%rip),%rbp
mov    0x2de1(%rip),%r15
mov    0x2dea(%rip),%r13
lea    0xe0b(%rip),%r14
lea    0xe31(%rip),%r12
jmp    1222 <main+0x92>
xchg   %ax,%ax
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1090 <flock@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
mov    0x4(%rsp),%eax
test   %eax,%eax
jle    12de <main+0x14e>
mov    (%r15),%esi
or     0x0(%rbp),%esi
mov    %rbx,%rdi
call   1090 <flock@plt>
test   %rax,%rax
je     1200 <main+0x70>
mov    %r14,%rdi
lea    0xdec(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
lea    0x4(%rsp),%rdx
call   1060 <fscanf@plt>
mov    0x4(%rsp),%eax
lea    -0x5(%rax),%ecx
mov    %ecx,0x4(%rsp)
cmp    $0x5,%eax
jl     1200 <main+0x70>
mov    %r14,%rdi
lea    0xdbf(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    0x4(%rsp),%edx
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x4(%rsp),%edx
lea    0xd91(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%edi
call   10b0 <sleep@plt>
cmpl   $0x0,0x4(%rsp)
jne    1200 <main+0x70>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xde7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"/home/arthur/labs/Eltexlabs/CLabs/gold.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [130 x i8] c"\D0\A0\D0\B0\D0\B1 \D0\BF\D0\BE\D0\B4 \D0\BD\D0\BE\D0\BC\D0\B5\D1\80\D0\BE\D0\BC: %d \D0\BE\D1\82\D0\BF\D1\80\D0\B0\D0\B2\D0\B8\D0\BB\D1\81\D1\8F \D0\B2 \D1\88\D0\B0\D1\85\D1\82\D1\83, \D0\97\D0\BE\D0\BB\D0\BE\D1\82\D0\BE \D0\B2 \D1\80\D1\83\D0\B4\D0\BD\D0\B8\D0\BA\D0\B5 \D0\BF\D0\BE\D1\81\D0\BB\D0\B5 \D1\80\D0\B0\D0\B1\D0\BE\D1\82\D1\8B = %d\0A\00"
@.str.5 = [102 x i8] c"\D0\A0\D0\B0\D0\B1 \D0\BF\D0\BE\D0\B4 \D0\BD\D0\BE\D0\BC\D0\B5\D1\80\D0\BE\D0\BC: %d \D0\BE\D1\82\D0\BF\D1\80\D0\B0\D0\B2\D0\B8\D0\BB\D1\81\D1\8F \D0\B2 \D1\88\D0\B0\D1\85\D1\82\D1\83, \D0\A0\D1\83\D0\B4\D0\BD\D0\B8\D0\BA \D0\BE\D0\B1\D1\80\D1\83\D1\88\D0\B8\D0\BB\D1\81\D1\8F\0A\00"
@LOCK_UN = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32* @fopen(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%11 = load i32*, i32** %6
%12 = call i32 @fclose(i32* %11)
br label %13
13: ; preds = %50, %2
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %55
16: ; preds = %13
%17 = load i32*, i32** %6
%18 = load i32, i32* @LOCK_EX
%19 = load i32, i32* @LOCK_NB
%20 = or i32 %18, %19
%21 = call i64 @flock(i32* %17, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %50
23: ; preds = %16
%24 = call i32* @fopen(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %6
%25 = load i32*, i32** %6
%26 = call i32 @fscanf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%27 = load i32, i32* %7
%28 = sub nsw i32 %27, 5
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %23
%32 = call i32* @fopen(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %32, i32** %6
%33 = load i32*, i32** %6
%34 = load i32, i32* %7
%35 = call i32 @fprintf(i32* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = load i32*, i32** %6
%37 = call i32 @fclose(i32* %36)
%38 = call i32 (...) @getpid()
%39 = load i32, i32* %7
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([130 x i8], [130 x i8]* @.str.4, i64 0, i64 0), i32 %38, i32 %39)
%41 = call i32 @sleep(i32 2)
br label %42
42: ; preds = %31, %23
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = call i32 (...) @getpid()
%47 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.5, i64 0, i64 0), i32 %46)
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %42
br label %50
50: ; preds = %49, %16
%51 = load i32*, i32** %6
%52 = load i32, i32* @LOCK_UN
%53 = call i64 @flock(i32* %51, i32 %52)
%54 = call i32 @sleep(i32 2)
br label %13
55: ; preds = %13
%56 = load i32, i32* %7
ret i32 %56
}

LLVM IR (-O2):
@.str = [43 x i8] c"/home/arthur/labs/Eltexlabs/CLabs/gold.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.3 = [3 x i8] c"w+\00"
@.str.4 = [130 x i8] c"\D0\A0\D0\B0\D0\B1 \D0\BF\D0\BE\D0\B4 \D0\BD\D0\BE\D0\BC\D0\B5\D1\80\D0\BE\D0\BC: %d \D0\BE\D1\82\D0\BF\D1\80\D0\B0\D0\B2\D0\B8\D0\BB\D1\81\D1\8F \D0\B2 \D1\88\D0\B0\D1\85\D1\82\D1\83, \D0\97\D0\BE\D0\BB\D0\BE\D1\82\D0\BE \D0\B2 \D1\80\D1\83\D0\B4\D0\BD\D0\B8\D0\BA\D0\B5 \D0\BF\D0\BE\D1\81\D0\BB\D0\B5 \D1\80\D0\B0\D0\B1\D0\BE\D1\82\D1\8B = %d\0A\00"
@.str.5 = [102 x i8] c"\D0\A0\D0\B0\D0\B1 \D0\BF\D0\BE\D0\B4 \D0\BD\D0\BE\D0\BC\D0\B5\D1\80\D0\BE\D0\BC: %d \D0\BE\D1\82\D0\BF\D1\80\D0\B0\D0\B2\D0\B8\D0\BB\D1\81\D1\8F \D0\B2 \D1\88\D0\B0\D1\85\D1\82\D1\83, \D0\A0\D1\83\D0\B4\D0\BD\D0\B8\D0\BA \D0\BE\D0\B1\D1\80\D1\83\D1\88\D0\B8\D0\BB\D1\81\D1\8F\0A\00"
@LOCK_UN = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%7 = call i32 @fclose(i32* %5)
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %45
10: ; preds = %2, %38
%11 = phi i32* [ %39, %38 ], [ %5, %2 ]
%12 = load i32, i32* @LOCK_EX
%13 = load i32, i32* @LOCK_NB
%14 = or i32 %13, %12
%15 = call i64 @flock(i32* %11, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %38, label %17
17: ; preds = %10
%18 = call i32* @fopen(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%20 = load i32, i32* %3
%21 = add nsw i32 %20, -5
store i32 %21, i32* %3
%22 = icmp sgt i32 %20, 4
br i1 %22, label %23, label %38
23: ; preds = %17
%24 = call i32* @fopen(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* %3
%26 = call i32 @fprintf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @fclose(i32* %24)
%28 = call i32 (...) @getpid()
%29 = load i32, i32* %3
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([130 x i8], [130 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %29)
%31 = call i32 @sleep(i32 2)
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %23
%35 = call i32 (...) @getpid()
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([102 x i8], [102 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %17, %23, %10
%39 = phi i32* [ %24, %23 ], [ %11, %10 ], [ %18, %17 ]
%40 = load i32, i32* @LOCK_UN
%41 = call i64 @flock(i32* %39, i32 %40)
%42 = call i32 @sleep(i32 2)
%43 = load i32, i32* %3
%44 = icmp sgt i32 %43, 0
br i1 %44, label %10, label %45
45: ; preds = %38, %2
%46 = phi i32 [ %8, %2 ], [ %43, %38 ]
ret i32 %46
}