// File: /workspace/github_c_makefile_star2_O3/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, 0xf5d7(%rip)      # 0x12280
jne 0x2d14
leaq 0xf606(%rip), %rdi      # 0x122b8
callq 0x22f0
leaq 0xf602(%rip), %rdi      # 0x122c0
callq 0x22f0
movl 0xf5f7(%rip), %edi      # 0x122c0
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x20b0
movl $0x1, %edi
callq 0x2440
movl %eax, 0xf5de(%rip)      # 0x122c8
xorl %edi, %edi
callq 0x2440
movl %eax, 0xf5d5(%rip)      # 0x122cc
movl $0x1, 0xf57f(%rip)      # 0x12280
callq 0x2370
movl %eax, 0xf5c4(%rip)      # 0x122d0
testl %eax, %eax
je 0x2ea9
cmpl $0x3, %ebp
jg 0x2d66
leaq 0xa45b(%rip), %rdi      # 0xd17b
callq 0x23a0
cmpl $0x1, %ebp
jg 0x2d66
leaq 0xa42c(%rip), %rdi      # 0xd15d
xorl %eax, %eax
callq 0x2030
leaq 0xad67(%rip), %rbx      # 0xdaa6
leaq 0xf54a(%rip), %rsi      # 0x12290
movq %rbx, %rdi
xorl %eax, %eax
callq 0x22a0
leaq 0xa410(%rip), %rdi      # 0xd167
xorl %eax, %eax
callq 0x2030
movq %rsp, %rsi
movq %rbx, %rdi
jmp 0x2d98
movq 0x8(%rbx), %rsi
leaq 0xf51f(%rip), %rdi      # 0x12290
callq 0x2190
cmpl $0x3, %ebp
je 0x2da1
cmpl $0x2, %ebp
jne 0x2dbe
leaq 0xa3e0(%rip), %rdi      # 0xd167
xorl %eax, %eax
callq 0x2030
leaq 0xad11(%rip), %rdi      # 0xdaa6
movq %rsp, %rsi
xorl %eax, %eax
callq 0x22a0
jmp 0x2dad
movq 0x10(%rbx), %rsi
movq %rsp, %rdi
callq 0x2190
leaq 0xa3bd(%rip), %rdi      # 0xd171
callq 0x2270
movq %rax, %rsi
jmp 0x2dce
movq 0x10(%rbx), %rsi
movq %rsp, %rdi
callq 0x2190
movq 0x18(%rbx), %rsi
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x2190
callq 0x51e0
xorl %edi, %edi
xorl %esi, %esi
callq 0x55a1
movq %rax, %rbx
movq %rax, %rdi
callq 0x45b8
movq %rax, %r14
movq %rsp, %rsi
movq %rax, %rdi
callq 0x4bf5
movq %r14, %rdi
movq %r15, %rsi
callq 0x4c31
leaq -0x1aa(%rip), %rcx      # 0x2c6a
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbx, %r8
callq 0x4c70
movl $0x1, 0x10(%rbx)
movl $0x1, %esi
movq %rbx, %rdi
callq 0x5628
movl $0x1, %edi
callq 0x2400
cmpl $0x1, 0x10(%rbx)
je 0x2e2a
movq %r14, %rdi
callq 0x49c4
movq %rbx, %rdi
callq 0x560a
xorl %edi, %edi
subl 0xf471(%rip), %edi      # 0x122d0
movl $0x2, %esi
callq 0x2300
movl 0xf44d(%rip), %edi      # 0x122bc
leaq 0xa216(%rip), %rsi      # 0xd08c
movl $0x4, %edx
callq 0x23b0
movl 0xf436(%rip), %edi      # 0x122bc
leaq 0xa1fd(%rip), %rsi      # 0xd08a
movl $0x1, %edx
callq 0x23b0
callq 0x51ec
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x20f0
movl 0xf402(%rip), %edi      # 0x122b8
callq 0x2440
movl $0x1, %edi
callq 0x20f0
movl 0xf3f9(%rip), %edi      # 0x122c4
callq 0x2440
leaq 0xa270(%rip), %rdi      # 0xd147
xorl %esi, %esi
callq 0x2380
leaq 0xa164(%rip), %rsi      # 0xd049
leaq 0xa260(%rip), %rdx      # 0xd14c
xorl %eax, %eax
callq 0x5516
xorl %edi, %edi
callq 0x2240
nop