#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 producer(struct my_custom_sem *empty, struct my_custom_sem *full,
              struct my_custom_sem *lock, int n, int *buffer_pointer,
              int prod_num) {
  int produced_number;
  int *producer_pointer;
  producer_pointer = buffer_pointer - 2;
  while (1) {
    down(empty);
    down(lock);
    produced_number = *producer_pointer;
    buffer_pointer[produced_number] = produced_number;
    *producer_pointer = (produced_number + 1) % n;
    printf("Producer '%d' Produced: %d\n", prod_num, produced_number);
    up(lock);
    up(full);
  }
  return;
}


x86 assembly (clang, -O0):
<producer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    $0xfffffffffffffff8,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <down@plt>
mov    -0x18(%rbp),%rdi
call   1040 <down@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edx
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
cltd
idivl  -0x1c(%rbp)
mov    -0x38(%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    -0x10(%rbp),%rdi
call   1050 <up@plt>
jmp    115b <producer+0x2b>

x86 assembly (clang, -O2):
<producer>:
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    %r13,%rdi
call   1040 <down@plt>
mov    %rbp,%rdi
call   1040 <down@plt>
movslq -0x8(%rbx),%rcx
mov    %ecx,(%rbx,%rcx,4)
lea    0x1(%rcx),%eax
cltd
idiv   %r15d
mov    %edx,-0x8(%rbx)
lea    0xe88(%rip),%rdi
mov    %r14d,%esi
mov    %ecx,%edx
call   1030 <printf@plt>
mov    %rbp,%rdi
call   1050 <up@plt>
mov    %r12,%rdi
call   1050 <up@plt>
jmp    1150 <producer+0x20>

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

LLVM IR (-O2):
%struct.my_custom_sem = type { i32 }
@.str = [28 x i8] c"Producer '%d' Produced: %d\0A\00"
define void @producer(%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 -2
br label %8
8: ; preds = %8, %6
%9 = tail call i32 @down(%struct.my_custom_sem* %0)
%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
store i32 %11, i32* %13
%14 = add nsw i32 %11, 1
%15 = srem i32 %14, %3
store i32 %15, i32* %7
%16 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %11)
%17 = tail call i32 @up(%struct.my_custom_sem* %2)
%18 = tail call i32 @up(%struct.my_custom_sem* %1)
br label %8
}