#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 /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
void *Consumidor(void *p) {
  extern sem_t mutex, full, empty;
  extern double bufer[10];
  extern int c_index;
  int *it;
  double valor = 0, *suma;
  it = (int *)p;
  suma = (double *)malloc(sizeof(double));
  *suma = 0.0;
  while (valor != -1) {
    sem_wait(&full);
    sem_wait(&mutex);
    valor = bufer[c_index];
    c_index = (c_index + 1) % 10;
    sem_post(&mutex);
    sem_post(&empty);
    *suma += valor;
  }
  *suma += 1.0;
  pthread_exit((void *)suma);
}


x86 assembly (clang, -O0):
<Consumidor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
movsd  -0x20(%rbp),%xmm0
movsd  0xe7e(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1197 <Consumidor+0x57>
jp     1197 <Consumidor+0x57>
jmp    1215 <Consumidor+0xd5>
mov    0x2e4a(%rip),%rdi
call   1030 <sem_wait@plt>
mov    0x2e16(%rip),%rdi
call   1030 <sem_wait@plt>
mov    0x2e12(%rip),%rax
movslq (%rax),%rcx
mov    0x2e20(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    0x2de3(%rip),%rax
mov    %edx,(%rax)
mov    0x2dd2(%rip),%rdi
call   1040 <sem_post@plt>
mov    0x2dd6(%rip),%rdi
call   1040 <sem_post@plt>
movsd  -0x20(%rbp),%xmm0
mov    -0x28(%rbp),%rax
addsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
jmp    1175 <Consumidor+0x35>
mov    -0x28(%rbp),%rax
movsd  0xde7(%rip),%xmm0
addsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Consumidor>:
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    0x2e82(%rip),%r14
mov    0x2e53(%rip),%r15
mov    0x2e54(%rip),%r13
mov    0x2e65(%rip),%rbp
mov    0x2e4e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <sem_wait@plt>
mov    %r15,%rdi
call   1030 <sem_wait@plt>
movslq 0x0(%r13),%rax
movsd  0x0(%rbp,%rax,8),%xmm0
movsd  %xmm0,(%rsp)
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>
movsd  (%rbx),%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rbx)
ucomisd 0xe04(%rip),%xmm1
jne    1190 <Consumidor+0x50>
jp     1190 <Consumidor+0x50>
addsd  0xe00(%rip),%xmm0
movsd  %xmm0,(%rbx)
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):
@full = external global i32
@mutex = external global i32
@bufer = external global [10 x double]
@c_index = external global i32
@empty = external global i32
define i8* @Consumidor(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca double
%6 = alloca double*
store i8* %0, i8** %3
store double 0.000000e+00, double* %5
%7 = load i8*, i8** %3
%8 = bitcast i8* %7 to i32*
store i32* %8, i32** %4
%9 = call i64 @malloc(i32 8)
%10 = inttoptr i64 %9 to double*
store double* %10, double** %6
%11 = load double*, double** %6
store double 0.000000e+00, double* %11
br label %12
12: ; preds = %15, %1
%13 = load double, double* %5
%14 = fcmp une double %13, -1.000000e+00
br i1 %14, label %15, label %31
15: ; preds = %12
%16 = call i32 @sem_wait(i32* @full)
%17 = call i32 @sem_wait(i32* @mutex)
%18 = load i32, i32* @c_index
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [10 x double], [10 x double]* @bufer, i64 0, i64 %19
%21 = load double, double* %20
store double %21, double* %5
%22 = load i32, i32* @c_index
%23 = add nsw i32 %22, 1
%24 = srem i32 %23, 10
store i32 %24, i32* @c_index
%25 = call i32 @sem_post(i32* @mutex)
%26 = call i32 @sem_post(i32* @empty)
%27 = load double, double* %5
%28 = load double*, double** %6
%29 = load double, double* %28
%30 = fadd double %29, %27
store double %30, double* %28
br label %12
31: ; preds = %12
%32 = load double*, double** %6
%33 = load double, double* %32
%34 = fadd double %33, 1.000000e+00
store double %34, double* %32
%35 = load double*, double** %6
%36 = bitcast double* %35 to i8*
%37 = call i32 @pthread_exit(i8* %36)
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
@full = external global i32
@mutex = external global i32
@bufer = external global [10 x double]
@c_index = external global i32
@empty = external global i32
define noalias i8* @Consumidor(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 = tail call i32 @sem_wait(i32* @full)
%6 = tail call i32 @sem_wait(i32* @mutex)
%7 = load i32, i32* @c_index
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds [10 x double], [10 x double]* @bufer, i64 0, i64 %8
%10 = load double, double* %9
%11 = add nsw i32 %7, 1
%12 = srem i32 %11, 10
store i32 %12, i32* @c_index
%13 = tail call i32 @sem_post(i32* @mutex)
%14 = tail call i32 @sem_post(i32* @empty)
%15 = load double, double* %3
%16 = fadd double %10, %15
store double %16, double* %3
%17 = fcmp une double %10, -1.000000e+00
br i1 %17, label %4, label %18
18: ; preds = %4
%19 = fadd double %16, 1.000000e+00
store double %19, double* %3
%20 = inttoptr i64 %2 to i8*
%21 = tail call i32 @pthread_exit(i8* %20)
ret i8* undef
}