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

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ pthread_exit(void *);
int rand();
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
void *Productor(void *p) {
  extern sem_t mutex, full, empty;
  extern double bufer[10];
  extern int p_index;
  int *it, i;
  double valor, *suma;
  it = (int *)p;
  suma = (double *)malloc(sizeof(double));
  *suma = 0.0;
  for (i = 0; i < 100; i++) {
    *suma += (valor = (double)(rand() % 1000));
    sem_wait(&empty);
    sem_wait(&mutex);
    bufer[p_index] = valor;
    p_index = (p_index + 1) % 10;
    sem_post(&mutex);
    sem_post(&full);
  }
  pthread_exit((void *)suma);
}


x86 assembly (clang, -O0):
<Productor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    1228 <Productor+0xd8>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cvtsi2sd %edx,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x30(%rbp),%rax
addsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
mov    0x2e17(%rip),%rdi
call   1030 <sem_wait@plt>
mov    0x2dfb(%rip),%rdi
call   1030 <sem_wait@plt>
movsd  -0x28(%rbp),%xmm0
mov    0x2df2(%rip),%rax
movslq (%rax),%rcx
mov    0x2e00(%rip),%rax
movsd  %xmm0,(%rax,%rcx,8)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    0x2dc8(%rip),%rax
mov    %edx,(%rax)
mov    0x2db7(%rip),%rdi
call   1040 <sem_post@plt>
mov    0x2dd3(%rip),%rdi
call   1040 <sem_post@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1184 <Productor+0x34>
mov    -0x30(%rbp),%rdi
call   1050 <pthread_exit@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
push   %rax
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movq   $0x0,(%rax)
mov    $0x64,%ebp
mov    0x2e45(%rip),%r15
mov    0x2e46(%rip),%r13
mov    0x2e57(%rip),%r14
mov    0x2e58(%rip),%r12
xor    %eax,%eax
call   1070 <rand@plt>
cltq
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rbx),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rbx)
mov    0x2dfc(%rip),%rdi
call   1030 <sem_wait@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
movslq 0x0(%r13),%rax
movsd  (%rsp),%xmm0
movsd  %xmm0,(%r14,%rax,8)
add    $0x1,%eax
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %eax,0x0(%r13)
mov    %r15,%rdi
call   1040 <sem_post@plt>
mov    %r12,%rdi
call   1040 <sem_post@plt>
add    $0xffffffff,%ebp
jne    1190 <Productor+0x40>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <pthread_exit@plt>

LLVM IR (-O0):
@empty = external global i32
@mutex = external global i32
@bufer = external global [10 x double]
@p_index = external global i32
@full = external global i32
define i8* @Productor(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca double
%7 = alloca double*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
store i32* %9, i32** %4
%10 = call i64 @malloc(i32 8)
%11 = inttoptr i64 %10 to double*
store double* %11, double** %7
%12 = load double*, double** %7
store double 0.000000e+00, double* %12
store i32 0, i32* %5
br label %13
13: ; preds = %34, %1
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 100
br i1 %15, label %16, label %37
16: ; preds = %13
%17 = call i32 (...) @rand()
%18 = srem i32 %17, 1000
%19 = sitofp i32 %18 to double
store double %19, double* %6
%20 = load double*, double** %7
%21 = load double, double* %20
%22 = fadd double %21, %19
store double %22, double* %20
%23 = call i32 @sem_wait(i32* @empty)
%24 = call i32 @sem_wait(i32* @mutex)
%25 = load double, double* %6
%26 = load i32, i32* @p_index
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [10 x double], [10 x double]* @bufer, i64 0, i64 %27
store double %25, double* %28
%29 = load i32, i32* @p_index
%30 = add nsw i32 %29, 1
%31 = srem i32 %30, 10
store i32 %31, i32* @p_index
%32 = call i32 @sem_post(i32* @mutex)
%33 = call i32 @sem_post(i32* @full)
br label %34
34: ; preds = %16
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
br label %13
37: ; preds = %13
%38 = load double*, double** %7
%39 = bitcast double* %38 to i8*
%40 = call i32 @pthread_exit(i8* %39)
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
@empty = external global i32
@mutex = external global i32
@bufer = external global [10 x double]
@p_index = external global i32
@full = external global i32
define noalias i8* @Productor(i8* nocapture readnone %0) {
%2 = tail call i64 @malloc(i32 8)
%3 = inttoptr i64 %2 to double*
store double 0.000000e+00, double* %3
br label %4
4: ; preds = %1, %4
%5 = phi i32 [ 0, %1 ], [ %20, %4 ]
%6 = tail call i32 (...) @rand()
%7 = srem i32 %6, 1000
%8 = sitofp i32 %7 to double
%9 = load double, double* %3
%10 = fadd double %9, %8
store double %10, double* %3
%11 = tail call i32 @sem_wait(i32* @empty)
%12 = tail call i32 @sem_wait(i32* @mutex)
%13 = load i32, i32* @p_index
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [10 x double], [10 x double]* @bufer, i64 0, i64 %14
store double %8, double* %15
%16 = add nsw i32 %13, 1
%17 = srem i32 %16, 10
store i32 %17, i32* @p_index
%18 = tail call i32 @sem_post(i32* @mutex)
%19 = tail call i32 @sem_post(i32* @full)
%20 = add nuw nsw i32 %5, 1
%21 = icmp eq i32 %20, 100
br i1 %21, label %22, label %4
22: ; preds = %4
%23 = inttoptr i64 %2 to i8*
%24 = tail call i32 @pthread_exit(i8* %23)
ret i8* undef
}