#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*/ printf(char *, int, ...);
int /*<<< orphan*/ sem_signal(int, int);
int /*<<< orphan*/ sem_wait(int, int);
int /*<<< orphan*/ sleep(int);
void consume(int id, int *buffer, int *in, int *out, int semid) {
  int i;
  int value;
  printf("Consumer of ID %d begins.\n", id);
  for (i = 0; i < 12; i++) {
    sem_wait(semid, 0);
    sem_wait(semid, 2);
    value = buffer[*in];
    *in = (*in + 1) % 5;
    sem_signal(semid, 2);
    sem_signal(semid, 1);
    printf("Consumer of ID %d : element %2d == %2d\n", id, i, value);
    if ((i + id) % 5 == 0)
      sleep(1);
  }
  printf("Consumer of ID %d has finished.\n", id);
}


x86 assembly (clang, -O0):
<consume>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0xc,-0x28(%rbp)
jge    1221 <consume+0xe1>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1030 <sem_wait@plt>
mov    -0x24(%rbp),%edi
mov    $0x2,%esi
call   1030 <sem_wait@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
mov    %edx,(%rax)
mov    -0x24(%rbp),%edi
mov    $0x2,%esi
call   1040 <sem_signal@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1040 <sem_signal@plt>
mov    -0x4(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    -0x4(%rbp),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    120e <consume+0xce>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    1213 <consume+0xd3>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1173 <consume+0x33>
mov    -0x4(%rbp),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consume>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r13d
lea    0xea2(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %ebx,%ebx
jmp    117c <consume+0x3c>
nopl   0x0(%rax)
add    $0x1,%ebx
cmp    $0xc,%ebx
je     1210 <consume+0xd0>
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <sem_wait@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1030 <sem_wait@plt>
movslq (%r14),%rax
mov    (%r15,%rax,4),%r12d
add    $0x1,%eax
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %eax,(%r14)
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <sem_signal@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <sem_signal@plt>
lea    0xe43(%rip),%rdi
mov    %r13d,%esi
mov    %ebx,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    (%rbx,%r13,1),%eax
imul   $0xcccccccd,%eax,%eax
add    $0x19999999,%eax
cmp    $0x33333332,%eax
ja     1170 <consume+0x30>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    1170 <consume+0x30>
lea    0xe2c(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Consumer of ID %d begins.\0A\00"
@.str.1 = [40 x i8] c"Consumer of ID %d : element %2d == %2d\0A\00"
@.str.2 = [33 x i8] c"Consumer of ID %d has finished.\0A\00"
define void @consume(i32 %0, i32* %1, i32* %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32 %4, i32* %10
%13 = load i32, i32* %6
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 0, i32* %11
br label %15
15: ; preds = %50, %5
%16 = load i32, i32* %11
%17 = icmp slt i32 %16, 12
br i1 %17, label %18, label %53
18: ; preds = %15
%19 = load i32, i32* %10
%20 = call i32 @sem_wait(i32 %19, i32 0)
%21 = load i32, i32* %10
%22 = call i32 @sem_wait(i32 %21, i32 2)
%23 = load i32*, i32** %7
%24 = load i32*, i32** %8
%25 = load i32, i32* %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %23, i64 %26
%28 = load i32, i32* %27
store i32 %28, i32* %12
%29 = load i32*, i32** %8
%30 = load i32, i32* %29
%31 = add nsw i32 %30, 1
%32 = srem i32 %31, 5
%33 = load i32*, i32** %8
store i32 %32, i32* %33
%34 = load i32, i32* %10
%35 = call i32 @sem_signal(i32 %34, i32 2)
%36 = load i32, i32* %10
%37 = call i32 @sem_signal(i32 %36, i32 1)
%38 = load i32, i32* %6
%39 = load i32, i32* %11
%40 = load i32, i32* %12
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %39, i32 %40)
%42 = load i32, i32* %11
%43 = load i32, i32* %6
%44 = add nsw i32 %42, %43
%45 = srem i32 %44, 5
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %18
%48 = call i32 @sleep(i32 1)
br label %49
49: ; preds = %47, %18
br label %50
50: ; preds = %49
%51 = load i32, i32* %11
%52 = add nsw i32 %51, 1
store i32 %52, i32* %11
br label %15
53: ; preds = %15
%54 = load i32, i32* %6
%55 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %54)
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"Consumer of ID %d begins.\0A\00"
@.str.1 = [40 x i8] c"Consumer of ID %d : element %2d == %2d\0A\00"
@.str.2 = [33 x i8] c"Consumer of ID %d has finished.\0A\00"
define void @consume(i32 %0, i32* nocapture %1, i32* nocapture %2, i32* nocapture readnone %3, i32 %4) {
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %7
7: ; preds = %5, %25
%8 = phi i32 [ 0, %5 ], [ %26, %25 ]
%9 = tail call i32 @sem_wait(i32 %4, i32 0)
%10 = tail call i32 @sem_wait(i32 %4, i32 2)
%11 = load i32, i32* %2
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i32, i32* %1, i64 %12
%14 = load i32, i32* %13
%15 = add nsw i32 %11, 1
%16 = srem i32 %15, 5
store i32 %16, i32* %2
%17 = tail call i32 @sem_signal(i32 %4, i32 2)
%18 = tail call i32 @sem_signal(i32 %4, i32 1)
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %8, i32 %14)
%20 = add nsw i32 %8, %0
%21 = srem i32 %20, 5
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %7
%24 = tail call i32 @sleep(i32 1)
br label %25
25: ; preds = %7, %23
%26 = add nuw nsw i32 %8, 1
%27 = icmp eq i32 %26, 12
br i1 %27, label %28, label %7
28: ; preds = %25
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %0)
ret void
}