// File: /workspace/github_c_makefile_star2_O2/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, 0xe60e(%rip)      # 0x11280
jne 0x2cd9
leaq 0xe63d(%rip), %rdi      # 0x112b8
callq 0x22f0
leaq 0xe639(%rip), %rdi      # 0x112c0
callq 0x22f0
movl 0xe62e(%rip), %edi      # 0x112c0
pushq $0x4
popq %rsi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20b0
pushq $0x1
popq %rdi
callq 0x2440
movl %eax, 0xe619(%rip)      # 0x112c8
xorl %edi, %edi
callq 0x2440
movl %eax, 0xe610(%rip)      # 0x112cc
movl $0x1, 0xe5ba(%rip)      # 0x11280
callq 0x2370
movl %eax, 0xe5ff(%rip)      # 0x112d0
testl %eax, %eax
je 0x2e70
cmpl $0x3, %ebp
jg 0x2d2b
leaq 0x9496(%rip), %rdi      # 0xc17b
callq 0x23a0
cmpl $0x1, %ebp
jg 0x2d2b
leaq 0x9467(%rip), %rdi      # 0xc15d
xorl %eax, %eax
callq 0x2030
leaq 0x9da2(%rip), %rbx      # 0xcaa6
leaq 0xe585(%rip), %rsi      # 0x11290
movq %rbx, %rdi
xorl %eax, %eax
callq 0x22a0
leaq 0x944b(%rip), %rdi      # 0xc167
xorl %eax, %eax
callq 0x2030
movq %rsp, %rsi
movq %rbx, %rdi
jmp 0x2d5d
movq 0x8(%rbx), %rsi
leaq 0xe55a(%rip), %rdi      # 0x11290
callq 0x2190
cmpl $0x3, %ebp
je 0x2d66
cmpl $0x2, %ebp
jne 0x2d83
leaq 0x941b(%rip), %rdi      # 0xc167
xorl %eax, %eax
callq 0x2030
leaq 0x9d4c(%rip), %rdi      # 0xcaa6
movq %rsp, %rsi
xorl %eax, %eax
callq 0x22a0
jmp 0x2d72
movq 0x10(%rbx), %rsi
movq %rsp, %rdi
callq 0x2190
leaq 0x93f8(%rip), %rdi      # 0xc171
callq 0x2270
movq %rax, %rsi
jmp 0x2d93
movq 0x10(%rbx), %rsi
movq %rsp, %rdi
callq 0x2190
movq 0x18(%rbx), %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x2190
callq 0x5120
xorl %edi, %edi
xorl %esi, %esi
callq 0x54d9
movq %rax, %rbx
movq %rax, %rdi
callq 0x454c
movq %rax, %r14
movq %rsp, %rsi
movq %rax, %rdi
callq 0x4b43
movq %r14, %rdi
movq %r15, %rsi
callq 0x4b7f
leaq -0x260(%rip), %rcx      # 0x2b79
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %r8
callq 0x4bbe
movl $0x1, 0x10(%rbx)
pushq $0x1
popq %rbp
pushq $0x1
popq %r15
movl %ebp, %eax
cmpl $0x1, %eax
jne 0x2e14
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5554
movl %ebp, %edi
callq 0x2400
movl 0x10(%rbx), %eax
jmp 0x2df8
movq %r14, %rdi
callq 0x4918
movq %rbx, %rdi
callq 0x5538
xorl %edi, %edi
subl 0xe4a4(%rip), %edi      # 0x112d0
pushq $0x2
popq %rsi
callq 0x2300
movl 0xe482(%rip), %edi      # 0x112bc
leaq 0x924b(%rip), %rsi      # 0xc08c
pushq $0x4
popq %rdx
callq 0x23b0
movl 0xe46d(%rip), %edi      # 0x112bc
leaq 0x9234(%rip), %rsi      # 0xc08a
pushq $0x1
popq %rdx
callq 0x23b0
callq 0x512c
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x20f0
movl 0xe43b(%rip), %edi      # 0x112b8
callq 0x2440
pushq $0x1
popq %rdi
callq 0x20f0
movl 0xe434(%rip), %edi      # 0x112c4
callq 0x2440
leaq 0x92ab(%rip), %rdi      # 0xc147
xorl %esi, %esi
callq 0x2380
leaq 0x919f(%rip), %rsi      # 0xc049
leaq 0x929b(%rip), %rdx      # 0xc14c
xorl %eax, %eax
callq 0x544e
xorl %edi, %edi
callq 0x2240
nop