// File: /workspace/github_c_makefile_star2_O2/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
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x10e0
cmpl $0x2, %ebp
jl 0x1780
movq 0x8(%rbx), %rdi
callq 0x12f8
testl %eax, %eax
jne 0x17a4
movq 0x2871(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x904(%rip), %rdi       # 0x2095
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x1160
pushq $0x1
popq %rax
jmp 0x1ae5
movq 0x8(%rbx), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
cmpl $0x2, %ebp
je 0x17be
movq 0x10(%rbx), %rbp
testq %rbp, %rbp
jne 0x17ca
xorl %ebp, %ebp
movq $0x0, (%rsp)
jmp 0x17fb
movq %rbp, %rdi
callq 0x1080
leaq 0x2(%rax), %rdi
callq 0x1110
movq %rax, %rbx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x1050
movq %rbx, %rdi
callq 0x1080
movq %rbx, (%rsp)
movw $0x50, (%rbx,%rax)
movl $0x100000, %edi         # imm = 0x100000
callq 0x1110
movq %rax, %r14
xorl %eax, %eax
cmpq $0x100000, %rax         # imm = 0x100000
je 0x182f
imull $0x343fd, %r15d, %r15d  # imm = 0x343FD
addl $0x269ec3, %r15d        # imm = 0x269EC3
movl %r15d, %ecx
shrl $0x10, %ecx
movb %cl, (%r14,%rax)
incq %rax
jmp 0x180a
leaq 0x859(%rip), %rdi       # 0x208f
movl $0x100000, %ecx         # imm = 0x100000
movq %rbp, %rsi
movq %r14, %rdx
callq 0x15e4
movq %rax, %rbx
movl $0x100000, %ecx         # imm = 0x100000
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq %r14, %rdx
callq 0x15e4
cmpq $-0x1, %rbx
je 0x1af4
cmpq $-0x1, %rax
je 0x1b0e
movq 0x277e(%rip), %rcx      # 0x3ff8
movq (%rcx), %rcx
cmpq %rax, %rbx
jne 0x1899
leaq 0x84f(%rip), %rdi       # 0x20d8
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1160
jmp 0x1ae3
leaq 0x863(%rip), %rsi       # 0x2103
leaq 0x7e8(%rip), %rdx       # 0x208f
movq %rcx, %rdi
movq %rbx, %rcx
movq 0x8(%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x10f0
movl $0x100000, %eax         # imm = 0x100000
movq %rax, 0x10(%rsp)
leaq 0x855(%rip), %rbx       # 0x2122
xorl %r15d, %r15d
movq %rbp, 0x18(%rsp)
movq 0x10(%rsp), %rax
movl %eax, %r12d
movl %eax, %edx
subl %r15d, %r12d
jbe 0x1944
shrl %r12d
leal (%r12,%r15), %r13d
movq 0x2706(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10f0
leaq 0x789(%rip), %rdi       # 0x208f
movq %rbp, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x15e4
movq %rax, %rbp
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x15e4
cmpq %rax, %rbp
je 0x1937
movq %r13, 0x10(%rsp)
jmp 0x193d
addl %r12d, %r15d
incl %r15d
movq 0x18(%rsp), %rbp
jmp 0x18d5
movq 0x26ad(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x7e2(%rip), %rsi       # 0x2137
movq %rdx, 0x28(%rsp)
xorl %eax, %eax
callq 0x10f0
movq 0x10(%rsp), %rax
xorl %r15d, %r15d
movl %eax, %ebp
movl %r15d, %ebx
subl %r15d, %ebp
jbe 0x19f2
movl %eax, 0x24(%rsp)
shrl %ebp
leal (%r15,%rbp), %r12d
movq 0x2674(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x7b3(%rip), %rsi       # 0x2141
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10f0
leaq (%r14,%r12), %rbx
movq 0x10(%rsp), %r13
movq %r12, 0x30(%rsp)
subl %r12d, %r13d
leaq 0x6df(%rip), %rdi       # 0x208f
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x15e4
movq %rax, %r12
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x15e4
cmpq %rax, %r12
je 0x19e8
addl %ebp, %r15d
incl %r15d
movl 0x24(%rsp), %eax
jmp 0x1969
movq 0x30(%rsp), %rax
jmp 0x1969
xorl %ebp, %ebp
testl %r15d, %r15d
je 0x1a40
addq %r14, %rbx
movq 0x10(%rsp), %r12
subl %r15d, %r12d
leaq 0x684(%rip), %rdi       # 0x208f
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x15e4
movq %rax, %r13
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x15e4
xorl %ecx, %ecx
cmpq %rax, %r13
sete %cl
subl %ecx, %r15d
movl %r15d, %ebp
movq 0x25b1(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl %ebp, %ebx
leaq 0x703(%rip), %rsi       # 0x2156
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10f0
movq %r14, %r12
addq %rbx, %r12
movq 0x10(%rsp), %r15
subl %ebp, %r15d
leaq 0x61d(%rip), %rdi       # 0x208f
movq 0x18(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x15e4
movq %rax, %r13
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x15e4
leaq 0x6c0(%rip), %rdi       # 0x2160
movq %r13, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10a0
pushq $0x22
popq %rdi
callq 0x1030
leaq 0x6c2(%rip), %r15       # 0x217e
cmpq 0x28(%rsp), %rbx
jae 0x1ad7
movzbl (%r14,%rbx), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10a0
incq %rbx
jmp 0x1abc
leaq 0x6a8(%rip), %rdi       # 0x2186
callq 0x1060
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x24fd(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x5c3(%rip), %rsi       # 0x20c8
leaq 0x583(%rip), %rdx       # 0x208f
jmp 0x1b24
movq 0x24e3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x5a9(%rip), %rsi       # 0x20c8
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x10f0
pushq $0x1
popq %rdi
callq 0x1150