#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 generateProduct();
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ putIntoBuffer(int, int);
int /*<<< orphan*/ sleepRandom();
int /*<<< orphan*/ srand(int);
int time(int /*<<< orphan*/ *);
void producer() {
  srand(time(NULL) ^ (getpid() << 16));
  sleepRandom();
  printf("Producer %d started\n", getpid());
  for (int i = 0; i < 3; i++) {
    int p = generateProduct();
    printf("Producer %d generated and is going to put (%d, %d) into the "
           "buffer\n",
           getpid(), p, getpid());
    putIntoBuffer(p, getpid());
    printf("Producer %d finished putting (%d, %d) into the buffer\n", getpid(),
           p, getpid());
  }
  printf("Producer %d finished\n", getpid());
}


x86 assembly (clang, -O0):
<producer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0xc(%rbp),%edi
shl    $0x10,%eax
xor    %eax,%edi
call   1070 <srand@plt>
mov    $0x0,%al
call   1030 <sleepRandom@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x3,-0x4(%rbp)
jge    1250 <producer+0xe0>
mov    $0x0,%al
call   1090 <generateProduct@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%esi
call   1060 <putIntoBuffer@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    %eax,%ecx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <producer+0x4d>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<producer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
shl    $0x10,%eax
xor    %eax,%ebx
mov    %ebx,%edi
call   1070 <srand@plt>
xor    %eax,%eax
call   1030 <sleepRandom@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe58(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <generateProduct@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe44(%rip),%r14
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1060 <putIntoBuffer@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe51(%rip),%r15
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <generateProduct@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1060 <putIntoBuffer@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1090 <generateProduct@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1060 <putIntoBuffer@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdb8(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"Producer %d started\0A\00"
@.str.1 = [68 x i8] c"Producer %d generated and is going to put (%d, %d) into the buffer\0A\00"
@.str.2 = [55 x i8] c"Producer %d finished putting (%d, %d) into the buffer\0A\00"
@.str.3 = [22 x i8] c"Producer %d finished\0A\00"
define void @producer() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 @time(i32* null)
%4 = call i32 (...) @getpid()
%5 = shl i32 %4, 16
%6 = xor i32 %3, %5
%7 = call i32 @srand(i32 %6)
%8 = call i32 (...) @sleepRandom()
%9 = call i32 (...) @getpid()
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 0, i32* %1
br label %11
11: ; preds = %27, %0
%12 = load i32, i32* %1
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = call i32 (...) @generateProduct()
store i32 %15, i32* %2
%16 = call i32 (...) @getpid()
%17 = load i32, i32* %2
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17, i32 %18)
%20 = load i32, i32* %2
%21 = call i32 (...) @getpid()
%22 = call i32 @putIntoBuffer(i32 %20, i32 %21)
%23 = call i32 (...) @getpid()
%24 = load i32, i32* %2
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %24, i32 %25)
br label %27
27: ; preds = %14
%28 = load i32, i32* %1
%29 = add nsw i32 %28, 1
store i32 %29, i32* %1
br label %11
30: ; preds = %11
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %31)
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"Producer %d started\0A\00"
@.str.1 = [68 x i8] c"Producer %d generated and is going to put (%d, %d) into the buffer\0A\00"
@.str.2 = [55 x i8] c"Producer %d finished putting (%d, %d) into the buffer\0A\00"
@.str.3 = [22 x i8] c"Producer %d finished\0A\00"
define void @producer() {
%1 = tail call i32 @time(i32* null)
%2 = tail call i32 (...) @getpid()
%3 = shl i32 %2, 16
%4 = xor i32 %3, %1
%5 = tail call i32 @srand(i32 %4)
%6 = tail call i32 (...) @sleepRandom()
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 (...) @generateProduct()
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %9, i32 %11)
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 @putIntoBuffer(i32 %9, i32 %13)
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (...) @getpid()
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %9, i32 %16)
%18 = tail call i32 (...) @generateProduct()
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %18, i32 %20)
%22 = tail call i32 (...) @getpid()
%23 = tail call i32 @putIntoBuffer(i32 %18, i32 %22)
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %18, i32 %25)
%27 = tail call i32 (...) @generateProduct()
%28 = tail call i32 (...) @getpid()
%29 = tail call i32 (...) @getpid()
%30 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %27, i32 %29)
%31 = tail call i32 (...) @getpid()
%32 = tail call i32 @putIntoBuffer(i32 %27, i32 %31)
%33 = tail call i32 (...) @getpid()
%34 = tail call i32 (...) @getpid()
%35 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %27, i32 %34)
%36 = tail call i32 (...) @getpid()
%37 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %36)
ret void
}