// File: cloudinary[P]fuif/transform/squeeze.h
// Function: inv_hsqueeze(Image&, int, int)
void inv_hsqueeze(Image &input, int c, int rc) {
    const Channel &chin = input.channel[c];
    const Channel &chin_residual = input.channel[rc];
    Channel chout(chin.w + chin_residual.w, chin.h,chin.minval,chin.maxval,chin.q,chin.hshift-1,chin.vshift,chin.hcshift-1,chin.vcshift);
    chout.component = chin.component;
    v_printf(4,"Undoing horizontal squeeze of channel %i using residuals in channel %i (going from width %i to %i)\n",c,rc,chin.w,chout.w);

    for (int y=0; y<chin.h; y++) {
      pixel_type avg = chin.value_nocheck(y,0);
      pixel_type next_avg = (1<chin.w ? next_avg = chin.value_nocheck(y,1) : avg);
      pixel_type tendency=smooth_tendency(avg,avg,next_avg);
      pixel_type diff = chin_residual.value(y,0) + tendency;
      pixel_type A = ((avg<<1)+diff+(diff>0?-(diff&1):(diff&1)))>>1;
      pixel_type B = A-diff;
      chout.value(y,0) = A;
      chout.value(y,1) = B;
      for (int x=1; x<chin_residual.w; x++) {
        pixel_type diff_minus_tendency = chin_residual.value(y,x);
        pixel_type avg = chin.value_nocheck(y,x);
        pixel_type next_avg = (x+1<chin.w ? next_avg = chin.value_nocheck(y,x+1) : avg);
        pixel_type left = chout.value_nocheck(y,(x<<1)-1);
        pixel_type tendency=smooth_tendency(left,avg,next_avg);
        pixel_type diff = diff_minus_tendency + tendency;
//        pixel_type A = ((avg<<1)+diff+(diff&1))>>1;
        pixel_type A = ((avg<<1)+diff+(diff>0?-(diff&1):(diff&1)))>>1;
        chout.value(y,x<<1) = A;
        pixel_type B = A-diff;
        chout.value(y,(x<<1)+1) = B;
//        if (c==2 && chin.w == 1352 && x > 180 && x < 185 && y > 118 && y < 121) printf("Cg = avg=%i, diff=%i=%i+%i, A=%i,B=%i\n",avg,diff,diff_minus_tendency,tendency,A,B);
      }
/*
      for (int x=1; x<chin_residual.w; x++) {
        pixel_type diff_minus_tendency = chin_residual.value(y,x);
        pixel_type avg = chin.value(y,x);

        pixel_type next_avg = (x+1<chin.w ? chin.value(y,x+1) : avg);
        pixel_type leftleft = (x>0 ? chout.value(y,x*2-2) : avg);
        pixel_type left = (x>0 ? chout.value(y,x*2-1) : avg);
        pixel_type tendency=smooth_tendency(leftleft,left,avg,next_avg);

        pixel_type diff = diff_minus_tendency + tendency;

        pixel_type A = ((avg<<1)+diff+(diff&1))>>1;
        pixel_type B = A-diff;
        chout.value(y,x*2) = A;
        chout.value(y,x*2+1) = B;
      }
*/
      if (chout.w & 1) chout.value(y,chout.w-1) = chin.value(y,chin.w-1);
    }
    input.channel[c] = chout;
}

