#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);
int /*<<< orphan*/ V(int, int /*<<< orphan*/);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ sleep(unsigned int);
int /*<<< orphan*/ srand(unsigned int);
void consume(int *buffer, int id_semaphores) {
  int out_index;
  int next_value;
  srand((unsigned int)getpid());
  out_index = 0;
  for (;;) {
    P(id_semaphores, 1);
    next_value = buffer[out_index];
    out_index = (out_index + 1) % 10;
    V(id_semaphores, 0);
    printf("consumer: %d\n", next_value);
    sleep((unsigned int)(10 + rand() % 10));
  }
}


x86 assembly (clang, -O0):
<consume>:
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    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1030 <P@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1040 <V@plt>
mov    -0x14(%rbp),%esi
lea    0xe29(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0xa,%edi
mov    %edi,-0x18(%rbp)
call   1080 <sleep@plt>
jmp    1194 <consume+0x24>

x86 assembly (clang, -O2):
<consume>:
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    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    $0x1,%esi
call   1030 <P@plt>
movslq %ebx,%rax
mov    (%r14,%rax,4),%ebp
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),%ebx
add    $0x1,%ebx
mov    %r12d,%edi
xor    %esi,%esi
call   1040 <V@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <rand@plt>
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
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0xa,%edi
call   1080 <sleep@plt>
jmp    11a0 <consume+0x30>

LLVM IR (-O0):
@.str = [14 x i8] c"consumer: %d\0A\00"
define void @consume(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 = load i32, i32* %4
%12 = call i32 @P(i32 %11, i32 1)
%13 = load i32*, i32** %3
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i32, i32* %13, i64 %15
%17 = load i32, i32* %16
store i32 %17, i32* %6
%18 = load i32, i32* %5
%19 = add nsw i32 %18, 1
%20 = srem i32 %19, 10
store i32 %20, i32* %5
%21 = load i32, i32* %4
%22 = call i32 @V(i32 %21, i32 0)
%23 = load i32, i32* %6
%24 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = call i32 (...) @rand()
%26 = srem i32 %25, 10
%27 = add nsw i32 10, %26
%28 = call i32 @sleep(i32 %27)
br label %10
}

LLVM IR (-O2):
@.str = [14 x i8] c"consumer: %d\0A\00"
define void @consume(i32* nocapture %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 ], [ %13, %6 ]
%8 = tail call i32 @P(i32 %1, i32 1)
%9 = sext i32 %7 to i64
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = add nsw i32 %7, 1
%13 = srem i32 %12, 10
%14 = tail call i32 @V(i32 %1, i32 0)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
%16 = tail call i32 (...) @rand()
%17 = srem i32 %16, 10
%18 = add nsw i32 %17, 10
%19 = tail call i32 @sleep(i32 %18)
br label %6
}