#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*/ getFromBuffer(int *, int *);
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sleepRandom();
int /*<<< orphan*/ srand(int);
int time(int /*<<< orphan*/ *);
void consumer() {
  srand(time(NULL) ^ (getpid() << 16));
  sleepRandom();
  printf("Consumer %d started\n", getpid());
  while (1) {
    int val, pid;
    getFromBuffer(&val, &pid);
    if (val == pid && pid == -1)
      break;
    printf("Consumer %d is consuming (%d, %d)\n", getpid(), val, pid);
    sleep(val);
    printf("Consumer %d finished consuming (%d, %d)\n", getpid(), val, pid);
  }
  printf("Consumer %d finished\n", getpid());
}


x86 assembly (clang, -O0):
<consumer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%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>
lea    -0x4(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <getFromBuffer@plt>
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    11de <consumer+0x6e>
cmpl   $0xffffffff,-0x8(%rbp)
jne    11de <consumer+0x6e>
jmp    1225 <consumer+0xb5>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11b6 <consumer+0x46>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consumer>:
push   %r15
push   %r14
push   %r12
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    0xe57(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rdi
lea    0x4(%rsp),%rsi
call   1060 <getFromBuffer@plt>
mov    0x4(%rsp),%eax
and    (%rsp),%eax
cmp    $0xffffffff,%eax
je     1243 <consumer+0xd3>
lea    0xe43(%rip),%r14
lea    0xe5f(%rip),%r15
mov    %rsp,%r12
lea    0x4(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    (%rsp),%edx
mov    0x4(%rsp),%ecx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    (%rsp),%edx
mov    0x4(%rsp),%ecx
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <getFromBuffer@plt>
mov    0x4(%rsp),%eax
and    (%rsp),%eax
cmp    $0xffffffff,%eax
jne    11f0 <consumer+0x80>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe10(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Consumer %d started\0A\00"
@.str.1 = [35 x i8] c"Consumer %d is consuming (%d, %d)\0A\00"
@.str.2 = [41 x i8] c"Consumer %d finished consuming (%d, %d)\0A\00"
@.str.3 = [22 x i8] c"Consumer %d finished\0A\00"
define void @consumer() {
%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)
br label %11
11: ; preds = %0, %20
%12 = call i32 @getFromBuffer(i32* %1, i32* %2)
%13 = load i32, i32* %1
%14 = load i32, i32* %2
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %16
br label %31
20: ; preds = %16, %11
%21 = call i32 (...) @getpid()
%22 = load i32, i32* %1
%23 = load i32, i32* %2
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22, i32 %23)
%25 = load i32, i32* %1
%26 = call i32 @sleep(i32 %25)
%27 = call i32 (...) @getpid()
%28 = load i32, i32* %1
%29 = load i32, i32* %2
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %27, i32 %28, i32 %29)
br label %11
31: ; preds = %19
%32 = call i32 (...) @getpid()
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %32)
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"Consumer %d started\0A\00"
@.str.1 = [35 x i8] c"Consumer %d is consuming (%d, %d)\0A\00"
@.str.2 = [41 x i8] c"Consumer %d finished consuming (%d, %d)\0A\00"
@.str.3 = [22 x i8] c"Consumer %d finished\0A\00"
define void @consumer() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32 @time(i32* null)
%4 = tail call i32 (...) @getpid()
%5 = shl i32 %4, 16
%6 = xor i32 %5, %3
%7 = tail call i32 @srand(i32 %6)
%8 = tail call i32 (...) @sleepRandom()
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = bitcast i32* %1 to i8*
%12 = bitcast i32* %2 to i8*
%13 = call i32 @getFromBuffer(i32* %1, i32* %2)
%14 = load i32, i32* %1
%15 = load i32, i32* %2
%16 = and i32 %15, %14
%17 = icmp eq i32 %16, -1
br i1 %17, label %34, label %18
18: ; preds = %0, %18
%19 = call i32 (...) @getpid()
%20 = load i32, i32* %1
%21 = load i32, i32* %2
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20, i32 %21)
%23 = load i32, i32* %1
%24 = call i32 @sleep(i32 %23)
%25 = call i32 (...) @getpid()
%26 = load i32, i32* %1
%27 = load i32, i32* %2
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26, i32 %27)
%29 = call i32 @getFromBuffer(i32* %1, i32* %2)
%30 = load i32, i32* %1
%31 = load i32, i32* %2
%32 = and i32 %31, %30
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %18
34: ; preds = %18, %0
%35 = call i32 (...) @getpid()
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %35)
ret void
}