#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*/ sem_t;
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int random();
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
void *Productor(void *p) {
  int p_index = 0, item, i, j, suma = 0;
  extern sem_t full[3], empty;
  extern pthread_mutex_t ex_buf;
  extern int bufer[10];
  for (i = 0; i < 100; i++) {
    suma += (item = random() % 100);
    sem_wait(&empty);
    pthread_mutex_lock(&ex_buf);
    bufer[p_index] = item;
    pthread_mutex_unlock(&ex_buf);
    for (j = 0; j < 3; j++)
      sem_post(&full[j]);
    p_index = (p_index + 1) % 10;
    fprintf(stdout, "Pro: Item %d\n", item);
  }
  fprintf(stdout, "Pro: Suma %d\n", suma);
}


x86 assembly (clang, -O0):
<Productor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    1245 <Productor+0xe5>
mov    $0x0,%al
call   1060 <random@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
add    -0x24(%rbp),%edx
mov    %edx,-0x24(%rbp)
mov    0x2e1e(%rip),%rdi
call   1030 <sem_wait@plt>
mov    0x2e22(%rip),%rdi
call   1080 <pthread_mutex_lock@plt>
mov    -0x18(%rbp),%edx
movslq -0x14(%rbp),%rcx
mov    0x2e17(%rip),%rax
mov    %edx,(%rax,%rcx,4)
mov    0x2e05(%rip),%rdi
call   1070 <pthread_mutex_unlock@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x20(%rbp)
jge    120e <Productor+0xae>
movslq -0x20(%rbp),%rax
mov    0x2df4(%rip),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   1040 <sem_post@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11df <Productor+0x7f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xdce(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1181 <Productor+0x21>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xdb6(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Productor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x64,%r15d
mov    0x2e6d(%rip),%rax
lea    0x4(%rax),%rcx
mov    %rcx,0x10(%rsp)
add    $0x8,%rax
mov    %rax,0x8(%rsp)
xor    %ebp,%ebp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <random@plt>
movslq %eax,%r14
imul   $0x51eb851f,%r14,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
imul   $0x64,%eax,%eax
sub    %eax,%r14d
mov    0x2dfd(%rip),%rdi
call   1030 <sem_wait@plt>
mov    0x2e01(%rip),%r13
mov    %r13,%rdi
call   1080 <pthread_mutex_lock@plt>
movslq %r12d,%rbx
mov    0x2df7(%rip),%rax
mov    %r14d,(%rax,%rbx,4)
mov    %r13,%rdi
call   1070 <pthread_mutex_unlock@plt>
mov    0x2dec(%rip),%rdi
call   1040 <sem_post@plt>
mov    0x10(%rsp),%rdi
call   1040 <sem_post@plt>
mov    0x8(%rsp),%rdi
call   1040 <sem_post@plt>
add    %r14d,%ebp
mov    %ebx,%eax
add    $0x1,%eax
cltq
imul   $0x66666667,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
neg    %eax
lea    (%rbx,%rax,1),%r12d
add    $0x1,%r12d
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
mov    %r14d,%edx
call   1050 <fprintf@plt>
add    $0xffffffff,%r15d
jne    11a0 <Productor+0x40>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
mov    %ebp,%edx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>

LLVM IR (-O0):
@empty = external global i32
@ex_buf = external global i32
@bufer = external global [10 x i32]
@full = external global [3 x i32]
@stdout = global i32 0
@.str = [14 x i8] c"Pro: Item %d\0A\00"
@.str.1 = [14 x i8] c"Pro: Suma %d\0A\00"
define i8* @Productor(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
store i32 0, i32* %8
store i32 0, i32* %6
br label %9
9: ; preds = %42, %1
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 100
br i1 %11, label %12, label %45
12: ; preds = %9
%13 = call i32 (...) @random()
%14 = srem i32 %13, 100
store i32 %14, i32* %5
%15 = load i32, i32* %8
%16 = add nsw i32 %15, %14
store i32 %16, i32* %8
%17 = call i32 @sem_wait(i32* @empty)
%18 = call i32 @pthread_mutex_lock(i32* @ex_buf)
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [10 x i32], [10 x i32]* @bufer, i64 0, i64 %21
store i32 %19, i32* %22
%23 = call i32 @pthread_mutex_unlock(i32* @ex_buf)
store i32 0, i32* %7
br label %24
24: ; preds = %32, %12
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 3
br i1 %26, label %27, label %35
27: ; preds = %24
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [3 x i32], [3 x i32]* @full, i64 0, i64 %29
%31 = call i32 @sem_post(i32* %30)
br label %32
32: ; preds = %27
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %24
35: ; preds = %24
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
%38 = srem i32 %37, 10
store i32 %38, i32* %4
%39 = load i32, i32* @stdout
%40 = load i32, i32* %5
%41 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %35
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
store i32 %44, i32* %6
br label %9
45: ; preds = %9
%46 = load i32, i32* @stdout
%47 = load i32, i32* %8
%48 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
@empty = external global i32
@ex_buf = external global i32
@bufer = external global [10 x i32]
@full = external global [3 x i32]
@stdout = global i32 0
@.str = [14 x i8] c"Pro: Item %d\0A\00"
@.str.1 = [14 x i8] c"Pro: Suma %d\0A\00"
define noalias i8* @Productor(i8* nocapture readnone %0) {
br label %2
2: ; preds = %1, %2
%3 = phi i32 [ 0, %1 ], [ %16, %2 ]
%4 = phi i32 [ 0, %1 ], [ %21, %2 ]
%5 = phi i32 [ 0, %1 ], [ %18, %2 ]
%6 = tail call i32 (...) @random()
%7 = srem i32 %6, 100
%8 = tail call i32 @sem_wait(i32* @empty)
%9 = tail call i32 @pthread_mutex_lock(i32* @ex_buf)
%10 = sext i32 %5 to i64
%11 = getelementptr inbounds [10 x i32], [10 x i32]* @bufer, i64 0, i64 %10
store i32 %7, i32* %11
%12 = tail call i32 @pthread_mutex_unlock(i32* @ex_buf)
%13 = tail call i32 @sem_post(i32* getelementptr inbounds ([3 x i32], [3 x i32]* @full, i64 0, i64 0))
%14 = tail call i32 @sem_post(i32* getelementptr inbounds ([3 x i32], [3 x i32]* @full, i64 0, i64 1))
%15 = tail call i32 @sem_post(i32* getelementptr inbounds ([3 x i32], [3 x i32]* @full, i64 0, i64 2))
%16 = add nsw i32 %7, %3
%17 = add nsw i32 %5, 1
%18 = srem i32 %17, 10
%19 = load i32, i32* @stdout
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%21 = add nuw nsw i32 %4, 1
%22 = icmp eq i32 %21, 100
br i1 %22, label %23, label %2
23: ; preds = %2
%24 = load i32, i32* @stdout
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %16)
ret i8* undef
}