// File: /workspace/github_c_makefile_star2_O2/deepika9294[P]Threading-Library/one-one/test/methods.c
// Function: main
int main() {

    //for getting return values
    void *tret;
    signal(SIGUSR2, signal_handler);

    printf("\n\n-----------------ALL THE METHODS TESTING (CREATE, JOIN, EXIT, KILL)--------------\n\n");
	dthread_init();

	int c1 = dthread_create( &t1[0], add , NULL);
    int j1 = dthread_join(t1[0], &tret);
    if( c1 == 0 && j1 == 0 ) {
        check = testing((intptr_t)tret, 100);
        if(check == 0) {
            printf(RED "**FAILED**: Addition test with create and join return value\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Addition test with create and join return value\n" NONE);
        }
    }
    else {
        printf("*****Issue in creating or joining thread *****\n");
    }

    c1 = dthread_create( &t1[1], add , NULL);
    j1 = dthread_join(25, &tret);
    if(c1 == 0) {
       check = testing(j1,ESRCH);
       if(check == 0) {
            printf(RED "**FAILED**: Joining an invalid thread test\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Joining an invalid thread test\n" NONE);
        }
    }

    j1 = dthread_join(t1[0], NULL);
    check = testing(j1, EINVAL);
    if(check == 0) {
        printf(RED "**FAILED**: Joining on already joined thread test\n" NONE);
    }
    else {
        printf(GREEN "**PASSED**: Joining on already joined thread test\n" NONE);
    }


    c1 = dthread_create( &t1[2], deadlock , NULL);

    
    c1 = dthread_create( &t1[3], exit_test, NULL);
    j1 = dthread_join(t1[3], &tret);

    if(c1 == 0) {
        check = testing((intptr_t)tret, 100);
        if(check == 0) {
            printf(RED "**FAILED**: Thread exit test with retval\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Thread exit test with retval\n" NONE);
        }
    }
    
    c1 = dthread_create( &t1[4], exit_control, NULL);
    j1 = dthread_join(t1[4], &tret);
    if(c1 == 0) {
        check = testing(counter, 1);
        if(check == 0) {
            printf(RED "**FAILED**: Thread exit test, i.e rest of the instructions are not executed\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Thread exit test, i.e rest of the instructions are not executed\n" NONE);
        }
    }

    c1 = dthread_create( &t1[8], sig_loop, NULL);
    int s = dthread_kill(t1[8], SIGUSR2);
    dthread_join(t1[8],&tret);

    if(c1 == 0) {
        check = testing(run, 0);
        if(check == 0) {
            printf(RED "**FAILED**: Signal Handler test case for infinite loop\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Signal Handler test case for infinite loop\n" NONE);
        }
    }

    c1 = dthread_create( &t1[5], looping, NULL);
    // j1 = dthread_join(t1[5], &tret);
    int k = dthread_kill(t1[5], -20);
    if(c1 == 0) {
        check = testing(k, EINVAL);
        if(check == 0) {
            printf(RED "**FAILED**: Invalid Signal Test\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Invalid Signal Test\n" NONE);
        }
    }
    k = dthread_kill(34, SIGUSR1);
    if(c1 == 0) {
        check = testing(k, ESRCH);
        if(check == 0) {
            printf(RED "**FAILED**: Thread kill test on invalid thread\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Thread kill test on invalid thread\n" NONE);
        }
    }
    k = dthread_kill(t1[5], SIGTSTP);
    
    if(c1 == 0) {
        check = testing(k, 0);
        if(check == 0) {
            printf(RED "**FAILED**: Valid Signal Test : SIGTSTP\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Valid Signal Test : SIGTSTP\n" NONE);
        }
    }

    c1 = dthread_create( &t1[6], cont, NULL);
    //stopping the thread
    k = dthread_kill(t1[6], SIGTSTP);
    //restarting again
    int k2 = dthread_kill(t1[6], SIGCONT);
    sleep(1);
    if(c1 == 0) {
        check = testing(signal_counter, 1);
        if(check == 0) {
            printf(RED "**FAILED**: SIGCONT handling test\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: SIGCONT handling test\n" NONE);
        }
    }
    j1 = dthread_join(t1[5], &tret);
    j1 = dthread_join(t1[6], &tret);

    c1 = dthread_create( &t1[7], square, (void *)(intptr_t)(4));
    j1 = dthread_join(t1[7], &tret);
    if(c1 == 0 && j1 == 0) {
        check = testing((intptr_t)tret, 16);
        if(check == 0) {
            printf(RED "**FAILED**: Creating threads with args and join return value test\n" NONE);
        }
        else {
            printf(GREEN "**PASSED**: Creating threads with args and join return value test\n" NONE);
        }
    }
    printf("\n--------------------------------EXITING METHODS TEST--------------------------------\n\n");
    
    dthread_exit(NULL);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x10, %rsp
leaq -0x1f(%rip), %rsi       # 0x186b
pushq $0xc
popq %rdi
callq 0x10a0
leaq 0xd76(%rip), %rdi       # 0x260f
callq 0x1050
callq 0x121c
leaq 0x2826(%rip), %rdi      # 0x40d0
leaq -0xf8(%rip), %rsi       # 0x17b9
xorl %edx, %edx
callq 0x1302
movl %eax, %ebx
movq 0x280f(%rip), %rdi      # 0x40d0
leaq -0x18(%rbp), %rsi
callq 0x1477
orl %ebx, %eax
jne 0x18dd
cmpl $0x64, -0x18(%rbp)
jne 0x18eb
leaq 0x80a(%rip), %rdi       # 0x20e5
jmp 0x18f2
leaq 0xd80(%rip), %rdi       # 0x2664
callq 0x1050
jmp 0x18f9
leaq 0x7a9(%rip), %rdi       # 0x209b
xorl %eax, %eax
callq 0x1090
leaq 0x27d8(%rip), %rdi      # 0x40d8
leaq -0x14e(%rip), %rsi      # 0x17b9
xorl %edx, %edx
callq 0x1302
movl %eax, %ebx
pushq $0x19
popq %rdi
leaq -0x18(%rbp), %rsi
callq 0x1477
testl %ebx, %ebx
jne 0x193c
cmpl $0x3, %eax
leaq 0x83e(%rip), %rax       # 0x2168
leaq 0x7fe(%rip), %rdi       # 0x212f
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
movq 0x278d(%rip), %rdi      # 0x40d0
xorl %esi, %esi
callq 0x1477
cmpl $0x16, %eax
leaq 0x88d(%rip), %rax       # 0x21e1
leaq 0x846(%rip), %rdi       # 0x21a1
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x2773(%rip), %rdi      # 0x40e0
leaq -0x19f(%rip), %rsi      # 0x17d5
xorl %edx, %edx
callq 0x1302
leaq 0x2766(%rip), %rdi      # 0x40e8
leaq -0x178(%rip), %rsi      # 0x1811
xorl %edx, %edx
callq 0x1302
movl %eax, %ebx
movq 0x274f(%rip), %rdi      # 0x40e8
leaq -0x18(%rbp), %rsi
callq 0x1477
testl %ebx, %ebx
jne 0x19c3
cmpl $0x64, -0x18(%rbp)
leaq 0x8a7(%rip), %rax       # 0x2258
leaq 0x869(%rip), %rdi       # 0x2221
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x2726(%rip), %rdi      # 0x40f0
leaq -0x1b0(%rip), %rsi      # 0x1821
xorl %edx, %edx
callq 0x1302
movl %eax, %ebx
movq 0x270f(%rip), %rdi      # 0x40f0
leaq -0x18(%rbp), %rsi
callq 0x1477
testl %ebx, %ebx
jne 0x1a0e
cmpl $0x1, 0x272b(%rip)      # 0x4120
leaq 0x8ed(%rip), %rax       # 0x22e9
leaq 0x88c(%rip), %rdi       # 0x228f
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x26fb(%rip), %rdi      # 0x4110
leaq -0x1cc(%rip), %rsi      # 0x1850
xorl %edx, %edx
callq 0x1302
movl %eax, %ebx
movq 0x26e4(%rip), %rdi      # 0x4110
pushq $0xc
popq %rsi
callq 0x1536
movq 0x26d5(%rip), %rdi      # 0x4110
leaq -0x18(%rbp), %rsi
callq 0x1477
testl %ebx, %ebx
jne 0x1a68
cmpl $0x0, 0x2671(%rip)      # 0x40c0
leaq 0x932(%rip), %rax       # 0x2388
leaq 0x8e6(%rip), %rdi       # 0x2343
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x2689(%rip), %rdi      # 0x40f8
leaq -0x23a(%rip), %rsi      # 0x183c
xorl %edx, %edx
callq 0x1302
movl %eax, %ebx
movq 0x2672(%rip), %rdi      # 0x40f8
pushq $-0x14
popq %rsi
callq 0x1536
testl %ebx, %ebx
jne 0x1aae
cmpl $0x16, %eax
leaq 0x95f(%rip), %rax       # 0x23fb
leaq 0x92a(%rip), %rdi       # 0x23cd
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
pushq $0x22
popq %rdi
pushq $0xa
popq %rsi
callq 0x1536
testl %ebx, %ebx
jne 0x1ad9
cmpl $0x3, %eax
leaq 0x99f(%rip), %rax       # 0x2466
leaq 0x95b(%rip), %rdi       # 0x2429
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
movq 0x2618(%rip), %rdi      # 0x40f8
pushq $0x14
popq %rsi
callq 0x1536
testl %ebx, %ebx
jne 0x1b07
testl %eax, %eax
leaq 0x9e4(%rip), %rax       # 0x24d9
leaq 0x9a7(%rip), %rdi       # 0x24a3
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x25f2(%rip), %rdi      # 0x4100
leaq -0x2d3(%rip), %rsi      # 0x1842
xorl %edx, %edx
callq 0x1302
movl %eax, %ebx
movq 0x25db(%rip), %rdi      # 0x4100
pushq $0x14
popq %rsi
callq 0x1536
movq 0x25cc(%rip), %rdi      # 0x4100
pushq $0x12
popq %rsi
callq 0x1536
pushq $0x1
popq %rdi
callq 0x1110
testl %ebx, %ebx
jne 0x1b68
cmpl $0x1, 0x25d5(%rip)      # 0x4124
leaq 0x9e9(%rip), %rax       # 0x253f
leaq 0x9b2(%rip), %rdi       # 0x250f
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
movq 0x2589(%rip), %rdi      # 0x40f8
leaq -0x18(%rbp), %rbx
movq %rbx, %rsi
callq 0x1477
movq 0x257e(%rip), %rdi      # 0x4100
movq %rbx, %rsi
callq 0x1477
leaq 0x2577(%rip), %rdi      # 0x4108
leaq -0x3cf(%rip), %rsi      # 0x17c9
pushq $0x4
popq %rdx
callq 0x1302
movl %eax, %r14d
movq 0x255e(%rip), %rdi      # 0x4108
movq %rbx, %rsi
callq 0x1477
orl %r14d, %eax
jne 0x1bd4
cmpl $0x10, -0x18(%rbp)
leaq 0x9fd(%rip), %rax       # 0x25bf
leaq 0x9a6(%rip), %rdi       # 0x256f
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0xab8(%rip), %rdi       # 0x2693
callq 0x1050
xorl %edi, %edi
callq 0x13d5
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq