#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 */

/* Variables and functions */
int /*<<< orphan*/ P(int, int /*<<< orphan*/);
int /*<<< orphan*/ V(int, int);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ sleep(unsigned int);
int /*<<< orphan*/ srand(unsigned int);
void produce(int *buffer, int id_semaphores) {
  int in_index;
  int next_value;
  srand((unsigned int)getpid());
  in_index = 0;
  for (;;) {
    sleep((unsigned int)(5 + rand() % 5));
    next_value = rand() % 100;
    printf("producer: %d\n", next_value);
    P(id_semaphores, 0);
    buffer[in_index] = next_value;
    in_index = (in_index + 1) % 10;
    V(id_semaphores, 1);
  }
}


x86 assembly (clang, -O0):
<produce>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%edi
call   1070 <srand@plt>
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x5,%edi
mov    %edi,-0x1c(%rbp)
call   1080 <sleep@plt>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe34(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1030 <P@plt>
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1040 <V@plt>
jmp    1194 <produce+0x24>

x86 assembly (clang, -O2):
<produce>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
call   1070 <srand@plt>
lea    0xe6d(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1090 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x5,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1090 <rand@plt>
movslq %eax,%rbx
imul   $0x51eb851f,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
imul   $0x64,%eax,%eax
sub    %eax,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
mov    %r12d,%edi
xor    %esi,%esi
call   1030 <P@plt>
movslq %ebp,%rax
mov    %ebx,(%r14,%rax,4)
mov    %eax,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
imul   $0x66666667,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%ebp
add    $0x1,%ebp
mov    %r12d,%edi
mov    $0x1,%esi
call   1040 <V@plt>
jmp    11a0 <produce+0x30>

LLVM IR (-O0):
@.str = [14 x i8] c"producer: %d\0A\00"
define void @produce(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = call i64 (...) @getpid()
%8 = trunc i64 %7 to i32
%9 = call i32 @srand(i32 %8)
store i32 0, i32* %5
br label %10
10: ; preds = %10, %2
%11 = call i32 (...) @rand()
%12 = srem i32 %11, 5
%13 = add nsw i32 5, %12
%14 = call i32 @sleep(i32 %13)
%15 = call i32 (...) @rand()
%16 = srem i32 %15, 100
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %4
%20 = call i32 @P(i32 %19, i32 0)
%21 = load i32, i32* %6
%22 = load i32*, i32** %3
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
store i32 %21, i32* %25
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
%28 = srem i32 %27, 10
store i32 %28, i32* %5
%29 = load i32, i32* %4
%30 = call i32 @V(i32 %29, i32 1)
br label %10
}

LLVM IR (-O2):
@.str = [14 x i8] c"producer: %d\0A\00"
define void @produce(i32* nocapture writeonly %0, i32 %1) {
%3 = tail call i64 (...) @getpid()
%4 = trunc i64 %3 to i32
%5 = tail call i32 @srand(i32 %4)
br label %6
6: ; preds = %6, %2
%7 = phi i32 [ 0, %2 ], [ %19, %6 ]
%8 = tail call i32 (...) @rand()
%9 = srem i32 %8, 5
%10 = add nsw i32 %9, 5
%11 = tail call i32 @sleep(i32 %10)
%12 = tail call i32 (...) @rand()
%13 = srem i32 %12, 100
%14 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = tail call i32 @P(i32 %1, i32 0)
%16 = sext i32 %7 to i64
%17 = getelementptr inbounds i32, i32* %0, i64 %16
store i32 %13, i32* %17
%18 = add nsw i32 %7, 1
%19 = srem i32 %18, 10
%20 = tail call i32 @V(i32 %1, i32 1)
br label %6
}