#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*/ dequeue(int *);
int /*<<< orphan*/ enqueue(int);
int rand();
void *my_thread(void *ptr) {
  int num = *(int *)ptr;
  int ret = 0;
  int tot = 2 * num;
  for (int i = 0; i < tot; i++) {
    if ((rand() % 2 + 1) > 1) {
      enqueue(i);
    } else {
      dequeue(&ret);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<my_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ac <my_thread+0x7c>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
cmp    $0x1,%edx
jle    1190 <my_thread+0x60>
mov    -0x18(%rbp),%edi
call   1040 <enqueue@plt>
jmp    1199 <my_thread+0x69>
lea    -0x10(%rbp),%rdi
call   1030 <dequeue@plt>
jmp    119e <my_thread+0x6e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115c <my_thread+0x2c>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_thread>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    (%rdi),%eax
movl   $0x0,0xc(%rsp)
test   %eax,%eax
jle    1189 <my_thread+0x59>
add    %eax,%eax
cmp    $0x2,%eax
mov    $0x1,%ebx
cmovge %eax,%ebx
lea    0xc(%rsp),%r14
xor    %ebp,%ebp
jmp    116f <my_thread+0x3f>
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <dequeue@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
je     1189 <my_thread+0x59>
xor    %eax,%eax
call   1050 <rand@plt>
and    $0x80000001,%eax
cmp    $0x1,%eax
jne    1160 <my_thread+0x30>
mov    %ebp,%edi
call   1040 <enqueue@plt>
jmp    1168 <my_thread+0x38>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @my_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
store i32 %9, i32* %3
store i32 0, i32* %4
%10 = load i32, i32* %3
%11 = mul nsw i32 2, %10
store i32 %11, i32* %5
store i32 0, i32* %6
br label %12
12: ; preds = %27, %1
%13 = load i32, i32* %6
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = call i32 (...) @rand()
%18 = srem i32 %17, 2
%19 = add nsw i32 %18, 1
%20 = icmp sgt i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %6
%23 = call i32 @enqueue(i32 %22)
br label %26
24: ; preds = %16
%25 = call i32 @dequeue(i32* %4)
br label %26
26: ; preds = %24, %21
br label %27
27: ; preds = %26
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %12
30: ; preds = %12
ret i8* null
}

LLVM IR (-O2):
define noalias i8* @my_thread(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%6 = icmp sgt i32 %4, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = shl nuw i32 %4, 1
%9 = call i32 @llvm.smax.i32(i32 %8, i32 1)
br label %11
10: ; preds = %20, %1
ret i8* null
11: ; preds = %7, %20
%12 = phi i32 [ %21, %20 ], [ 0, %7 ]
%13 = call i32 (...) @rand()
%14 = and i32 %13, -2147483647
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @enqueue(i32 %12)
br label %20
18: ; preds = %11
%19 = call i32 @dequeue(i32* %2)
br label %20
20: ; preds = %16, %18
%21 = add nuw nsw i32 %12, 1
%22 = icmp eq i32 %21, %9
br i1 %22, label %10, label %11
}