#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);
int /*<<< orphan*/ sleep(int);
void read_memory(int *buffer, int *in, int *out) {
  int i;
  int value;
  for (i = 0; i < 10; i++) {
    sleep(1);
    while (*in == *out)
      ;
    value = buffer[*out];
    *out = (*out + 1) % 5;
    printf("Child: element %2d == %2d read from the buffer.\n", i, value);
  }
}


x86 assembly (clang, -O0):
<read_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xa,-0x1c(%rbp)
jge    11ae <read_memory+0x8e>
mov    $0x1,%edi
call   1040 <sleep@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jne    1166 <read_memory+0x46>
jmp    114f <read_memory+0x2f>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%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    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113b <read_memory+0x1b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_memory>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebp,%ebp
lea    0xec6(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1040 <sleep@plt>
movslq (%rbx),%rax
cmp    %eax,(%r14)
je     11a0 <read_memory+0x80>
mov    (%r15,%rax,4),%edx
add    $0x1,%eax
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rsi
shr    $0x3f,%rsi
sar    $0x21,%rcx
add    %esi,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %eax,(%rbx)
mov    %r12,%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    $0xa,%ebp
jne    1140 <read_memory+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
jmp    11a0 <read_memory+0x80>

LLVM IR (-O0):
@.str = [49 x i8] c"Child: element %2d == %2d read from the buffer.\0A\00"
define void @read_memory(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i32 0, i32* %7
br label %9
9: ; preds = %36, %3
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 10
br i1 %11, label %12, label %39
12: ; preds = %9
%13 = call i32 @sleep(i32 1)
br label %14
14: ; preds = %20, %12
%15 = load i32*, i32** %5
%16 = load i32, i32* %15
%17 = load i32*, i32** %6
%18 = load i32, i32* %17
%19 = icmp eq i32 %16, %18
br i1 %19, label %20, label %21
20: ; preds = %14
br label %14
21: ; preds = %14
%22 = load i32*, i32** %4
%23 = load i32*, i32** %6
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %22, i64 %25
%27 = load i32, i32* %26
store i32 %27, i32* %8
%28 = load i32*, i32** %6
%29 = load i32, i32* %28
%30 = add nsw i32 %29, 1
%31 = srem i32 %30, 5
%32 = load i32*, i32** %6
store i32 %31, i32* %32
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %34)
br label %36
36: ; preds = %21
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %9
39: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [49 x i8] c"Child: element %2d == %2d read from the buffer.\0A\00"
define void @read_memory(i32* nocapture %0, i32* nocapture %1, i32* nocapture %2) {
br label %4
4: ; preds = %3, %11
%5 = phi i32 [ 0, %3 ], [ %18, %11 ]
%6 = tail call i32 @sleep(i32 1)
%7 = load i32, i32* %1
%8 = load i32, i32* %2
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %11
10: ; preds = %4, %10
br label %10
11: ; preds = %4
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* %13
%15 = add nsw i32 %8, 1
%16 = srem i32 %15, 5
store i32 %16, i32* %2
%17 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %14)
%18 = add nuw nsw i32 %5, 1
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %4
20: ; preds = %11
ret void
}