#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_t;
typedef int /*<<< orphan*/ pthread_attr_t;

/* Variables and functions */
void *Consumidor(void *);
int /*<<< orphan*/ pthread_attr_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  void *(*)(void *), int *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_init(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ srandom(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  int i;
  extern sem_t full[3], empty;
  extern int contador[10];
  pthread_t consumidor[3], productor;
  int v[3];
  void *Productor(void *), *Consumidor(void *);
  pthread_attr_t atributos;
  pthread_attr_init(&atributos);
  sem_init(&empty, 0, 10);
  for (i = 0; i < 10; i++)
    contador[i] = 0;
  for (i = 0; i < 3; i++)
    sem_init(&full[i], 0, 0);
  srandom(-time(NULL));
  pthread_create(&productor, &atributos, Productor, NULL);
  for (i = 0; i < 3; i++) {
    v[i] = i;
    pthread_create(&consumidor[i], &atributos, Consumidor, &v[i]);
  }
  pthread_join(productor, NULL);
  for (i = 0; i < 3; i++)
    pthread_join(consumidor[i], NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <pthread_attr_init@plt>
mov    0x2e51(%rip),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <sem_init@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    11bc <main+0x5c>
movslq -0x8(%rbp),%rcx
mov    0x2e19(%rip),%rax
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1192 <main+0x32>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11f6 <main+0x96>
movslq -0x8(%rbp),%rax
mov    0x2e10(%rip),%rdi
shl    $0x2,%rax
add    %rax,%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <sem_init@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11c3 <main+0x63>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
xor    %edi,%edi
sub    %eax,%edi
call   1030 <srandom@plt>
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    0x2dc9(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <pthread_create@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1278 <main+0x118>
mov    -0x8(%rbp),%ecx
movslq -0x8(%rbp),%rax
mov    %ecx,-0x24(%rbp,%rax,4)
movslq -0x8(%rbp),%rax
lea    -0x14(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
movslq -0x8(%rbp),%rax
lea    -0x24(%rbp),%rcx
shl    $0x2,%rax
add    %rax,%rcx
lea    -0x28(%rbp),%rsi
mov    0x2d63(%rip),%rdx
call   1070 <pthread_create@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1227 <main+0xc7>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <pthread_join@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    12b4 <main+0x154>
movslq -0x8(%rbp),%rax
mov    -0x14(%rbp,%rax,4),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <pthread_join@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    128b <main+0x12b>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0x24(%rsp),%r14
mov    %r14,%rdi
call   1040 <pthread_attr_init@plt>
mov    0x2e55(%rip),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <sem_init@plt>
xorps  %xmm0,%xmm0
mov    0x2e2f(%rip),%rax
movaps %xmm0,(%rax)
movaps %xmm0,0x10(%rax)
movq   $0x0,0x20(%rax)
mov    0x2e41(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <sem_init@plt>
lea    0x4(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <sem_init@plt>
add    $0x8,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <sem_init@plt>
xor    %edi,%edi
call   1050 <time@plt>
neg    %eax
mov    %eax,%edi
call   1030 <srandom@plt>
mov    0x2df9(%rip),%rdx
lea    0x14(%rsp),%rdi
mov    %r14,%rsi
xor    %ecx,%ecx
call   1070 <pthread_create@plt>
movl   $0x0,0x8(%rsp)
mov    0x2dc3(%rip),%rbx
lea    0x18(%rsp),%rdi
lea    0x8(%rsp),%rcx
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <pthread_create@plt>
lea    0xc(%rsp),%rcx
movl   $0x1,0xc(%rsp)
lea    0x1c(%rsp),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <pthread_create@plt>
lea    0x10(%rsp),%rcx
movl   $0x2,0x10(%rsp)
lea    0x20(%rsp),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <pthread_create@plt>
mov    0x14(%rsp),%edi
xor    %esi,%esi
call   1080 <pthread_join@plt>
mov    0x18(%rsp),%edi
xor    %esi,%esi
call   1080 <pthread_join@plt>
mov    0x1c(%rsp),%edi
xor    %esi,%esi
call   1080 <pthread_join@plt>
mov    0x20(%rsp),%edi
xor    %esi,%esi
call   1080 <pthread_join@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@empty = external global i32
@contador = external global [10 x i32]
@full = external global [3 x i32]
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [3 x i32]
%4 = alloca i32
%5 = alloca [3 x i32]
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @pthread_attr_init(i32* %6)
%8 = call i32 @sem_init(i32* @empty, i32 0, i32 10)
store i32 0, i32* %2
br label %9
9: ; preds = %16, %0
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load i32, i32* %2
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [10 x i32], [10 x i32]* @contador, i64 0, i64 %14
store i32 0, i32* %15
br label %16
16: ; preds = %12
%17 = load i32, i32* %2
%18 = add nsw i32 %17, 1
store i32 %18, i32* %2
br label %9
19: ; preds = %9
store i32 0, i32* %2
br label %20
20: ; preds = %28, %19
%21 = load i32, i32* %2
%22 = icmp slt i32 %21, 3
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i32, i32* %2
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [3 x i32], [3 x i32]* @full, i64 0, i64 %25
%27 = call i32 @sem_init(i32* %26, i32 0, i32 0)
br label %28
28: ; preds = %23
%29 = load i32, i32* %2
%30 = add nsw i32 %29, 1
store i32 %30, i32* %2
br label %20
31: ; preds = %20
%32 = call i32 @time(i32* null)
%33 = sub nsw i32 0, %32
%34 = call i32 @srandom(i32 %33)
%35 = call i32 @pthread_create(i32* %4, i32* %6, i8* (i8*)* @Productor, i32* null)
store i32 0, i32* %2
br label %36
36: ; preds = %51, %31
%37 = load i32, i32* %2
%38 = icmp slt i32 %37, 3
br i1 %38, label %39, label %54
39: ; preds = %36
%40 = load i32, i32* %2
%41 = load i32, i32* %2
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 %42
store i32 %40, i32* %43
%44 = load i32, i32* %2
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %45
%47 = load i32, i32* %2
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [3 x i32], [3 x i32]* %5, i64 0, i64 %48
%50 = call i32 @pthread_create(i32* %46, i32* %6, i8* (i8*)* @Consumidor, i32* %49)
br label %51
51: ; preds = %39
%52 = load i32, i32* %2
%53 = add nsw i32 %52, 1
store i32 %53, i32* %2
br label %36
54: ; preds = %36
%55 = load i32, i32* %4
%56 = call i32 @pthread_join(i32 %55, i32* null)
store i32 0, i32* %2
br label %57
57: ; preds = %66, %54
%58 = load i32, i32* %2
%59 = icmp slt i32 %58, 3
br i1 %59, label %60, label %69
60: ; preds = %57
%61 = load i32, i32* %2
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %62
%64 = load i32, i32* %63
%65 = call i32 @pthread_join(i32 %64, i32* null)
br label %66
66: ; preds = %60
%67 = load i32, i32* %2
%68 = add nsw i32 %67, 1
store i32 %68, i32* %2
br label %57
69: ; preds = %57
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
@empty = external global i32
@contador = external global [10 x i32]
@full = external global [3 x i32]
define i32 @main() {
%1 = alloca [3 x i32]
%2 = alloca i32
%3 = alloca [3 x i32]
%4 = alloca i32
%5 = bitcast [3 x i32]* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast [3 x i32]* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = call i32 @pthread_attr_init(i32* %4)
%10 = call i32 @sem_init(i32* @empty, i32 0, i32 10)
%11 = call i32 @sem_init(i32* getelementptr inbounds ([3 x i32], [3 x i32]* @full, i64 0, i64 0), i32 0, i32 0)
%12 = call i32 @sem_init(i32* getelementptr inbounds ([3 x i32], [3 x i32]* @full, i64 0, i64 1), i32 0, i32 0)
%13 = call i32 @sem_init(i32* getelementptr inbounds ([3 x i32], [3 x i32]* @full, i64 0, i64 2), i32 0, i32 0)
%14 = call i32 @time(i32* null)
%15 = sub nsw i32 0, %14
%16 = call i32 @srandom(i32 %15)
%17 = call i32 @pthread_create(i32* %2, i32* %4, i8* (i8*)* @Productor, i32* null)
%18 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
store i32 0, i32* %18
%19 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 0
%20 = call i32 @pthread_create(i32* %19, i32* %4, i8* (i8*)* @Consumidor, i32* %18)
%21 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
store i32 1, i32* %21
%22 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 1
%23 = call i32 @pthread_create(i32* %22, i32* %4, i8* (i8*)* @Consumidor, i32* %21)
%24 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
store i32 2, i32* %24
%25 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 0, i64 2
%26 = call i32 @pthread_create(i32* %25, i32* %4, i8* (i8*)* @Consumidor, i32* %24)
%27 = load i32, i32* %2
%28 = call i32 @pthread_join(i32 %27, i32* null)
%29 = load i32, i32* %19
%30 = call i32 @pthread_join(i32 %29, i32* null)
%31 = load i32, i32* %22
%32 = call i32 @pthread_join(i32 %31, i32* null)
%33 = load i32, i32* %25
%34 = call i32 @pthread_join(i32 %33, i32* null)
ret i32 0
}