#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 thread_w_arguments;
typedef int second_step_threads_arguments;
typedef int pthread_t;

/* Variables and functions */
int /*<<< orphan*/ create_second_step_threads(int *, int *, int *, int *, int);
int /*<<< orphan*/ create_third_step_threads(int *, int *, int *, int *, int);
int /*<<< orphan*/ create_threads_w(int *, int *, int *, int *, int);
int find_integers_amount(int, char **);
int /*<<< orphan*/ free(int *);
int *initialize_second_threads_arguments(int);
int *initialize_thread_vectors(int);
int *initialize_thread_w_arguments_vectors(int);
int *initialize_vectors(int);
int /*<<< orphan*/ join_threads(int *, int);
int /*<<< orphan*/ print_vector(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int *tranlaste_arguments_to_vector(int, char **);
int main(int argc, char *argv[]) {
  int integers_amount;
  int second_step_threads_amount;
  int *vector_v;
  int *vector_w;
  pthread_t *threads_w;
  pthread_t *second_step_threads;
  struct thread_w_arguments *thread_w_args;
  struct second_step_threads_arguments *second_threads_args;
  if (argc < 3) {
    printf("número de argumentos inválido!\n");
    return 0;
  } else {
    integers_amount = find_integers_amount(argc, argv);
    second_step_threads_amount = (integers_amount * (integers_amount - 1) / 2);
    printf("integers_amount: %d\n", integers_amount);
    printf("Fator etapa 2: %d\n", second_step_threads_amount);
    vector_v = tranlaste_arguments_to_vector(integers_amount, argv);
  }
  vector_w = initialize_vectors(integers_amount);
  threads_w = initialize_thread_vectors(integers_amount);
  thread_w_args = initialize_thread_w_arguments_vectors(integers_amount);
  create_threads_w(threads_w, thread_w_args, vector_v, vector_w,
                   integers_amount);
  join_threads(threads_w, integers_amount);
  printf("Number of input values = %d\n", integers_amount);
  printf("Input values x = ");
  print_vector(vector_v, integers_amount);
  printf("After initialization w = ");
  print_vector(vector_w, integers_amount);
  second_step_threads = initialize_thread_vectors(second_step_threads_amount);
  second_threads_args =
      initialize_second_threads_arguments(second_step_threads_amount);
  create_second_step_threads(second_step_threads, second_threads_args, vector_v,
                             vector_w, integers_amount);
  join_threads(second_step_threads, second_step_threads_amount);
  printf("After step 2\n");
  printf("w = ");
  print_vector(vector_w, integers_amount);
  create_third_step_threads(threads_w, thread_w_args, vector_v, vector_w,
                            integers_amount);
  join_threads(threads_w, integers_amount);
  free(threads_w);
  free(thread_w_args);
  free(second_step_threads);
  free(second_threads_args);
  free(vector_v);
  free(vector_w);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    120a <main+0x3a>
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13b7 <main+0x1e7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10b0 <find_integers_amount@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
imul   %ecx,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <tranlaste_arguments_to_vector@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   10d0 <initialize_vectors@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   10a0 <initialize_thread_vectors@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   10e0 <initialize_thread_w_arguments_vectors@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x14(%rbp),%r8d
call   1050 <create_threads_w@plt>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10c0 <join_threads@plt>
mov    -0x14(%rbp),%esi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <print_vector@plt>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <print_vector@plt>
mov    -0x18(%rbp),%edi
call   10a0 <initialize_thread_vectors@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <initialize_second_threads_arguments@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x14(%rbp),%r8d
call   1070 <create_second_step_threads@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   10c0 <join_threads@plt>
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <print_vector@plt>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x14(%rbp),%r8d
call   10f0 <create_third_step_threads@plt>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10c0 <join_threads@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jg     1204 <main+0x24>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
jmp    1375 <main+0x195>
mov    %rsi,%r14
call   10c0 <find_integers_amount@plt>
mov    %eax,%ebx
lea    -0x1(%rbx),%eax
imul   %ebx,%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
lea    0xddc(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xde1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <tranlaste_arguments_to_vector@plt>
mov    %rax,%r14
mov    %ebx,%edi
call   10e0 <initialize_vectors@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   10b0 <initialize_thread_vectors@plt>
mov    %rax,%r12
mov    %ebx,%edi
call   10f0 <initialize_thread_w_arguments_vectors@plt>
mov    %rax,0x10(%rsp)
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
call   1060 <create_threads_w@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   10d0 <join_threads@plt>
lea    0xd99(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   10a0 <print_vector@plt>
lea    0xda0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <print_vector@plt>
mov    %ebp,%edi
call   10b0 <initialize_thread_vectors@plt>
mov    %rax,%r13
mov    %ebp,%edi
call   1090 <initialize_second_threads_arguments@plt>
mov    %rax,0x8(%rsp)
mov    %r13,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
call   1080 <create_second_step_threads@plt>
mov    %r13,%rdi
mov    %ebp,%esi
call   10d0 <join_threads@plt>
lea    0xd6e(%rip),%rdi
call   1040 <puts@plt>
lea    0xd5d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <print_vector@plt>
mov    %r12,%rdi
mov    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %ebx,%r8d
call   1100 <create_third_step_threads@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   10d0 <join_threads@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.thread_w_arguments = type opaque
%struct.second_step_threads_arguments = type opaque
@.str = [34 x i8] c"n\C3\BAmero de argumentos inv\C3\A1lido!\0A\00"
@.str.1 = [21 x i8] c"integers_amount: %d\0A\00"
@.str.2 = [19 x i8] c"Fator etapa 2: %d\0A\00"
@.str.3 = [29 x i8] c"Number of input values = %d\0A\00"
@.str.4 = [18 x i8] c"Input values x = \00"
@.str.5 = [26 x i8] c"After initialization w = \00"
@.str.6 = [14 x i8] c"After step 2\0A\00"
@.str.7 = [5 x i8] c"w = \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca %struct.thread_w_arguments*
%13 = alloca %struct.second_step_threads_arguments*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %106
18: ; preds = %2
%19 = load i32, i32* %4
%20 = load i8**, i8*** %5
%21 = call i32 @find_integers_amount(i32 %19, i8** %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 1
%25 = mul nsw i32 %22, %24
%26 = sdiv i32 %25, 2
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %7
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %6
%32 = load i8**, i8*** %5
%33 = call i32* @tranlaste_arguments_to_vector(i32 %31, i8** %32)
store i32* %33, i32** %8
br label %34
34: ; preds = %18
%35 = load i32, i32* %6
%36 = call i32* @initialize_vectors(i32 %35)
store i32* %36, i32** %9
%37 = load i32, i32* %6
%38 = call i32* @initialize_thread_vectors(i32 %37)
store i32* %38, i32** %10
%39 = load i32, i32* %6
%40 = call i32* @initialize_thread_w_arguments_vectors(i32 %39)
%41 = bitcast i32* %40 to %struct.thread_w_arguments*
store %struct.thread_w_arguments* %41, %struct.thread_w_arguments** %12
%42 = load i32*, i32** %10
%43 = load %struct.thread_w_arguments*, %struct.thread_w_arguments** %12
%44 = bitcast %struct.thread_w_arguments* %43 to i32*
%45 = load i32*, i32** %8
%46 = load i32*, i32** %9
%47 = load i32, i32* %6
%48 = call i32 @create_threads_w(i32* %42, i32* %44, i32* %45, i32* %46, i32 %47)
%49 = load i32*, i32** %10
%50 = load i32, i32* %6
%51 = call i32 @join_threads(i32* %49, i32 %50)
%52 = load i32, i32* %6
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32*, i32** %8
%56 = load i32, i32* %6
%57 = call i32 @print_vector(i32* %55, i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32*, i32** %9
%60 = load i32, i32* %6
%61 = call i32 @print_vector(i32* %59, i32 %60)
%62 = load i32, i32* %7
%63 = call i32* @initialize_thread_vectors(i32 %62)
store i32* %63, i32** %11
%64 = load i32, i32* %7
%65 = call i32* @initialize_second_threads_arguments(i32 %64)
%66 = bitcast i32* %65 to %struct.second_step_threads_arguments*
store %struct.second_step_threads_arguments* %66, %struct.second_step_threads_arguments** %13
%67 = load i32*, i32** %11
%68 = load %struct.second_step_threads_arguments*, %struct.second_step_threads_arguments** %13
%69 = bitcast %struct.second_step_threads_arguments* %68 to i32*
%70 = load i32*, i32** %8
%71 = load i32*, i32** %9
%72 = load i32, i32* %6
%73 = call i32 @create_second_step_threads(i32* %67, i32* %69, i32* %70, i32* %71, i32 %72)
%74 = load i32*, i32** %11
%75 = load i32, i32* %7
%76 = call i32 @join_threads(i32* %74, i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%79 = load i32*, i32** %9
%80 = load i32, i32* %6
%81 = call i32 @print_vector(i32* %79, i32 %80)
%82 = load i32*, i32** %10
%83 = load %struct.thread_w_arguments*, %struct.thread_w_arguments** %12
%84 = bitcast %struct.thread_w_arguments* %83 to i32*
%85 = load i32*, i32** %8
%86 = load i32*, i32** %9
%87 = load i32, i32* %6
%88 = call i32 @create_third_step_threads(i32* %82, i32* %84, i32* %85, i32* %86, i32 %87)
%89 = load i32*, i32** %10
%90 = load i32, i32* %6
%91 = call i32 @join_threads(i32* %89, i32 %90)
%92 = load i32*, i32** %10
%93 = call i32 @free(i32* %92)
%94 = load %struct.thread_w_arguments*, %struct.thread_w_arguments** %12
%95 = bitcast %struct.thread_w_arguments* %94 to i32*
%96 = call i32 @free(i32* %95)
%97 = load i32*, i32** %11
%98 = call i32 @free(i32* %97)
%99 = load %struct.second_step_threads_arguments*, %struct.second_step_threads_arguments** %13
%100 = bitcast %struct.second_step_threads_arguments* %99 to i32*
%101 = call i32 @free(i32* %100)
%102 = load i32*, i32** %8
%103 = call i32 @free(i32* %102)
%104 = load i32*, i32** %9
%105 = call i32 @free(i32* %104)
store i32 0, i32* %3
br label %106
106: ; preds = %34, %16
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"integers_amount: %d\0A\00"
@.str.2 = [19 x i8] c"Fator etapa 2: %d\0A\00"
@.str.3 = [29 x i8] c"Number of input values = %d\0A\00"
@.str.4 = [18 x i8] c"Input values x = \00"
@.str.5 = [26 x i8] c"After initialization w = \00"
@.str.7 = [5 x i8] c"w = \00"
@str = [13 x i8] c"After step 2\00"
@str.8 = [33 x i8] c"n\C3\BAmero de argumentos inv\C3\A1lido!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0))
br label %39
6: ; preds = %2
%7 = tail call i32 @find_integers_amount(i32 %0, i8** %1)
%8 = add nsw i32 %7, -1
%9 = mul nsw i32 %8, %7
%10 = sdiv i32 %9, 2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%13 = tail call i32* @tranlaste_arguments_to_vector(i32 %7, i8** %1)
%14 = tail call i32* @initialize_vectors(i32 %7)
%15 = tail call i32* @initialize_thread_vectors(i32 %7)
%16 = tail call i32* @initialize_thread_w_arguments_vectors(i32 %7)
%17 = tail call i32 @create_threads_w(i32* %15, i32* %16, i32* %13, i32* %14, i32 %7)
%18 = tail call i32 @join_threads(i32* %15, i32 %7)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%21 = tail call i32 @print_vector(i32* %13, i32 %7)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%23 = tail call i32 @print_vector(i32* %14, i32 %7)
%24 = tail call i32* @initialize_thread_vectors(i32 %10)
%25 = tail call i32* @initialize_second_threads_arguments(i32 %10)
%26 = tail call i32 @create_second_step_threads(i32* %24, i32* %25, i32* %13, i32* %14, i32 %7)
%27 = tail call i32 @join_threads(i32* %24, i32 %10)
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%30 = tail call i32 @print_vector(i32* %14, i32 %7)
%31 = tail call i32 @create_third_step_threads(i32* %15, i32* %16, i32* %13, i32* %14, i32 %7)
%32 = tail call i32 @join_threads(i32* %15, i32 %7)
%33 = tail call i32 @free(i32* %15)
%34 = tail call i32 @free(i32* %16)
%35 = tail call i32 @free(i32* %24)
%36 = tail call i32 @free(i32* %25)
%37 = tail call i32 @free(i32* %13)
%38 = tail call i32 @free(i32* %14)
br label %39
39: ; preds = %6, %4
ret i32 0
}