#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 */
typedef int /*<<< orphan*/ pthread_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  void *(*)(void *), int *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
void *criar_threads_de_forma_ordenada(void *quantidade) {
  int valor = *((int *)quantidade);
  printf("Thread %d\n", valor);
  pthread_t thr;
  if (valor == 10) {
    return NULL;
  }
  valor++;
  pthread_create(&thr, NULL, criar_threads_de_forma_ordenada, &valor);
  pthread_join(thr, NULL);
  return NULL;
}


x86 assembly (clang, -O0):
<criar_threads_de_forma_ordenada>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xeb1(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0xa,-0x14(%rbp)
jne    116b <criar_threads_de_forma_ordenada+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    11a0 <criar_threads_de_forma_ordenada+0x70>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    0x2e65(%rip),%rdx
lea    -0x14(%rbp),%rcx
call   1040 <pthread_create@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_join@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<criar_threads_de_forma_ordenada>:
push   %rbx
sub    $0x10,%rsp
mov    (%rdi),%ebx
lea    0xec2(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
cmp    $0xa,%ebx
je     1174 <criar_threads_de_forma_ordenada+0x44>
add    $0x1,%ebx
mov    %ebx,0xc(%rsp)
mov    0x2e90(%rip),%rdx
lea    0x8(%rsp),%rdi
lea    0xc(%rsp),%rcx
xor    %esi,%esi
call   1040 <pthread_create@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1050 <pthread_join@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"Thread %d\0A\00"
define i8* @criar_threads_de_forma_ordenada(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 10
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %20
14: ; preds = %1
%15 = load i32, i32* %4
%16 = add nsw i32 %15, 1
store i32 %16, i32* %4
%17 = call i32 @pthread_create(i32* %5, i32* null, i8* (i8*)* @criar_threads_de_forma_ordenada, i32* %4)
%18 = load i32, i32* %5
%19 = call i32 @pthread_join(i32 %18, i32* null)
store i8* null, i8** %2
br label %20
20: ; preds = %14, %13
%21 = load i8*, i8** %2
ret i8* %21
}

LLVM IR (-O2):
@.str = [11 x i8] c"Thread %d\0A\00"
define noalias i8* @criar_threads_de_forma_ordenada(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = bitcast i32* %3 to i8*
%9 = icmp eq i32 %6, 10
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = add nsw i32 %6, 1
store i32 %11, i32* %2
%12 = call i32 @pthread_create(i32* %3, i32* null, i8* (i8*)* @criar_threads_de_forma_ordenada, i32* %2)
%13 = load i32, i32* %3
%14 = call i32 @pthread_join(i32 %13, i32* null)
br label %15
15: ; preds = %1, %10
ret i8* null
}