#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_down2(int, int, int);
int /*<<< orphan*/ jd_sem_up(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ usleep(int);
void wplata(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++) {
        jd_sem_up(semid, mutex);
        printf("wplata %d\n", *value);
        trans = 100;
        (*value) += trans;
        usleep(10000);
        jd_sem_down2(semid, mutex, brak_kasy);
        usleep(30000);
      }
      close(fd);
      exit(0);
    }
  }
}


x86 assembly (clang, -O0):
<wplata>:
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    128a <wplata+0x10a>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x28(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
sub    $0xffffffff,%eax
je     11e0 <wplata+0x60>
jmp    11d0 <wplata+0x50>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     11f6 <wplata+0x76>
jmp    1277 <wplata+0xf7>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    1268 <wplata+0xe8>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <jd_sem_up@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
movl   $0x64,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x8(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    $0x2710,%edi
call   10a0 <usleep@plt>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <jd_sem_down2@plt>
mov    $0x7530,%edi
call   10a0 <usleep@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11fd <wplata+0x7d>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    127c <wplata+0xfc>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a7 <wplata+0x27>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wplata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,0x4(%rsp)
test   %r9d,%r9d
jle    11c5 <wplata+0x45>
mov    %r9d,%ebp
mov    %r8d,%r12d
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rdi,%rbx
mov    0x40(%rsp),%r13d
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
je     11d4 <wplata+0x54>
test   %eax,%eax
je     11ea <wplata+0x6a>
add    $0xffffffff,%ebp
jne    11b0 <wplata+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
test   %r13d,%r13d
jle    1240 <wplata+0xc0>
lea    0xe15(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r12d,%esi
call   1060 <jd_sem_up@plt>
mov    (%rbx),%esi
mov    %rbp,%rdi
call   1040 <printf@plt>
addl   $0x64,(%rbx)
mov    $0x2710,%edi
call   10a0 <usleep@plt>
mov    %r14d,%edi
mov    %r12d,%esi
mov    %r15d,%edx
call   1030 <jd_sem_down2@plt>
mov    $0x7530,%edi
call   10a0 <usleep@plt>
add    $0xffffffff,%r13d
jne    1200 <wplata+0x80>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [11 x i8] c"wplata %d\0A\00"
define void @wplata(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 = %58, %7
%20 = load i32, i32* %15
%21 = load i32, i32* %13
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %61
23: ; preds = %19
%24 = call i32 (...) @fork()
store i32 %24, i32* %17
switch i32 %24, label %57 [
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 = %50, %28
%30 = load i32, i32* %16
%31 = load i32, i32* %14
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %53
33: ; preds = %29
%34 = load i32, i32* %10
%35 = load i32, i32* %12
%36 = call i32 @jd_sem_up(i32 %34, i32 %35)
%37 = load i32*, i32** %8
%38 = load i32, i32* %37
%39 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %38)
store i32 100, i32* %18
%40 = load i32, i32* %18
%41 = load i32*, i32** %8
%42 = load i32, i32* %41
%43 = add nsw i32 %42, %40
store i32 %43, i32* %41
%44 = call i32 @usleep(i32 10000)
%45 = load i32, i32* %10
%46 = load i32, i32* %12
%47 = load i32, i32* %11
%48 = call i32 @jd_sem_down2(i32 %45, i32 %46, i32 %47)
%49 = call i32 @usleep(i32 30000)
br label %50
50: ; preds = %33
%51 = load i32, i32* %16
%52 = add nsw i32 %51, 1
store i32 %52, i32* %16
br label %29
53: ; preds = %29
%54 = load i32, i32* %9
%55 = call i32 @close(i32 %54)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %23
br label %58
58: ; preds = %57
%59 = load i32, i32* %15
%60 = add nsw i32 %59, 1
store i32 %60, i32* %15
br label %19
61: ; preds = %19
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [11 x i8] c"wplata %d\0A\00"
define void @wplata(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 %35
9: ; preds = %7, %32
%10 = phi i32 [ %33, %32 ], [ 0, %7 ]
%11 = tail call i32 (...) @fork()
switch i32 %11, label %32 [
i32 -1, label %14
i32 0, label %12
]
12: ; preds = %9
%13 = icmp sgt i32 %6, 0
br i1 %13, label %17, label %29
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, %17
%18 = phi i32 [ %27, %17 ], [ 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 ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %0
%23 = add nsw i32 %22, 100
store i32 %23, i32* %0
%24 = tail call i32 @usleep(i32 10000)
%25 = tail call i32 @jd_sem_down2(i32 %2, i32 %4, i32 %3)
%26 = tail call i32 @usleep(i32 30000)
%27 = add nuw nsw i32 %18, 1
%28 = icmp eq i32 %27, %6
br i1 %28, label %29, label %17
29: ; preds = %17, %12
%30 = tail call i32 @close(i32 %1)
%31 = tail call i32 @exit(i32 0)
unreachable
32: ; preds = %9
%33 = add nuw nsw i32 %10, 1
%34 = icmp eq i32 %33, %5
br i1 %34, label %35, label %9
35: ; preds = %32, %7
ret void
}