// File: /workspace/github_c_makefile_star2_O1/robertdavidgraham[P]wc2/wcdiff.c
// Function: main
int main(int argc, char *argv[])
{
    unsigned char *buf;
    unsigned seed = 0;
    unsigned length = 1024 * 1024;
    unsigned i;
    unsigned min = 0;
    unsigned max = length;
    unsigned min_diff, max_diff;
    long x, y;
    const char *progname1 = "./wc2";
    const char *progname2 = "./wc2";
    const char *parms1 = NULL;
    char *parms2 = NULL;

    signal(SIGPIPE, SIG_IGN);

    /* The first parameter is the program we are testing against 'wc' */
    if (argc < 2 || !is_executable(argv[1])) {
        fprintf(stderr, "[-] first parameter must be an executable program\n");
        return 1;
    } else
        progname2 = argv[1];

    /* The second parameter is option parameters */
    if (argc >= 3) {
        parms1 = argv[2];
    } else
        parms1 = NULL;

    if (parms1) {
        parms2 = malloc(strlen(parms1) + 2);
        strcpy(parms2, parms1);
        strcat(parms2, "P");
    }

    /* Create a buffer of deterministically random data. This will be the
     * same data whenever we run this program, on any CPu, any OS, and
     * any compiler */
    buf = malloc(length);
    for (i=0; i<length; i++)
        buf[i] = r_rand(&seed);

    /* First, make sure there's a difference */
    x = word_count(progname1, parms1, buf, max);
    y = word_count(progname2, parms2, buf, max);
    if (x == -1) {
        fprintf(stderr, "[-] %s: failed\n", progname1);
        exit(1);
    }
    if (y == -1) {
        fprintf(stderr, "[-] %s: failed\n", progname2);
        exit(1);
    }
    if (x == y) {
        fprintf(stderr, "[+] difference: none! (identical results)\n");
        return 0;
    } else {
        fprintf(stderr, "[+] difference: %s=%lu %s=%lu\n", progname1, x, progname2, y);
    }

    /* Now search for one past the first difference */
    min = 0;
    max = length;
    while (min < max) {
        unsigned half = (max - min)/2 + min;
        fprintf(stderr, "max=%8lu\b\b\b\b\b\b\b\b\b\b\b\b", (unsigned long)(max));
        x = word_count(progname1, parms1, buf, half);
        y = word_count(progname2, parms2, buf, half);
        if (x == y) {
            /* we went too far, so we need to go back */
            min = half + 1;
        } else {
            /* we didn't go far enough */
            max = half;
        }
    }
    max_diff = max;
    fprintf(stderr, "max=%8lu\n", (unsigned long)(max_diff));

    /* Now search for one before the first difference */
    min = 0;
    max = max_diff;
    while (min < max) {
        unsigned half = (max - min)/2 + min;
        fprintf(stderr, "min=%8lu\b\b\b\b\b\b\b\b\b\b\b\b", (unsigned long)(min));
        /* fprintf(stderr, "min=%u, half=%u, max=%u\n", min, half, max); */
        x = word_count(progname1, parms1, buf + half, max_diff - half);
        y = word_count(progname2, parms2, buf + half, max_diff - half);
        if (x == y) {
            /* we went too far, so we need to go back */
            max = half;
        } else {
            /* we didn't go far enough */
            min = half + 1;
        }
    }
    min_diff = min;

    /* If they are equal, reduce min by one */
    if (min_diff > 0) {
        x = word_count(progname1, parms1, buf + min_diff, max_diff - min_diff);
        y = word_count(progname2, parms2, buf + min_diff, max_diff - min_diff);
        if (x == y) {
            min_diff--;
        }
    }
    fprintf(stderr, "min=%8lu\n", (unsigned long)(min_diff));

    /* print results */
    x = word_count(progname1, parms1, buf + min_diff, max_diff - min_diff);
    y = word_count(progname2, parms2, buf + min_diff, max_diff - min_diff);
    printf("Diff string: wc=%ld wc2=%ld \n", x, y);
    /* print_diff(buf, min_diff, max_diff); */
    printf("\"");
    for (i=min_diff; i<max_diff; i++) {
        printf("\\\\x%02x", buf[i]);
    }
    printf("\"\n");



/*

    if (argc < 3 || argv[2][0] != '-') {
        fprintf(stderr, "[-] second parameter must be option like -m or -w\n");
        return 1;
    }

    if (argc < 4 || !is_exists(argv[3])) {
        fprintf(stderr, "[-] third parameter must be file\n");
        return 1;
    }
*/

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movl $0x1, %esi
movl $0xd, %edi
callq 0x10e0
cmpl $0x2, %ebp
jl 0x199b
movq 0x8(%rbx), %rdi
callq 0x14ef
testl %eax, %eax
jne 0x19b6
callq 0x13a8
movl $0x1, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2, %ebp
jne 0x19c0
xorl %r15d, %r15d
jmp 0x19c4
movq 0x10(%rbx), %r15
movq 0x8(%rbx), %rax
movq %rax, (%rsp)
xorl %r14d, %r14d
testq %r15, %r15
jne 0x19df
movq $0x0, 0x10(%rsp)
jmp 0x1a11
movq %r15, %rdi
callq 0x1080
leaq 0x2(%rax), %rdi
callq 0x1110
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x1050
movq %rbx, %rdi
callq 0x1080
movq %rbx, 0x10(%rsp)
movw $0x50, (%rbx,%rax)
movl $0x100000, %edi         # imm = 0x100000
callq 0x1110
movq %rax, %rbx
xorl %eax, %eax
imull $0x343fd, %r14d, %r14d  # imm = 0x343FD
addl $0x269ec3, %r14d        # imm = 0x269EC3
movl %r14d, %ecx
shrl $0x10, %ecx
movb %cl, (%rbx,%rax)
incq %rax
cmpq $0x100000, %rax         # imm = 0x100000
jne 0x1a20
leaq 0x646(%rip), %rdi       # 0x208f
movl $0x100000, %ecx         # imm = 0x100000
movq %r15, %rsi
movq %rbx, %rdx
callq 0x17c3
movq %rax, %rbp
movl $0x100000, %ecx         # imm = 0x100000
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x17c3
cmpq $-0x1, %rbp
je 0x1d1d
cmpq $-0x1, %rax
je 0x1d22
movq 0x256b(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
cmpq %rax, %rbp
jne 0x1aa1
callq 0x1347
xorl %ebp, %ebp
jmp 0x19a5
movq %r15, 0x8(%rsp)
leaq 0x656(%rip), %rsi       # 0x2103
leaq 0x5db(%rip), %rdx       # 0x208f
movq %rbp, %rcx
movq (%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x10f0
movl $0x100000, %r15d        # imm = 0x100000
xorl %r13d, %r13d
movl %r15d, %r12d
subl %r13d, %r12d
shrl %r12d
leal (%r12,%r13), %r14d
movq 0x2516(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl %r15d, %ebp
movl %r15d, %edx
leaq 0x630(%rip), %rsi       # 0x2122
xorl %eax, %eax
callq 0x10f0
leaq 0x58f(%rip), %rdi       # 0x208f
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x17c3
movq %rax, %r15
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x17c3
cmpq %rax, %r15
je 0x1b31
movl %r14d, %r15d
jmp 0x1b3a
addl %r12d, %r13d
incl %r13d
movl %ebp, %r15d
cmpl %r15d, %r13d
jb 0x1ace
movq 0x24b2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl %r15d, %edx
leaq 0x5e4(%rip), %rsi       # 0x2137
movl $0x0, 0x1c(%rsp)
movq %rdx, 0x30(%rsp)
xorl %eax, %eax
callq 0x10f0
movl $0x0, %ecx
testl %r15d, %r15d
movl %r15d, 0x20(%rsp)
je 0x1c10
xorl %ecx, %ecx
movl %r15d, %eax
movq %rcx, 0x28(%rsp)
movl %eax, 0x24(%rsp)
movl %eax, %r12d
subl %ecx, %r12d
shrl %r12d
leal (%r12,%rcx), %ebp
movq 0x245c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl %ecx, %edx
leaq 0x599(%rip), %rsi       # 0x2141
xorl %eax, %eax
callq 0x10f0
leaq (%rbx,%rbp), %r14
subl %ebp, %r15d
leaq 0x4d2(%rip), %rdi       # 0x208f
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x17c3
movq %rax, %r13
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x17c3
cmpq %rax, %r13
je 0x1bfc
movq 0x28(%rsp), %rax
addl %r12d, %eax
incl %eax
movq %rax, %rcx
movl 0x24(%rsp), %eax
jmp 0x1c03
movl %ebp, %eax
movq 0x28(%rsp), %rcx
cmpl %eax, %ecx
movl 0x20(%rsp), %r15d
jb 0x1b7f
testl %ecx, %ecx
je 0x1c64
movl %ecx, %r14d
addq %rbx, %r14
movl %r15d, %ebp
subl %ecx, %r15d
leaq 0x468(%rip), %rdi       # 0x208f
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq %rcx, %r12
movq %r15, %rcx
callq 0x17c3
movq %rax, %r13
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movl %ebp, %r15d
callq 0x17c3
xorl %ecx, %ecx
cmpq %rax, %r13
sete %cl
subl %ecx, %r12d
movl %r12d, 0x1c(%rsp)
movq 0x238d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl 0x1c(%rsp), %r12d
movl %r12d, %r13d
leaq 0x4d9(%rip), %rsi       # 0x2156
xorl %ebp, %ebp
movq %r13, %rdx
xorl %eax, %eax
callq 0x10f0
movq %rbx, %r14
addq %r13, %r14
subl %r12d, %r15d
leaq 0x3f6(%rip), %rdi       # 0x208f
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x17c3
movq %rax, 0x8(%rsp)
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x17c3
leaq 0x497(%rip), %rdi       # 0x2160
movq 0x8(%rsp), %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10a0
movl $0x22, %edi
callq 0x1030
cmpl %r12d, 0x20(%rsp)
movq 0x30(%rsp), %r15
jbe 0x1d0c
leaq 0x489(%rip), %r14       # 0x217e
movzbl (%rbx,%r13), %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x10a0
incq %r13
cmpq %r13, %r15
jne 0x1cf5
leaq 0x473(%rip), %rdi       # 0x2186
callq 0x1060
jmp 0x19a5
callq 0x1380
movq (%rsp), %rdi
callq 0x135c