void IMAGE_SHRINK(unsigned char *dest_ptr, int dest_dx, int dest_dy,
                  int dest_pitch, int dest_bpp, unsigned char *src_ptr,
                  int src_dx, int src_dy, int src_pitch, int src_bpp,
                  void (*row_func)(unsigned char *, int, unsigned char *, int),
                  void (*row2x_func)(unsigned char *, unsigned char *,
                                     unsigned char *, int, unsigned char *,
                                     int)) {
  register unsigned char *d = dest_ptr;
  register unsigned char *s = src_ptr;
  register int count = dest_dy;
  register int limit = src_dy >> 1;
  register int step = dest_dy;
  if (count) {
    do {
      (*row_func)(d, dest_dx, s, src_dx);
      d += dest_pitch;
      do {
        s += src_pitch;
      } while ((limit -= step) >= 0);
      limit += src_dy;
    } while (--count);
  }
}


x86 assembly (clang, -O0):
<IMAGE_SHRINK>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x38(%rbp),%rax
mov    0x30(%rbp),%rax
mov    0x28(%rbp),%eax
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x18(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11cf <IMAGE_SHRINK+0xcf>
jmp    1165 <IMAGE_SHRINK+0x65>
mov    0x30(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    0x10(%rbp),%ecx
call   *%rax
mov    -0x14(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    0x20(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    118a <IMAGE_SHRINK+0x8a>
mov    0x18(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jne    1165 <IMAGE_SHRINK+0x65>
jmp    11cf <IMAGE_SHRINK+0xcf>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<IMAGE_SHRINK>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,0xc(%rsp)
test   %edx,%edx
je     1178 <IMAGE_SHRINK+0x78>
mov    %r9,%rbx
mov    %edx,%ebp
mov    %rdi,%r15
mov    0x58(%rsp),%r13d
sar    %r13d
movslq %ecx,%rax
mov    %rax,0x10(%rsp)
movslq 0x60(%rsp),%r14
mov    %edx,%r12d
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    0xc(%rsp),%esi
mov    %rbx,%rdx
mov    0x50(%rsp),%ecx
call   *0x70(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %r14,%rbx
sub    %ebp,%r13d
jns    1160 <IMAGE_SHRINK+0x60>
add    0x10(%rsp),%r15
add    0x58(%rsp),%r13d
add    $0xffffffff,%r12d
jne    1140 <IMAGE_SHRINK+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @IMAGE_SHRINK(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4, i8* %5, i32 %6, i32 %7, i32 %8, i32 %9, void (i8*, i32, i8*, i32)* %10, void (i8*, i8*, i8*, i32, i8*, i32)* %11) {
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca void (i8*, i32, i8*, i32)*
%24 = alloca void (i8*, i8*, i8*, i32, i8*, i32)*
%25 = alloca i8*
%26 = alloca i8*
%27 = alloca i32
%28 = alloca i32
%29 = alloca i32
store i8* %0, i8** %13
store i32 %1, i32* %14
store i32 %2, i32* %15
store i32 %3, i32* %16
store i32 %4, i32* %17
store i8* %5, i8** %18
store i32 %6, i32* %19
store i32 %7, i32* %20
store i32 %8, i32* %21
store i32 %9, i32* %22
store void (i8*, i32, i8*, i32)* %10, void (i8*, i32, i8*, i32)** %23
store void (i8*, i8*, i8*, i32, i8*, i32)* %11, void (i8*, i8*, i8*, i32, i8*, i32)** %24
%30 = load i8*, i8** %13
store i8* %30, i8** %25
%31 = load i8*, i8** %18
store i8* %31, i8** %26
%32 = load i32, i32* %15
store i32 %32, i32* %27
%33 = load i32, i32* %20
%34 = ashr i32 %33, 1
store i32 %34, i32* %28
%35 = load i32, i32* %15
store i32 %35, i32* %29
%36 = load i32, i32* %27
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %68
38: ; preds = %12
br label %39
39: ; preds = %63, %38
%40 = load void (i8*, i32, i8*, i32)*, void (i8*, i32, i8*, i32)** %23
%41 = load i8*, i8** %25
%42 = load i32, i32* %14
%43 = load i8*, i8** %26
%44 = load i32, i32* %19
%45 = load i32, i32* %16
%46 = load i8*, i8** %25
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %25
br label %49
49: ; preds = %54, %39
%50 = load i32, i32* %21
%51 = load i8*, i8** %26
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8* %53, i8** %26
br label %54
54: ; preds = %49
%55 = load i32, i32* %29
%56 = load i32, i32* %28
%57 = sub nsw i32 %56, %55
store i32 %57, i32* %28
%58 = icmp sge i32 %57, 0
br i1 %58, label %49, label %59
59: ; preds = %54
%60 = load i32, i32* %20
%61 = load i32, i32* %28
%62 = add nsw i32 %61, %60
store i32 %62, i32* %28
br label %63
63: ; preds = %59
%64 = load i32, i32* %27
%65 = add nsw i32 %64, -1
store i32 %65, i32* %27
%66 = icmp ne i32 %65, 0
br i1 %66, label %39, label %67
67: ; preds = %63
br label %68
68: ; preds = %67, %12
ret void
}

LLVM IR (-O2):
define void @IMAGE_SHRINK(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4, i8* %5, i32 %6, i32 %7, i32 %8, i32 %9, void (i8*, i32, i8*, i32)* nocapture %10, void (i8*, i8*, i8*, i32, i8*, i32)* nocapture readnone %11) {
%13 = icmp eq i32 %2, 0
br i1 %13, label %34, label %14
14: ; preds = %12
%15 = ashr i32 %7, 1
%16 = sext i32 %3 to i64
%17 = sext i32 %8 to i64
br label %18
18: ; preds = %14, %29
%19 = phi i8* [ %26, %29 ], [ %5, %14 ]
%20 = phi i32 [ %32, %29 ], [ %2, %14 ]
%21 = phi i32 [ %31, %29 ], [ %15, %14 ]
%22 = phi i8* [ %30, %29 ], [ %0, %14 ]
tail call void %10(i8* %22, i32 %1, i8* %19, i32 %6)
br label %23
23: ; preds = %23, %18
%24 = phi i8* [ %19, %18 ], [ %26, %23 ]
%25 = phi i32 [ %21, %18 ], [ %27, %23 ]
%26 = getelementptr inbounds i8, i8* %24, i64 %17
%27 = sub nsw i32 %25, %2
%28 = icmp sgt i32 %27, -1
br i1 %28, label %23, label %29
29: ; preds = %23
%30 = getelementptr inbounds i8, i8* %22, i64 %16
%31 = add nsw i32 %27, %7
%32 = add nsw i32 %20, -1
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %18
34: ; preds = %29, %12
ret void
}