// Assembly:
pushq %rbx
subq $0x130, %rsp            # imm = 0x130
movq %rdi, 0x128(%rsp)
movl %esi, 0x124(%rsp)
movl %edx, 0x120(%rsp)
movq 0x128(%rsp), %rdi
movslq 0x124(%rsp), %rsi
callq 0x4ba0
movq %rax, 0x118(%rsp)
movq 0x128(%rsp), %rdi
movslq 0x120(%rsp), %rsi
callq 0x4ba0
movq %rax, 0x110(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x110(%rsp), %rcx
movl 0x18(%rcx), %ecx
movl 0x18(%rax), %esi
movl 0x1c(%rax), %edx
addl %ecx, %esi
movswl 0x20(%rax), %ecx
movswl 0x22(%rax), %r8d
movl 0x28(%rax), %r9d
movl 0x2c(%rax), %edi
decl %edi
movl 0x30(%rax), %r10d
movl 0x34(%rax), %r11d
decl %r11d
movl 0x38(%rax), %ebx
movq %rsp, %rax
movl %ebx, 0x18(%rax)
movl %r11d, 0x10(%rax)
movl %r10d, 0x8(%rax)
movl %edi, (%rax)
leaq 0xd0(%rsp), %rdi
callq 0x12200
movq 0x118(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, 0x10c(%rsp)
movl 0x124(%rsp), %edx
movl 0x120(%rsp), %ecx
movq 0x118(%rsp), %rax
movl 0x18(%rax), %r8d
movl 0xe8(%rsp), %r9d
leaq 0x3b2bd(%rip), %rsi     # 0x46b10
xorl %eax, %eax
movl $0x4, %edi
callq 0x3a770
jmp 0xb861
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
movq 0x118(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0xbd9b
movq 0x118(%rsp), %rdi
movl 0xc0(%rsp), %esi
xorl %edx, %edx
callq 0x131b0
movw %ax, 0x96(%rsp)
jmp 0xb8a4
movw 0x96(%rsp), %ax
movw %ax, 0xbe(%rsp)
movq 0x118(%rsp), %rcx
movl $0x1, %eax
cmpl 0x18(%rcx), %eax
jge 0xb903
movq 0x118(%rsp), %rdi
movl 0xc0(%rsp), %esi
movl $0x1, %edx
callq 0x131b0
movw %ax, 0x94(%rsp)
jmp 0xb8e9
movw 0x94(%rsp), %ax
movw %ax, 0xbc(%rsp)
movw %ax, 0x92(%rsp)
jmp 0xb913
movw 0xbe(%rsp), %ax
movw %ax, 0x92(%rsp)
movw 0x92(%rsp), %ax
movw %ax, 0xbc(%rsp)
movswl 0xbe(%rsp), %esi
movswl 0xbc(%rsp), %edx
movl %esi, %edi
callq 0x131f0
movw %ax, 0x90(%rsp)
jmp 0xb944
movw 0x90(%rsp), %ax
movw %ax, 0xba(%rsp)
movq 0x110(%rsp), %rdi
movl 0xc0(%rsp), %esi
xorl %edx, %edx
callq 0x12ba0
movw %ax, 0x8e(%rsp)
jmp 0xb974
movw 0x8e(%rsp), %ax
cwtl
movswl 0xba(%rsp), %ecx
addl %ecx, %eax
movw %ax, 0xb8(%rsp)
movswl 0xbe(%rsp), %eax
shll %eax
movswl 0xb8(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x88(%rsp)
movswl 0xb8(%rsp), %eax
cmpl $0x0, %eax
jle 0xb9cf
movswl 0xb8(%rsp), %ecx
andl $0x1, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x84(%rsp)
jmp 0xb9e1
movswl 0xb8(%rsp), %eax
andl $0x1, %eax
movl %eax, 0x84(%rsp)
movl 0x88(%rsp), %eax
movl 0x84(%rsp), %ecx
addl %ecx, %eax
shrl %eax
movw %ax, 0xb6(%rsp)
movzwl 0xb6(%rsp), %eax
movzwl 0xb8(%rsp), %ecx
subl %ecx, %eax
movw %ax, 0xb4(%rsp)
movw 0xb6(%rsp), %ax
movw %ax, 0x76(%rsp)
movl 0xc0(%rsp), %esi
leaq 0xd0(%rsp), %rdi
xorl %edx, %edx
callq 0x12110
movq %rax, 0x78(%rsp)
jmp 0xba3f
movq 0x78(%rsp), %rax
movw 0x76(%rsp), %cx
movw %cx, (%rax)
movw 0xb4(%rsp), %ax
movw %ax, 0x66(%rsp)
movl 0xc0(%rsp), %esi
leaq 0xd0(%rsp), %rdi
movl $0x1, %edx
callq 0x12110
movq %rax, 0x68(%rsp)
jmp 0xba79
movq 0x68(%rsp), %rax
movw 0x66(%rsp), %cx
movw %cx, (%rax)
movl $0x1, 0xb0(%rsp)
movl 0xb0(%rsp), %eax
movq 0x110(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jge 0xbd23
movq 0x110(%rsp), %rdi
movl 0xc0(%rsp), %esi
movl 0xb0(%rsp), %edx
callq 0x12ba0
movw %ax, 0x64(%rsp)
jmp 0xbacb
movw 0x64(%rsp), %ax
movw %ax, 0xae(%rsp)
movq 0x118(%rsp), %rdi
movl 0xc0(%rsp), %esi
movl 0xb0(%rsp), %edx
callq 0x131b0
movw %ax, 0x62(%rsp)
jmp 0xbafa
movw 0x62(%rsp), %ax
movw %ax, 0xac(%rsp)
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movq 0x118(%rsp), %rcx
cmpl 0x18(%rcx), %eax
jge 0xbb56
movq 0x118(%rsp), %rdi
movl 0xc0(%rsp), %esi
movl 0xb0(%rsp), %edx
incl %edx
callq 0x131b0
movw %ax, 0x60(%rsp)
jmp 0xbb42
movw 0x60(%rsp), %ax
movw %ax, 0xaa(%rsp)
movw %ax, 0x5e(%rsp)
jmp 0xbb63
movw 0xac(%rsp), %ax
movw %ax, 0x5e(%rsp)
movw 0x5e(%rsp), %ax
movw %ax, 0xaa(%rsp)
movl 0xc0(%rsp), %esi
movl 0xb0(%rsp), %ecx
movl %ecx, %eax
leal -0x1(%rax,%rax), %edx
leaq 0xd0(%rsp), %rdi
callq 0x131b0
movw %ax, 0x5c(%rsp)
jmp 0xbb98
movw 0x5c(%rsp), %ax
movw %ax, 0xa8(%rsp)
movswl 0xa8(%rsp), %edi
movswl 0xac(%rsp), %esi
movswl 0xaa(%rsp), %edx
callq 0x131f0
movw %ax, 0x5a(%rsp)
jmp 0xbbc9
movw 0x5a(%rsp), %ax
movw %ax, 0xa6(%rsp)
movswl 0xae(%rsp), %eax
movswl 0xa6(%rsp), %ecx
addl %ecx, %eax
movw %ax, 0xa4(%rsp)
movswl 0xac(%rsp), %eax
shll %eax
movswl 0xa4(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x54(%rsp)
movswl 0xa4(%rsp), %eax
cmpl $0x0, %eax
jle 0xbc2a
movswl 0xa4(%rsp), %ecx
andl $0x1, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x50(%rsp)
jmp 0xbc39
movswl 0xa4(%rsp), %eax
andl $0x1, %eax
movl %eax, 0x50(%rsp)
movl 0x54(%rsp), %eax
movl 0x50(%rsp), %ecx
addl %ecx, %eax
shrl %eax
movw %ax, 0xa2(%rsp)
movw 0xa2(%rsp), %ax
movw %ax, 0x46(%rsp)
movl 0xc0(%rsp), %esi
movl 0xb0(%rsp), %edx
addl %edx, %edx
leaq 0xd0(%rsp), %rdi
callq 0x12110
movq %rax, 0x48(%rsp)
jmp 0xbc7e
movq 0x48(%rsp), %rax
movw 0x46(%rsp), %cx
movw %cx, (%rax)
movzwl 0xa2(%rsp), %eax
movzwl 0xa4(%rsp), %ecx
subl %ecx, %eax
movw %ax, 0xa0(%rsp)
movw 0xa0(%rsp), %ax
movw %ax, 0x36(%rsp)
movl 0xc0(%rsp), %esi
movl 0xb0(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0xd0(%rsp), %rdi
callq 0x12110
movq %rax, 0x38(%rsp)
jmp 0xbcda
movq 0x38(%rsp), %rax
movw 0x36(%rsp), %cx
movw %cx, (%rax)
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0xba91
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x12380
jmp 0xbdd8
movl 0xe8(%rsp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xbd83
movq 0x118(%rsp), %rdi
movl 0xc0(%rsp), %esi
movl 0x18(%rdi), %edx
decl %edx
callq 0x12ba0
movw %ax, 0x34(%rsp)
jmp 0xbd52
movl 0xc0(%rsp), %esi
movl 0xe8(%rsp), %edx
decl %edx
leaq 0xd0(%rsp), %rdi
callq 0x12110
movq %rax, 0x28(%rsp)
jmp 0xbd76
movq 0x28(%rsp), %rax
movw 0x34(%rsp), %cx
movw %cx, (%rax)
jmp 0xbd85
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0xb86c
movq 0x128(%rsp), %rdi
movslq 0x124(%rsp), %rsi
callq 0x4ba0
movq %rax, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x12390
jmp 0xbdc2
leaq 0xd0(%rsp), %rdi
callq 0x12380
addq $0x130, %rsp            # imm = 0x130
popq %rbx
retq
movq 0xc8(%rsp), %rdi
callq 0x36a0
nopw %cs:(%rax,%rax)