// File: /workspace/github_c_makefile_star2_O1/liuhaotian[P]xmppsh/xmppsh.c
// Function: main
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char jid[20], pass[20];





	if(!RedirStatus){
		pipe(pipeo2i);
		pipe(pipei2o);

		fcntl(pipei2o[0], F_SETFL, O_NONBLOCK);

		standardout = dup(1);
		standardin  = dup(0);

		RedirStatus = 1;

		pid = fork();
		if (pid >= 0) // fork succeeds
		{
			if (pid == 0) // child process
			{
				//signal(SIGINT, handler);
			// Child will execv the command
			
			//setpgid(0, 0); only for backgrounded processes
				close(0);
				dup(pipeo2i[0]);
				close(1);
				dup(pipei2o[1]);
				
				execvp("bash", NULL);
				xmpp_debug(conn->ctx, "xmpp", "Bash terminated!");
				exit(0);
	     	}
		}
	}





    /* take a master jid and password on the command line */
    if(argc < 4){
        printf("Usage: xmppsh <MasterID> <Robot ID> <Password>\n \
                MasterID: The main@jabber.org use to send out the cmd\n \
                Robot ID: The bash@jabber.org use to receive the cmd\n \
                Password: The bash@jabber.org Password\n");
    }
    if (argc < 2) {        
        printf("MasterID:");
        scanf("%s", master);
        printf("Robot ID:");
        scanf("%s", jid);
        strcpy(pass, getpass("Password:"));
    }
    else if (argc == 2)
    {
        strcpy(master, argv[1]);
        
        //printf("MasterID:");
        //scanf("%s", master);
        printf("Robot ID:");
        scanf("%s", jid);
        strcpy(pass, getpass("Password:"));
    }
    else if(argc == 3)
    {
    	strcpy(master, argv[1]);
    	strcpy(jid, argv[2]);
        
        //printf("MasterID:");
        //scanf("%s", master);
        //printf("Robot ID:");
        //scanf("%s", jid);
        strcpy(pass, getpass("Password:"));
    }
    else
    {
		strcpy(master, argv[1]);
    	strcpy(jid, argv[2]);
		strcpy(pass, argv[3]);
        
        //printf("MasterID:");
        //scanf("%s", master);
        //printf("Robot ID:");
        //scanf("%s", jid);
        //strcpy(pass, getpass("Password:"));

    }
    

    /* init library */
    xmpp_initialize();

    /* create a context */
    if (DEBUGMOD)
    {
    	log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
    }
    else
    {
    	log = NULL;
    }
    
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);

    /* enter the event loop - 
       our connect handler will trigger an exit */
    //xmpp_run(ctx);
    ctx->loop_status = XMPP_LOOP_RUNNING;
    while (ctx->loop_status == XMPP_LOOP_RUNNING) {
		xmpp_run_once(ctx, 1);
		sleep(1);
    }

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

	kill(-pid, SIGINT);
	write(pipeo2i[1], "exit", 4);
	write(pipeo2i[1], "\n", 1);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movl %edi, %ebp
cmpl $0x0, 0xf5d0(%rip)      # 0x12280
jne 0x2d1b
leaq 0xf5ff(%rip), %rdi      # 0x122b8
callq 0x22f0
leaq 0xf5fb(%rip), %rdi      # 0x122c0
callq 0x22f0
movl 0xf5f0(%rip), %edi      # 0x122c0
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20b0
movl $0x1, %edi
callq 0x2440
movl %eax, 0xf5d7(%rip)      # 0x122c8
xorl %edi, %edi
callq 0x2440
movl %eax, 0xf5ce(%rip)      # 0x122cc
movl $0x1, 0xf578(%rip)      # 0x12280
callq 0x2370
movl %eax, 0xf5bd(%rip)      # 0x122d0
testl %eax, %eax
je 0x2ebe
cmpl $0x3, %ebp
jg 0x2d2c
leaq 0xa454(%rip), %rdi      # 0xd17b
callq 0x23a0
cmpl $0x1, %ebp
jg 0x2d6d
leaq 0xa425(%rip), %rdi      # 0xd15d
xorl %eax, %eax
callq 0x2030
leaq 0xad60(%rip), %rbx      # 0xdaa6
leaq 0xf543(%rip), %rsi      # 0x12290
movq %rbx, %rdi
xorl %eax, %eax
callq 0x22a0
leaq 0xa409(%rip), %rdi      # 0xd167
xorl %eax, %eax
callq 0x2030
movq %rsp, %rsi
movq %rbx, %rdi
jmp 0x2da7
movq 0x8(%rbx), %rsi
leaq 0xf518(%rip), %rdi      # 0x12290
callq 0x2190
cmpl $0x3, %ebp
je 0x2e98
cmpl $0x2, %ebp
jne 0x2ea9
leaq 0xa3d1(%rip), %rdi      # 0xd167
xorl %eax, %eax
callq 0x2030
leaq 0xad02(%rip), %rdi      # 0xdaa6
movq %rsp, %rsi
xorl %eax, %eax
callq 0x22a0
leaq 0xa3bc(%rip), %rdi      # 0xd171
callq 0x2270
movq %rax, %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x2190
callq 0x5210
xorl %edi, %edi
xorl %esi, %esi
callq 0x55d1
movq %rax, %rbx
movq %rax, %rdi
callq 0x45f4
movq %rax, %r14
movq %rsp, %rsi
movq %rax, %rdi
callq 0x4c24
movq %r14, %rdi
movq %r15, %rsi
callq 0x4c60
leaq -0x192(%rip), %rcx      # 0x2c71
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %r8
callq 0x4c9f
movl $0x1, 0x10(%rbx)
movl $0x1, %esi
movq %rbx, %rdi
callq 0x5658
movl $0x1, %edi
callq 0x2400
cmpl $0x1, 0x10(%rbx)
je 0x2e19
movq %r14, %rdi
callq 0x49f6
movq %rbx, %rdi
callq 0x563a
xorl %edi, %edi
subl 0xf482(%rip), %edi      # 0x122d0
movl $0x2, %esi
callq 0x2300
movl 0xf45e(%rip), %edi      # 0x122bc
leaq 0xa227(%rip), %rsi      # 0xd08c
movl $0x4, %edx
callq 0x23b0
movl 0xf447(%rip), %edi      # 0x122bc
leaq 0xa20e(%rip), %rsi      # 0xd08a
movl $0x1, %edx
callq 0x23b0
callq 0x521c
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rbx), %rsi
movq %rsp, %rdi
callq 0x2190
jmp 0x2dae
movq 0x10(%rbx), %rsi
movq %rsp, %rdi
callq 0x2190
movq 0x18(%rbx), %rsi
jmp 0x2dbd
xorl %edi, %edi
callq 0x20f0
movl 0xf3ed(%rip), %edi      # 0x122b8
callq 0x2440
movl $0x1, %edi
callq 0x20f0
movl 0xf3e4(%rip), %edi      # 0x122c4
callq 0x2440
leaq 0xa25b(%rip), %rdi      # 0xd147
xorl %esi, %esi
callq 0x2380
leaq 0xa14f(%rip), %rsi      # 0xd049
leaq 0xa24b(%rip), %rdx      # 0xd14c
xorl %eax, %eax
callq 0x5546
xorl %edi, %edi
callq 0x2240
nop