// File: /workspace/github_c_makefile_star2_O3/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 0x1963
movq 0x8(%rbx), %rdi
callq 0x14ef
testl %eax, %eax
jne 0x197e
callq 0x13a8
movl $0x1, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
cmpl $0x2, %ebp
je 0x1998
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x19a5
xorl %ebx, %ebx
movq $0x0, 0x8(%rsp)
jmp 0x19d8
movq %rbx, %rdi
callq 0x1080
leaq 0x2(%rax), %rdi
callq 0x1110
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1050
movq %r14, %rdi
callq 0x1080
movq %r14, 0x8(%rsp)
movw $0x50, (%r14,%rax)
movl $0x100000, %edi         # imm = 0x100000
callq 0x1110
movq %rax, %r14
xorl %eax, %eax
imull $0x343fd, %r15d, %r15d  # imm = 0x343FD
addl $0x269ec3, %r15d        # imm = 0x269EC3
movl %r15d, %ecx
shrl $0x10, %ecx
movb %cl, (%r14,%rax)
incq %rax
cmpq $0x100000, %rax         # imm = 0x100000
jne 0x19e7
leaq 0x67e(%rip), %rdi       # 0x208f
movl $0x100000, %ecx         # imm = 0x100000
movq %rbx, %rsi
movq %r14, %rdx
callq 0x178b
movq %rax, %rbp
movl $0x100000, %ecx         # imm = 0x100000
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x178b
cmpq $-0x1, %rbp
je 0x1cd9
cmpq $-0x1, %rax
je 0x1cde
movq 0x25a2(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
cmpq %rax, %rbp
jne 0x1a6a
callq 0x1347
xorl %ebp, %ebp
jmp 0x196d
movq %rbx, 0x20(%rsp)
leaq 0x68d(%rip), %rsi       # 0x2103
leaq 0x612(%rip), %rdx       # 0x208f
movq %rbp, %rcx
movq 0x10(%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x10f0
movl $0x100000, %r12d        # imm = 0x100000
xorl %r13d, %r13d
movq %r12, %rax
subl %r13d, %r12d
shrl %r12d
leal (%r12,%r13), %ebx
movq 0x254c(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
movq %rax, %rbp
movl %eax, %edx
leaq 0x667(%rip), %rsi       # 0x2122
xorl %eax, %eax
callq 0x10f0
leaq 0x5c6(%rip), %rdi       # 0x208f
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x178b
movq %rax, %r15
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x178b
cmpq %rax, %r15
je 0x1afb
movl %ebx, %r12d
jmp 0x1b04
addl %r12d, %r13d
incl %r13d
movq %rbp, %r12
cmpl %r12d, %r13d
jb 0x1a98
movq 0x24e8(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl %r12d, %edx
leaq 0x61a(%rip), %rsi       # 0x2137
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x10f0
testl %r12d, %r12d
je 0x1c22
xorl %ecx, %ecx
movq %r12, 0x30(%rsp)
movl %r12d, %eax
movq %rcx, 0x18(%rsp)
movl %eax, 0x2c(%rsp)
movl %eax, %r12d
subl %ecx, %r12d
shrl %r12d
leal (%r12,%rcx), %ebp
movq 0x24a1(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl %ecx, %edx
leaq 0x5de(%rip), %rsi       # 0x2141
xorl %eax, %eax
callq 0x10f0
leaq (%r14,%rbp), %rbx
movq 0x30(%rsp), %r15
subl %ebp, %r15d
leaq 0x512(%rip), %rdi       # 0x208f
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x178b
movq %rax, %r13
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x178b
cmpq %rax, %r13
je 0x1bbd
movq 0x18(%rsp), %rax
addl %r12d, %eax
incl %eax
movq %rax, %rcx
movl 0x2c(%rsp), %eax
jmp 0x1bc4
movl %ebp, %eax
movq 0x18(%rsp), %rcx
cmpl %eax, %ecx
jb 0x1b3a
xorl %r13d, %r13d
testl %ecx, %ecx
movq 0x30(%rsp), %r12
je 0x1c22
movl %ecx, %ebx
addq %r14, %rbx
movl %r12d, %r15d
subl %ecx, %r15d
leaq 0x4a5(%rip), %rdi       # 0x208f
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
movq %rcx, %rbp
movq %r15, %rcx
callq 0x178b
movq %rax, %r13
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x178b
xorl %ecx, %ecx
cmpq %rax, %r13
sete %cl
subl %ecx, %ebp
movl %ebp, %r13d
movq 0x23cf(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl %r13d, %ebx
leaq 0x520(%rip), %rsi       # 0x2156
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10f0
movq %rbx, 0x18(%rsp)
addq %rbx, %r14
movl %r12d, %ebx
subl %r13d, %ebx
leaq 0x438(%rip), %rdi       # 0x208f
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x178b
movq %rax, %r15
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x178b
leaq 0x4da(%rip), %rdi       # 0x2160
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10a0
movl $0x22, %edi
callq 0x1030
cmpl %r13d, %r12d
jbe 0x1cc8
subl 0x18(%rsp), %r12d
leaq 0x4d0(%rip), %rbx       # 0x217e
xorl %r15d, %r15d
movzbl (%r14,%r15), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10a0
incq %r15
cmpl %r15d, %r12d
jne 0x1cb1
leaq 0x4b7(%rip), %rdi       # 0x2186
callq 0x1060
jmp 0x196d
callq 0x1380
movq 0x10(%rsp), %rdi
callq 0x135c