#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 */
int /*<<< orphan*/ EntrarDato(int, int);
int ProducirDato(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
void *productor(void *p) {
  extern sem_t mutex, entradas_ocupadas, entradas_libres;
  extern double buffer[100];
  extern int ind_p, MAX_P;
  extern double total_p;
  int dato;
  double valor, *suma;
  suma = (double *)malloc(sizeof(double));
  *suma = 0.0;
  sem_wait(&mutex);
  while (MAX_P > 0) {
    sem_post(&mutex);
    dato = ProducirDato(100);
    sem_wait(&entradas_libres);
    sem_wait(&mutex);
    EntrarDato(dato, ind_p);
    total_p += dato;
    ind_p = (ind_p + 1) % 100;
    MAX_P--;
    sem_post(&mutex);
    sem_post(&entradas_ocupadas);
    sem_wait(&mutex);
  }
  sem_post(&mutex);
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<productor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x8,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
mov    0x2e2c(%rip),%rdi
call   1040 <sem_wait@plt>
mov    0x2e28(%rip),%rax
cmpl   $0x0,(%rax)
jle    1259 <productor+0xf9>
mov    0x2e10(%rip),%rdi
call   1060 <sem_post@plt>
mov    $0x64,%edi
call   1050 <ProducirDato@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e27(%rip),%rdi
call   1040 <sem_wait@plt>
mov    0x2deb(%rip),%rdi
call   1040 <sem_wait@plt>
mov    -0x14(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1030 <EntrarDato@plt>
cvtsi2sdl -0x14(%rbp),%xmm0
mov    0x2dd9(%rip),%rax
addsd  (%rax),%xmm0
mov    0x2dce(%rip),%rax
movsd  %xmm0,(%rax)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    0x2db7(%rip),%rax
mov    %edx,(%rax)
mov    0x2d9e(%rip),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    0x2d92(%rip),%rax
mov    %ecx,(%rax)
mov    0x2d81(%rip),%rdi
call   1060 <sem_post@plt>
mov    0x2d9d(%rip),%rdi
call   1060 <sem_post@plt>
mov    0x2d69(%rip),%rdi
call   1040 <sem_wait@plt>
jmp    1191 <productor+0x31>
mov    0x2d58(%rip),%rdi
call   1060 <sem_post@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <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   1080 <malloc@plt>
movq   $0x0,(%rax)
mov    0x2e35(%rip),%rdi
call   1040 <sem_wait@plt>
mov    0x2e31(%rip),%r12
cmpl   $0x0,(%r12)
jle    125a <productor+0xfa>
mov    0x2e17(%rip),%rbx
mov    0x2e28(%rip),%r13
mov    0x2e19(%rip),%r14
mov    0x2e2a(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <sem_post@plt>
mov    $0x64,%edi
call   1050 <ProducirDato@plt>
mov    %eax,%ebp
mov    0x2e0d(%rip),%rdi
call   1040 <sem_wait@plt>
mov    %rbx,%rdi
call   1040 <sem_wait@plt>
mov    0x0(%r13),%esi
mov    %ebp,%edi
call   1030 <EntrarDato@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
addsd  (%r14),%xmm0
movsd  %xmm0,(%r14)
mov    0x0(%r13),%eax
mov    %eax,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   $0x51eb851f,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,0x0(%r13)
addl   $0xffffffff,(%r12)
mov    %rbx,%rdi
call   1060 <sem_post@plt>
mov    %r15,%rdi
call   1060 <sem_post@plt>
mov    %rbx,%rdi
call   1040 <sem_wait@plt>
cmpl   $0x0,(%r12)
jg     11c0 <productor+0x60>
mov    0x2d57(%rip),%rdi
call   1060 <sem_post@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <pthread_exit@plt>

LLVM IR (-O0):
@mutex = external global i32
@MAX_P = external global i32
@entradas_libres = external global i32
@ind_p = external global i32
@total_p = external global double
@entradas_ocupadas = external global i32
define i8* @productor(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca double
%6 = alloca double*
store i8* %0, i8** %3
%7 = call i64 @malloc(i32 8)
%8 = inttoptr i64 %7 to double*
store double* %8, double** %6
%9 = load double*, double** %6
store double 0.000000e+00, double* %9
%10 = call i32 @sem_wait(i32* @mutex)
br label %11
11: ; preds = %14, %1
%12 = load i32, i32* @MAX_P
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %34
14: ; preds = %11
%15 = call i32 @sem_post(i32* @mutex)
%16 = call i32 @ProducirDato(i32 100)
store i32 %16, i32* %4
%17 = call i32 @sem_wait(i32* @entradas_libres)
%18 = call i32 @sem_wait(i32* @mutex)
%19 = load i32, i32* %4
%20 = load i32, i32* @ind_p
%21 = call i32 @EntrarDato(i32 %19, i32 %20)
%22 = load i32, i32* %4
%23 = sitofp i32 %22 to double
%24 = load double, double* @total_p
%25 = fadd double %24, %23
store double %25, double* @total_p
%26 = load i32, i32* @ind_p
%27 = add nsw i32 %26, 1
%28 = srem i32 %27, 100
store i32 %28, i32* @ind_p
%29 = load i32, i32* @MAX_P
%30 = add nsw i32 %29, -1
store i32 %30, i32* @MAX_P
%31 = call i32 @sem_post(i32* @mutex)
%32 = call i32 @sem_post(i32* @entradas_ocupadas)
%33 = call i32 @sem_wait(i32* @mutex)
br label %11
34: ; preds = %11
%35 = call i32 @sem_post(i32* @mutex)
%36 = call i32 @pthread_exit(i32* null)
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
@mutex = external global i32
@MAX_P = external global i32
@entradas_libres = external global i32
@ind_p = external global i32
@total_p = external global double
@entradas_ocupadas = 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
%4 = tail call i32 @sem_wait(i32* @mutex)
%5 = load i32, i32* @MAX_P
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %27
7: ; preds = %1, %7
%8 = tail call i32 @sem_post(i32* @mutex)
%9 = tail call i32 @ProducirDato(i32 100)
%10 = tail call i32 @sem_wait(i32* @entradas_libres)
%11 = tail call i32 @sem_wait(i32* @mutex)
%12 = load i32, i32* @ind_p
%13 = tail call i32 @EntrarDato(i32 %9, i32 %12)
%14 = sitofp i32 %9 to double
%15 = load double, double* @total_p
%16 = fadd double %15, %14
store double %16, double* @total_p
%17 = load i32, i32* @ind_p
%18 = add nsw i32 %17, 1
%19 = srem i32 %18, 100
store i32 %19, i32* @ind_p
%20 = load i32, i32* @MAX_P
%21 = add nsw i32 %20, -1
store i32 %21, i32* @MAX_P
%22 = tail call i32 @sem_post(i32* @mutex)
%23 = tail call i32 @sem_post(i32* @entradas_ocupadas)
%24 = tail call i32 @sem_wait(i32* @mutex)
%25 = load i32, i32* @MAX_P
%26 = icmp sgt i32 %25, 0
br i1 %26, label %7, label %27
27: ; preds = %7, %1
%28 = tail call i32 @sem_post(i32* @mutex)
%29 = tail call i32 @pthread_exit(i32* null)
ret i8* undef
}