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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ jd_sem_down(int, int);
int /*<<< orphan*/ jd_sem_lidl(int, int, int);
int /*<<< orphan*/ jd_sem_up(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ usleep(int);
void wyplata(int *value, int fd, int semid, int brak_kasy, int mutex, int count,
             int count_i) {
  int j, i, pid, trans;
  for (j = 0; j < count; j++) {
    switch (pid = fork()) {
    case -1:
      perror("fork error");
      exit(1);
    case 0:
      for (i = 0; i < count_i; i++) {
        while (1) {
          jd_sem_up(semid, mutex);
          printf("wyplata -100 %d\n", *value);
          trans = 100;
          if (*value - 100 >= 0) {
            (*value) -= trans;
            usleep(10000);
            break;
          } else {
            jd_sem_lidl(semid, mutex, brak_kasy);
          }
        }
        jd_sem_down(semid, mutex);
        usleep(30000);
      }
      close(fd);
      exit(0);
    }
  }
}


x86 assembly (clang, -O0):
<wyplata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12c8 <wyplata+0x138>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x28(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
sub    $0xffffffff,%eax
je     11f0 <wyplata+0x60>
jmp    11e0 <wyplata+0x50>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     1206 <wyplata+0x76>
jmp    12b5 <wyplata+0x125>
lea    0xe09(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    12a6 <wyplata+0x116>
jmp    121e <wyplata+0x8e>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1070 <jd_sem_up@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xdd5(%rip),%rdi
call   1040 <printf@plt>
movl   $0x64,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
sub    $0x64,%eax
cmp    $0x0,%eax
jl     1270 <wyplata+0xe0>
mov    -0x2c(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
mov    $0x2710,%edi
call   10b0 <usleep@plt>
jmp    1283 <wyplata+0xf3>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1060 <jd_sem_lidl@plt>
jmp    121e <wyplata+0x8e>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <jd_sem_down@plt>
mov    $0x7530,%edi
call   10b0 <usleep@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    120d <wyplata+0x7d>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    12ba <wyplata+0x12a>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11b7 <wyplata+0x27>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wyplata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %r9d,%r9d
jle    11d6 <wyplata+0x46>
mov    %r9d,%r13d
mov    %r8d,%r12d
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11e5 <wyplata+0x55>
test   %eax,%eax
je     11fb <wyplata+0x6b>
add    $0xffffffff,%r13d
jne    11c0 <wyplata+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %r14d,0x4(%rsp)
cmpl   $0x0,0x40(%rsp)
jle    1276 <wyplata+0xe6>
xor    %r14d,%r14d
lea    0xdfa(%rip),%r13
jmp    122d <wyplata+0x9d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r12d,%esi
mov    %r15d,%edx
call   1060 <jd_sem_lidl@plt>
mov    %ebp,%edi
mov    %r12d,%esi
call   1070 <jd_sem_up@plt>
mov    (%rbx),%esi
mov    %r13,%rdi
call   1040 <printf@plt>
mov    (%rbx),%eax
cmp    $0x63,%eax
jle    1220 <wyplata+0x90>
add    $0xffffff9c,%eax
mov    %eax,(%rbx)
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    %ebp,%edi
mov    %r12d,%esi
call   1030 <jd_sem_down@plt>
mov    $0x7530,%edi
call   10b0 <usleep@plt>
add    $0x1,%r14d
cmp    0x40(%rsp),%r14d
jne    122d <wyplata+0x9d>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [17 x i8] c"wyplata -100 %d\0A\00"
define void @wyplata(i32* %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6) {
%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
%18 = alloca i32
store i32* %0, i32** %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 %6, i32* %14
store i32 0, i32* %15
br label %19
19: ; preds = %70, %7
%20 = load i32, i32* %15
%21 = load i32, i32* %13
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %73
23: ; preds = %19
%24 = call i32 (...) @fork()
store i32 %24, i32* %17
switch i32 %24, label %69 [
i32 -1, label %25
i32 0, label %28
]
25: ; preds = %23
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %23
store i32 0, i32* %16
br label %29
29: ; preds = %62, %28
%30 = load i32, i32* %16
%31 = load i32, i32* %14
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %65
33: ; preds = %29
br label %34
34: ; preds = %33, %56
%35 = load i32, i32* %10
%36 = load i32, i32* %12
%37 = call i32 @jd_sem_up(i32 %35, i32 %36)
%38 = load i32*, i32** %8
%39 = load i32, i32* %38
%40 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %39)
store i32 100, i32* %18
%41 = load i32*, i32** %8
%42 = load i32, i32* %41
%43 = sub nsw i32 %42, 100
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %34
%46 = load i32, i32* %18
%47 = load i32*, i32** %8
%48 = load i32, i32* %47
%49 = sub nsw i32 %48, %46
store i32 %49, i32* %47
%50 = call i32 @usleep(i32 10000)
br label %57
51: ; preds = %34
%52 = load i32, i32* %10
%53 = load i32, i32* %12
%54 = load i32, i32* %11
%55 = call i32 @jd_sem_lidl(i32 %52, i32 %53, i32 %54)
br label %56
56: ; preds = %51
br label %34
57: ; preds = %45
%58 = load i32, i32* %10
%59 = load i32, i32* %12
%60 = call i32 @jd_sem_down(i32 %58, i32 %59)
%61 = call i32 @usleep(i32 30000)
br label %62
62: ; preds = %57
%63 = load i32, i32* %16
%64 = add nsw i32 %63, 1
store i32 %64, i32* %16
br label %29
65: ; preds = %29
%66 = load i32, i32* %9
%67 = call i32 @close(i32 %66)
%68 = call i32 @exit(i32 0)
unreachable
69: ; preds = %23
br label %70
70: ; preds = %69
%71 = load i32, i32* %15
%72 = add nsw i32 %71, 1
store i32 %72, i32* %15
br label %19
73: ; preds = %19
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [17 x i8] c"wyplata -100 %d\0A\00"
define void @wyplata(i32* nocapture %0, i32 %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 %6) {
%8 = icmp sgt i32 %5, 0
br i1 %8, label %9, label %45
9: ; preds = %7, %42
%10 = phi i32 [ %43, %42 ], [ 0, %7 ]
%11 = tail call i32 (...) @fork()
switch i32 %11, label %42 [
i32 -1, label %14
i32 0, label %12
]
12: ; preds = %9
%13 = icmp sgt i32 %6, 0
br i1 %13, label %17, label %39
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %12, %24
%18 = phi i32 [ %30, %24 ], [ 0, %12 ]
%19 = tail call i32 @jd_sem_up(i32 %2, i32 %4)
%20 = load i32, i32* %0
%21 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %0
%23 = icmp sgt i32 %22, 99
br i1 %23, label %24, label %32
24: ; preds = %32, %17
%25 = phi i32 [ %22, %17 ], [ %37, %32 ]
%26 = add nsw i32 %25, -100
store i32 %26, i32* %0
%27 = tail call i32 @usleep(i32 10000)
%28 = tail call i32 @jd_sem_down(i32 %2, i32 %4)
%29 = tail call i32 @usleep(i32 30000)
%30 = add nuw nsw i32 %18, 1
%31 = icmp eq i32 %30, %6
br i1 %31, label %39, label %17
32: ; preds = %17, %32
%33 = tail call i32 @jd_sem_lidl(i32 %2, i32 %4, i32 %3)
%34 = tail call i32 @jd_sem_up(i32 %2, i32 %4)
%35 = load i32, i32* %0
%36 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %0
%38 = icmp sgt i32 %37, 99
br i1 %38, label %24, label %32
39: ; preds = %24, %12
%40 = tail call i32 @close(i32 %1)
%41 = tail call i32 @exit(i32 0)
unreachable
42: ; preds = %9
%43 = add nuw nsw i32 %10, 1
%44 = icmp eq i32 %43, %5
br i1 %44, label %45, label %9
45: ; preds = %42, %7
ret void
}