#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 buffer {
  int dummy;
};

/* Variables and functions */
scalar_t__ pop(struct buffer *);
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ sleep(int);
void *dequeue(void *ptr) {
  struct buffer *buffer = (struct buffer *)ptr;
  int i = 0;
  int wait_time, data;
  while (i < 100) {
    wait_time = rand() % 20;
    sleep(wait_time);
    data = ((int)pop(buffer));
    printf("Dequeue element, %d\n", data);
    i++;
  }
}


x86 assembly (clang, -O0):
<dequeue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jge    11a8 <dequeue+0x68>
mov    $0x0,%al
call   1060 <rand@plt>
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <sleep@plt>
mov    -0x18(%rbp),%rdi
call   1040 <pop@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe6b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115b <dequeue+0x1b>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dequeue>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x64,%ebp
lea    0xead(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1060 <rand@plt>
movslq %eax,%rdi
imul   $0x66666667,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
shl    $0x2,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
call   1040 <pop@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
add    $0xffffffff,%ebp
jne    1160 <dequeue+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
@.str = [21 x i8] c"Dequeue element, %d\0A\00"
define i8* @dequeue(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.buffer*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to %struct.buffer*
store %struct.buffer* %9, %struct.buffer** %4
store i32 0, i32* %5
br label %10
10: ; preds = %13, %1
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 100
br i1 %12, label %13, label %25
13: ; preds = %10
%14 = call i32 (...) @rand()
%15 = srem i32 %14, 20
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = call i32 @sleep(i32 %16)
%18 = load %struct.buffer*, %struct.buffer** %4
%19 = call i64 @pop(%struct.buffer* %18)
%20 = trunc i64 %19 to i32
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %10
25: ; preds = %10
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
@.str = [21 x i8] c"Dequeue element, %d\0A\00"
define noalias i8* @dequeue(i8* %0) {
%2 = bitcast i8* %0 to %struct.buffer*
br label %3
3: ; preds = %1, %3
%4 = phi i32 [ 0, %1 ], [ %11, %3 ]
%5 = tail call i32 (...) @rand()
%6 = srem i32 %5, 20
%7 = tail call i32 @sleep(i32 %6)
%8 = tail call i64 @pop(%struct.buffer* %2)
%9 = trunc i64 %8 to i32
%10 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = add nuw nsw i32 %4, 1
%12 = icmp eq i32 %11, 100
br i1 %12, label %13, label %3
13: ; preds = %3
ret i8* undef
}