// File: /workspace/github_c_makefile_star2_O3/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 -0x22(%rip), %rsi       # 0x18c7
movl $0xc, %edi
callq 0x10a0
leaq 0xd15(%rip), %rdi       # 0x260f
callq 0x1050
callq 0x123c
leaq 0x27c5(%rip), %rdi      # 0x40d0
leaq -0xf5(%rip), %rsi       # 0x181d
xorl %edx, %edx
callq 0x1330
movl %eax, %ebx
movq 0x27ae(%rip), %rdi      # 0x40d0
leaq -0x18(%rbp), %rsi
callq 0x14c0
orl %ebx, %eax
jne 0x193e
cmpl $0x64, -0x18(%rbp)
jne 0x194c
leaq 0x7a9(%rip), %rdi       # 0x20e5
jmp 0x1953
leaq 0xd1f(%rip), %rdi       # 0x2664
callq 0x1050
jmp 0x195a
leaq 0x748(%rip), %rdi       # 0x209b
xorl %eax, %eax
callq 0x1090
leaq 0x2777(%rip), %rdi      # 0x40d8
leaq -0x14b(%rip), %rsi      # 0x181d
xorl %edx, %edx
callq 0x1330
movl %eax, %ebx
leaq -0x18(%rbp), %rsi
movl $0x19, %edi
callq 0x14c0
testl %ebx, %ebx
jne 0x199f
cmpl $0x3, %eax
leaq 0x7db(%rip), %rax       # 0x2168
leaq 0x79b(%rip), %rdi       # 0x212f
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
movq 0x272a(%rip), %rdi      # 0x40d0
xorl %esi, %esi
callq 0x14c0
cmpl $0x16, %eax
leaq 0x82a(%rip), %rax       # 0x21e1
leaq 0x7e3(%rip), %rdi       # 0x21a1
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x2710(%rip), %rdi      # 0x40e0
leaq -0x1a3(%rip), %rsi      # 0x1834
xorl %edx, %edx
callq 0x1330
leaq 0x2703(%rip), %rdi      # 0x40e8
leaq -0x17c(%rip), %rsi      # 0x1870
xorl %edx, %edx
callq 0x1330
movl %eax, %ebx
movq 0x26ec(%rip), %rdi      # 0x40e8
leaq -0x18(%rbp), %rsi
callq 0x14c0
testl %ebx, %ebx
jne 0x1a26
cmpl $0x64, -0x18(%rbp)
leaq 0x844(%rip), %rax       # 0x2258
leaq 0x806(%rip), %rdi       # 0x2221
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x26c3(%rip), %rdi      # 0x40f0
leaq -0x1b2(%rip), %rsi      # 0x1882
xorl %edx, %edx
callq 0x1330
movl %eax, %ebx
movq 0x26ac(%rip), %rdi      # 0x40f0
leaq -0x18(%rbp), %rsi
callq 0x14c0
testl %ebx, %ebx
jne 0x1a71
cmpl $0x1, 0x26c8(%rip)      # 0x4120
leaq 0x88a(%rip), %rax       # 0x22e9
leaq 0x829(%rip), %rdi       # 0x228f
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x2698(%rip), %rdi      # 0x4110
leaq -0x1ce(%rip), %rsi      # 0x18b1
xorl %edx, %edx
callq 0x1330
movl %eax, %ebx
movq 0x2681(%rip), %rdi      # 0x4110
movl $0xc, %esi
callq 0x157a
movq 0x2670(%rip), %rdi      # 0x4110
leaq -0x18(%rbp), %rsi
callq 0x14c0
testl %ebx, %ebx
jne 0x1acd
cmpl $0x0, 0x260c(%rip)      # 0x40c0
leaq 0x8cd(%rip), %rax       # 0x2388
leaq 0x881(%rip), %rdi       # 0x2343
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0x2624(%rip), %rdi      # 0x40f8
leaq -0x23e(%rip), %rsi      # 0x189d
xorl %edx, %edx
callq 0x1330
movl %eax, %ebx
movq 0x260d(%rip), %rdi      # 0x40f8
movl $0xffffffec, %esi       # imm = 0xFFFFFFEC
callq 0x157a
testl %ebx, %ebx
je 0x1b1b
movl $0x22, %edi
movl $0xa, %esi
callq 0x157a
movq 0x25e9(%rip), %rdi      # 0x40f8
movl $0x14, %esi
callq 0x157a
jmp 0x1b8e
cmpl $0x16, %eax
leaq 0x8d6(%rip), %rax       # 0x23fb
leaq 0x8a1(%rip), %rdi       # 0x23cd
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
movl $0x22, %edi
movl $0xa, %esi
callq 0x157a
cmpl $0x3, %eax
leaq 0x916(%rip), %rax       # 0x2466
leaq 0x8d2(%rip), %rdi       # 0x2429
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
movq 0x258f(%rip), %rdi      # 0x40f8
movl $0x14, %esi
callq 0x157a
testl %eax, %eax
je 0x1b80
leaq 0x925(%rip), %rdi       # 0x24a3
jmp 0x1b87
leaq 0x952(%rip), %rdi       # 0x24d9
xorl %eax, %eax
callq 0x1090
leaq 0x256b(%rip), %rdi      # 0x4100
leaq -0x2f9(%rip), %rsi      # 0x18a3
xorl %edx, %edx
callq 0x1330
movl %eax, %ebx
movq 0x2554(%rip), %rdi      # 0x4100
movl $0x14, %esi
callq 0x157a
movq 0x2543(%rip), %rdi      # 0x4100
movl $0x12, %esi
callq 0x157a
movl $0x1, %edi
callq 0x1110
testl %ebx, %ebx
jne 0x1bf5
cmpl $0x1, 0x2548(%rip)      # 0x4124
leaq 0x95c(%rip), %rax       # 0x253f
leaq 0x925(%rip), %rdi       # 0x250f
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
movq 0x24fc(%rip), %rdi      # 0x40f8
leaq -0x18(%rbp), %rbx
movq %rbx, %rsi
callq 0x14c0
movq 0x24f1(%rip), %rdi      # 0x4100
movq %rbx, %rsi
callq 0x14c0
leaq 0x24ea(%rip), %rdi      # 0x4108
leaq -0x3fd(%rip), %rsi      # 0x1828
movl $0x4, %edx
callq 0x1330
movl %eax, %r14d
movq 0x24cf(%rip), %rdi      # 0x4108
movq %rbx, %rsi
callq 0x14c0
orl %r14d, %eax
jne 0x1c63
cmpl $0x10, -0x18(%rbp)
leaq 0x96e(%rip), %rax       # 0x25bf
leaq 0x917(%rip), %rdi       # 0x256f
cmoveq %rax, %rdi
xorl %eax, %eax
callq 0x1090
leaq 0xa29(%rip), %rdi       # 0x2693
callq 0x1050
xorl %edi, %edi
callq 0x1416
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
popq %r14
popq %rbp
retq