#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 */
struct my_custom_sem {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ down(struct my_custom_sem *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ up(struct my_custom_sem *);
void consumer(struct my_custom_sem *empty, struct my_custom_sem *full,
              struct my_custom_sem *lock, int n, int *buffer_pointer,
              int cons_num) {
  int consumed_number, consumed_index;
  int *consumer_pointer;
  consumer_pointer = buffer_pointer - 1;
  while (1) {
    down(full);
    down(lock);
    consumed_index = *consumer_pointer;
    consumed_number = buffer_pointer[consumed_index];
    *consumer_pointer = (consumed_index + 1) % n;
    printf("Consumer '%d' Consumed: %d\n", cons_num, consumed_number);
    up(lock);
    up(empty);
  }
  return;
}


x86 assembly (clang, -O0):
<consumer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <down@plt>
mov    -0x18(%rbp),%rdi
call   1040 <down@plt>
mov    -0x40(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
cltd
idivl  -0x1c(%rbp)
mov    -0x40(%rbp),%rax
mov    %edx,(%rax)
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
lea    0xe5c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <up@plt>
mov    -0x8(%rbp),%rdi
call   1050 <up@plt>
jmp    115b <consumer+0x2b>

x86 assembly (clang, -O2):
<consumer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8,%rbx
mov    %ecx,%r15d
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%r13
nopl   (%rax)
mov    %r12,%rdi
call   1040 <down@plt>
mov    %rbp,%rdi
call   1040 <down@plt>
movslq -0x4(%rbx),%rax
mov    (%rbx,%rax,4),%ecx
add    $0x1,%eax
cltd
idiv   %r15d
mov    %edx,-0x4(%rbx)
lea    0xe88(%rip),%rdi
mov    %r14d,%esi
mov    %ecx,%edx
call   1030 <printf@plt>
mov    %rbp,%rdi
call   1050 <up@plt>
mov    %r13,%rdi
call   1050 <up@plt>
jmp    1150 <consumer+0x20>

LLVM IR (-O0):
%struct.my_custom_sem = type { i32 }
@.str = [28 x i8] c"Consumer '%d' Consumed: %d\0A\00"
define void @consumer(%struct.my_custom_sem* %0, %struct.my_custom_sem* %1, %struct.my_custom_sem* %2, i32 %3, i32* %4, i32 %5) {
%7 = alloca %struct.my_custom_sem*
%8 = alloca %struct.my_custom_sem*
%9 = alloca %struct.my_custom_sem*
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32*
store %struct.my_custom_sem* %0, %struct.my_custom_sem** %7
store %struct.my_custom_sem* %1, %struct.my_custom_sem** %8
store %struct.my_custom_sem* %2, %struct.my_custom_sem** %9
store i32 %3, i32* %10
store i32* %4, i32** %11
store i32 %5, i32* %12
%16 = load i32*, i32** %11
%17 = getelementptr inbounds i32, i32* %16, i64 -1
store i32* %17, i32** %15
br label %18
18: ; preds = %6, %18
%19 = load %struct.my_custom_sem*, %struct.my_custom_sem** %8
%20 = call i32 @down(%struct.my_custom_sem* %19)
%21 = load %struct.my_custom_sem*, %struct.my_custom_sem** %9
%22 = call i32 @down(%struct.my_custom_sem* %21)
%23 = load i32*, i32** %15
%24 = load i32, i32* %23
store i32 %24, i32* %14
%25 = load i32*, i32** %11
%26 = load i32, i32* %14
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %28
store i32 %29, i32* %13
%30 = load i32, i32* %14
%31 = add nsw i32 %30, 1
%32 = load i32, i32* %10
%33 = srem i32 %31, %32
%34 = load i32*, i32** %15
store i32 %33, i32* %34
%35 = load i32, i32* %12
%36 = load i32, i32* %13
%37 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36)
%38 = load %struct.my_custom_sem*, %struct.my_custom_sem** %9
%39 = call i32 @up(%struct.my_custom_sem* %38)
%40 = load %struct.my_custom_sem*, %struct.my_custom_sem** %7
%41 = call i32 @up(%struct.my_custom_sem* %40)
br label %18
}

LLVM IR (-O2):
%struct.my_custom_sem = type { i32 }
@.str = [28 x i8] c"Consumer '%d' Consumed: %d\0A\00"
define void @consumer(%struct.my_custom_sem* %0, %struct.my_custom_sem* %1, %struct.my_custom_sem* %2, i32 %3, i32* nocapture %4, i32 %5) {
%7 = getelementptr inbounds i32, i32* %4, i64 -1
br label %8
8: ; preds = %8, %6
%9 = tail call i32 @down(%struct.my_custom_sem* %1)
%10 = tail call i32 @down(%struct.my_custom_sem* %2)
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i32, i32* %4, i64 %12
%14 = load i32, i32* %13
%15 = add nsw i32 %11, 1
%16 = srem i32 %15, %3
store i32 %16, i32* %7
%17 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %14)
%18 = tail call i32 @up(%struct.my_custom_sem* %2)
%19 = tail call i32 @up(%struct.my_custom_sem* %0)
br label %8
}