#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ free(void *);
scalar_t__ is_multiple(int, int);
void *malloc(int);
int mipmap_image(void *, int, int, int, unsigned char *, int, int);
int up_scale_image(unsigned char const *const, int, int, int, void *, int, int);
int upper_multiple(int, int);
int scale_image(const unsigned char *const orig, int width, int height,
                int channels, unsigned char *resampled, int rwidth,
                int rheight) {
  if (width < rwidth && height < rheight) {
    return up_scale_image(orig, width, height, channels, resampled, rwidth,
                          rheight);
  } else if (is_multiple(width, rwidth) && is_multiple(height, rheight)) {
    return mipmap_image(orig, width, height, channels, resampled,
                        width / rwidth, height / rheight);
  } else {
    int twidth = upper_multiple(width, rwidth);
    int theight = upper_multiple(height, rheight);
    void *pixels = malloc(twidth * theight * channels);
    if (!pixels)
      return 0;
    if (!up_scale_image(orig, width, height, channels, pixels, twidth,
                        theight)) {
      free(pixels);
      return 0;
    }
    int ret = mipmap_image(pixels, twidth, theight, channels, resampled,
                           twidth / rwidth, theight / rheight);
    free(pixels);
    return ret;
  }
}


x86 assembly (clang, -O0):
<scale_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    11c0 <scale_image+0x60>
mov    -0x18(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    11c0 <scale_image+0x60>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
mov    0x10(%rbp),%eax
mov    %eax,(%rsp)
call   1060 <up_scale_image@plt>
mov    %eax,-0x4(%rbp)
jmp    12e9 <scale_image+0x189>
mov    -0x14(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1050 <is_multiple@plt>
cmp    $0x0,%rax
je     1222 <scale_image+0xc2>
mov    -0x18(%rbp),%edi
mov    0x10(%rbp),%esi
call   1050 <is_multiple@plt>
cmp    $0x0,%rax
je     1222 <scale_image+0xc2>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x14(%rbp),%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,%r9d
mov    -0x18(%rbp),%eax
cltd
idivl  0x10(%rbp)
mov    -0x48(%rbp),%edx
mov    %eax,(%rsp)
call   1080 <mipmap_image@plt>
mov    %eax,-0x4(%rbp)
jmp    12e9 <scale_image+0x189>
mov    -0x14(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1040 <upper_multiple@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
mov    0x10(%rbp),%esi
call   1040 <upper_multiple@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%edi
imul   -0x34(%rbp),%edi
imul   -0x1c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1269 <scale_image+0x109>
movl   $0x0,-0x4(%rbp)
jmp    12e9 <scale_image+0x189>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x40(%rbp),%r8
mov    -0x30(%rbp),%r9d
mov    -0x34(%rbp),%eax
mov    %eax,(%rsp)
call   1060 <up_scale_image@plt>
cmp    $0x0,%eax
jne    12a7 <scale_image+0x147>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e9 <scale_image+0x189>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x34(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,%r9d
mov    -0x34(%rbp),%eax
cltd
idivl  0x10(%rbp)
mov    -0x4c(%rbp),%edx
mov    %eax,(%rsp)
call   1080 <mipmap_image@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scale_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%r13d
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x60(%rsp),%r12d
cmp    %r9d,%esi
jge    11aa <scale_image+0x4a>
cmp    %r12d,%ebp
jge    11aa <scale_image+0x4a>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %r15d,%ecx
mov    %r13d,%r9d
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <up_scale_image@plt>
mov    %r8,0x20(%rsp)
mov    %ebx,%edi
mov    %r13d,%esi
call   1050 <is_multiple@plt>
test   %rax,%rax
je     11f8 <scale_image+0x98>
mov    %ebp,%edi
mov    %r12d,%esi
call   1050 <is_multiple@plt>
test   %rax,%rax
je     11f8 <scale_image+0x98>
mov    %ebx,%eax
cltd
idiv   %r13d
mov    %eax,%r9d
mov    %ebp,%eax
cltd
idiv   %r12d
mov    %eax,(%rsp)
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %r15d,%ecx
mov    0x20(%rsp),%r8
call   1080 <mipmap_image@plt>
jmp    129f <scale_image+0x13f>
mov    %ebx,%edi
mov    %r13d,%esi
call   1040 <upper_multiple@plt>
mov    %r15d,0x1c(%rsp)
mov    %eax,%r15d
mov    %ebp,%edi
mov    %r12d,%esi
call   1040 <upper_multiple@plt>
mov    %r15d,0x18(%rsp)
mov    %r15d,%edi
mov    0x1c(%rsp),%r15d
imul   %r15d,%edi
mov    %eax,0x14(%rsp)
imul   %eax,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     129d <scale_image+0x13d>
mov    0x14(%rsp),%ecx
mov    %ecx,(%rsp)
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %r15d,%ecx
mov    %rax,%rbp
mov    %rax,%r8
mov    0x18(%rsp),%ebx
mov    %ebx,%r9d
call   1060 <up_scale_image@plt>
test   %eax,%eax
je     1295 <scale_image+0x135>
mov    %ebx,%eax
cltd
idiv   %r13d
mov    %eax,%r9d
mov    0x14(%rsp),%ecx
mov    %ecx,%eax
cltd
idiv   %r12d
mov    %eax,(%rsp)
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %ecx,%edx
mov    %r15d,%ecx
mov    0x20(%rsp),%r8
call   1080 <mipmap_image@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    129f <scale_image+0x13f>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @scale_image(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5, i32 %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i32
store i8* %0, i8** %9
store i32 %1, i32* %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i8* %4, i8** %13
store i32 %5, i32* %14
store i32 %6, i32* %15
%20 = load i32, i32* %10
%21 = load i32, i32* %14
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %7
%24 = load i32, i32* %11
%25 = load i32, i32* %15
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %36
27: ; preds = %23
%28 = load i8*, i8** %9
%29 = load i32, i32* %10
%30 = load i32, i32* %11
%31 = load i32, i32* %12
%32 = load i8*, i8** %13
%33 = load i32, i32* %14
%34 = load i32, i32* %15
%35 = call i32 @up_scale_image(i8* %28, i32 %29, i32 %30, i32 %31, i8* %32, i32 %33, i32 %34)
store i32 %35, i32* %8
br label %104
36: ; preds = %23, %7
%37 = load i32, i32* %10
%38 = load i32, i32* %14
%39 = call i64 @is_multiple(i32 %37, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %59
41: ; preds = %36
%42 = load i32, i32* %11
%43 = load i32, i32* %15
%44 = call i64 @is_multiple(i32 %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %59
46: ; preds = %41
%47 = load i8*, i8** %9
%48 = load i32, i32* %10
%49 = load i32, i32* %11
%50 = load i32, i32* %12
%51 = load i8*, i8** %13
%52 = load i32, i32* %10
%53 = load i32, i32* %14
%54 = sdiv i32 %52, %53
%55 = load i32, i32* %11
%56 = load i32, i32* %15
%57 = sdiv i32 %55, %56
%58 = call i32 @mipmap_image(i8* %47, i32 %48, i32 %49, i32 %50, i8* %51, i32 %54, i32 %57)
store i32 %58, i32* %8
br label %104
59: ; preds = %41, %36
%60 = load i32, i32* %10
%61 = load i32, i32* %14
%62 = call i32 @upper_multiple(i32 %60, i32 %61)
store i32 %62, i32* %16
%63 = load i32, i32* %11
%64 = load i32, i32* %15
%65 = call i32 @upper_multiple(i32 %63, i32 %64)
store i32 %65, i32* %17
%66 = load i32, i32* %16
%67 = load i32, i32* %17
%68 = mul nsw i32 %66, %67
%69 = load i32, i32* %12
%70 = mul nsw i32 %68, %69
%71 = call i8* @malloc(i32 %70)
store i8* %71, i8** %18
%72 = load i8*, i8** %18
%73 = icmp ne i8* %72, null
br i1 %73, label %75, label %74
74: ; preds = %59
store i32 0, i32* %8
br label %104
75: ; preds = %59
%76 = load i8*, i8** %9
%77 = load i32, i32* %10
%78 = load i32, i32* %11
%79 = load i32, i32* %12
%80 = load i8*, i8** %18
%81 = load i32, i32* %16
%82 = load i32, i32* %17
%83 = call i32 @up_scale_image(i8* %76, i32 %77, i32 %78, i32 %79, i8* %80, i32 %81, i32 %82)
%84 = icmp ne i32 %83, 0
br i1 %84, label %88, label %85
85: ; preds = %75
%86 = load i8*, i8** %18
%87 = call i32 @free(i8* %86)
store i32 0, i32* %8
br label %104
88: ; preds = %75
%89 = load i8*, i8** %18
%90 = load i32, i32* %16
%91 = load i32, i32* %17
%92 = load i32, i32* %12
%93 = load i8*, i8** %13
%94 = load i32, i32* %16
%95 = load i32, i32* %14
%96 = sdiv i32 %94, %95
%97 = load i32, i32* %17
%98 = load i32, i32* %15
%99 = sdiv i32 %97, %98
%100 = call i32 @mipmap_image(i8* %89, i32 %90, i32 %91, i32 %92, i8* %93, i32 %96, i32 %99)
store i32 %100, i32* %19
%101 = load i8*, i8** %18
%102 = call i32 @free(i8* %101)
%103 = load i32, i32* %19
store i32 %103, i32* %8
br label %104
104: ; preds = %88, %85, %74, %46, %27
%105 = load i32, i32* %8
ret i32 %105
}

LLVM IR (-O2):
define i32 @scale_image(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5, i32 %6) {
%8 = icmp slt i32 %1, %5
%9 = icmp slt i32 %2, %6
%10 = and i1 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @up_scale_image(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5, i32 %6)
br label %40
13: ; preds = %7
%14 = tail call i64 @is_multiple(i32 %1, i32 %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %13
%17 = tail call i64 @is_multiple(i32 %2, i32 %6)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = sdiv i32 %1, %5
%21 = sdiv i32 %2, %6
%22 = tail call i32 @mipmap_image(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %20, i32 %21)
br label %40
23: ; preds = %16, %13
%24 = tail call i32 @upper_multiple(i32 %1, i32 %5)
%25 = tail call i32 @upper_multiple(i32 %2, i32 %6)
%26 = mul i32 %24, %3
%27 = mul i32 %26, %25
%28 = tail call i8* @malloc(i32 %27)
%29 = icmp eq i8* %28, null
br i1 %29, label %40, label %30
30: ; preds = %23
%31 = tail call i32 @up_scale_image(i8* %0, i32 %1, i32 %2, i32 %3, i8* %28, i32 %24, i32 %25)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = tail call i32 @free(i8* %28)
br label %40
35: ; preds = %30
%36 = sdiv i32 %24, %5
%37 = sdiv i32 %25, %6
%38 = tail call i32 @mipmap_image(i8* %28, i32 %24, i32 %25, i32 %3, i8* %4, i32 %36, i32 %37)
%39 = tail call i32 @free(i8* %28)
br label %40
40: ; preds = %33, %35, %23, %19, %11
%41 = phi i32 [ %12, %11 ], [ %22, %19 ], [ %38, %35 ], [ 0, %33 ], [ 0, %23 ]
ret i32 %41
}