#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 */
double floor(double);
double min(double, double);
double pow(double, double);
int pp_getscale(int win, int hin, double par, int wout, int hout,
                double parweight, int *sx, int *sy) {
  int sxc, syc, sxm, sym;
  int exactpar;
  double parrat;
  double errpar, errsize, err;
  double errmin;
  double parnorm;
  double srat;
  if (win <= 0 || hin <= 0 || win > wout || hin > hout || par <= 0.0)
    return -1;
  if (par > 1.0)
    parnorm = par;
  else
    parnorm = 1.0 / par;
  exactpar = parnorm - floor(parnorm) < 0.01;
  sxm = sxc = (int)floor((double)wout / win);
  sym = syc = (int)floor((double)hout / hin);
  errmin = -1;
  while (1) {
    parrat = (double)syc / sxc / par;
    if (parrat > 1.0)
      errpar = parrat;
    else
      errpar = 1.0 / parrat;
    srat = min((double)sym / syc, (double)sxm / sxc);
    if (exactpar)
      errsize = 1.0;
    else
      errsize = pow(srat, parweight);
    err = errpar * errsize;
    if (err < errmin || errmin == -1) {
      *sx = sxc;
      *sy = syc;
      errmin = err;
    }
    if (parrat < 1.0)
      sxc--;
    else
      syc--;
    if (srat >= 2.0)
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pp_getscale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movsd  %xmm1,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1193 <pp_getscale+0x63>
cmpl   $0x0,-0xc(%rbp)
jle    1193 <pp_getscale+0x63>
mov    -0x8(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1193 <pp_getscale+0x63>
mov    -0xc(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     1193 <pp_getscale+0x63>
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
jb     119f <pp_getscale+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <pp_getscale+0x285>
movsd  -0x18(%rbp),%xmm0
movsd  0xe54(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11c5 <pp_getscale+0x95>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x80(%rbp)
jmp    11d7 <pp_getscale+0xa7>
movsd  0xe33(%rip),%xmm0
divsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x80(%rbp)
movsd  -0x80(%rbp),%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
movsd  -0x80(%rbp),%xmm0
call   1050 <floor@plt>
movsd  -0x90(%rbp),%xmm1
ff
subsd  %xmm0,%xmm1
movsd  0xe0e(%rip),%xmm0
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4c(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
call   1050 <floor@plt>
cvttsd2si %xmm0,%eax
mov    %eax,-0x3c(%rbp)
mov    %eax,-0x44(%rbp)
cvtsi2sdl -0x20(%rbp),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
call   1050 <floor@plt>
cvttsd2si %xmm0,%eax
mov    %eax,-0x40(%rbp)
mov    %eax,-0x48(%rbp)
movsd  0xdb5(%rip),%xmm0
movsd  %xmm0,-0x78(%rbp)
cvtsi2sdl -0x40(%rbp),%xmm0
cvtsi2sdl -0x3c(%rbp),%xmm1
divsd  %xmm1,%xmm0
divsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  0xd83(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1296 <pp_getscale+0x166>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
jmp    12a8 <pp_getscale+0x178>
movsd  0xd62(%rip),%xmm0
divsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
cvtsi2sdl -0x48(%rbp),%xmm0
cvtsi2sdl -0x40(%rbp),%xmm1
divsd  %xmm1,%xmm0
cvtsi2sdl -0x44(%rbp),%xmm1
cvtsi2sdl -0x3c(%rbp),%xmm2
divsd  %xmm2,%xmm1
call   1030 <min@plt>
movsd  %xmm0,-0x88(%rbp)
ff
cmpl   $0x0,-0x4c(%rbp)
je     12ed <pp_getscale+0x1bd>
movsd  0xd1d(%rip),%xmm0
movsd  %xmm0,-0x68(%rbp)
jmp    1304 <pp_getscale+0x1d4>
movsd  -0x88(%rbp),%xmm0
ff
movsd  -0x28(%rbp),%xmm1
call   1040 <pow@plt>
movsd  %xmm0,-0x68(%rbp)
movsd  -0x60(%rbp),%xmm0
mulsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x70(%rbp),%xmm1
movsd  -0x78(%rbp),%xmm0
ucomisd %xmm1,%xmm0
ja     1344 <pp_getscale+0x214>
movsd  -0x78(%rbp),%xmm0
movsd  0xcd4(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1360 <pp_getscale+0x230>
jp     1360 <pp_getscale+0x230>
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
movsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  0xc98(%rip),%xmm0
ucomisd -0x58(%rbp),%xmm0
jbe    1381 <pp_getscale+0x251>
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    138a <pp_getscale+0x25a>
mov    -0x40(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x40(%rbp)
movsd  -0x88(%rbp),%xmm0
ff
movsd  0xc7e(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     13a9 <pp_getscale+0x279>
jmp    13ae <pp_getscale+0x27e>
jmp    1258 <pp_getscale+0x128>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pp_getscale>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
movsd  %xmm1,0x20(%rsp)
movsd  %xmm0,(%rsp)
mov    $0xffffffff,%eax
test   %edi,%edi
jle    1339 <pp_getscale+0x209>
mov    %esi,%ebp
test   %esi,%esi
jle    1339 <pp_getscale+0x209>
mov    %edx,%r13d
mov    %edi,%ebx
cmp    %edx,%edi
jg     1339 <pp_getscale+0x209>
xorpd  %xmm0,%xmm0
ucomisd (%rsp),%xmm0
jae    1339 <pp_getscale+0x209>
mov    %ecx,%r15d
cmp    %ecx,%ebp
jg     1339 <pp_getscale+0x209>
mov    %r9,%r14
mov    %r8,%r12
movsd  (%rsp),%xmm0
ucomisd 0xe66(%rip),%xmm0
ja     11a9 <pp_getscale+0x79>
movsd  0xe5c(%rip),%xmm0
divsd  (%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
call   1050 <floor@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %r13d,%xmm1
movsd  0x8(%rsp),%xmm2
subsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1050 <floor@plt>
movapd %xmm0,0x10(%rsp)
cvttsd2si %xmm0,%ebx
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
divsd  %xmm1,%xmm0
call   1050 <floor@plt>
cvttsd2si %xmm0,%ebp
cvttpd2dq %xmm0,%xmm0
cvtdq2pd %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
cvttpd2dq 0x10(%rsp),%xmm0
cvtdq2pd %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movsd  0xdde(%rip),%xmm0
movsd  0xdce(%rip),%xmm3
jmp    126d <pp_getscale+0x13d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,(%r12)
mov    %ebp,(%r14)
movapd %xmm1,%xmm0
xor    %eax,%eax
xor    %ecx,%ecx
ucomisd 0x38(%rsp),%xmm3
seta   %al
setbe  %cl
sub    %ecx,%ebp
sub    %eax,%ebx
ucomisd 0xdb1(%rip),%xmm2
jae    1337 <pp_getscale+0x207>
movsd  %xmm0,0x10(%rsp)
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
xorps  %xmm2,%xmm2
cvtsi2sd %ebx,%xmm2
movapd %xmm1,%xmm0
divsd  %xmm2,%xmm0
divsd  (%rsp),%xmm0
ucomisd %xmm3,%xmm0
movapd %xmm0,%xmm4
ja     12a0 <pp_getscale+0x170>
movapd %xmm3,%xmm4
divsd  %xmm0,%xmm4
movsd  %xmm4,0x30(%rsp)
movsd  %xmm0,0x38(%rsp)
movapd 0x50(%rsp),%xmm0
divsd  %xmm1,%xmm0
movapd 0x40(%rsp),%xmm1
divsd  %xmm2,%xmm1
call   1030 <min@plt>
movsd  0xd33(%rip),%xmm3
movapd %xmm0,%xmm2
movsd  0xd37(%rip),%xmm0
ucomisd 0x8(%rsp),%xmm0
movapd %xmm3,%xmm0
ja     1308 <pp_getscale+0x1d8>
movapd %xmm2,%xmm0
movsd  0x20(%rsp),%xmm1
movsd  %xmm2,0x28(%rsp)
call   1040 <pow@plt>
movsd  0x28(%rsp),%xmm2
movsd  0xcf8(%rip),%xmm3
movsd  0x30(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  0x10(%rsp),%xmm0
ucomisd 0xce8(%rip),%xmm0
jne    1328 <pp_getscale+0x1f8>
jnp    1240 <pp_getscale+0x110>
ucomisd %xmm1,%xmm0
jbe    124b <pp_getscale+0x11b>
jmp    1240 <pp_getscale+0x110>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pp_getscale(i32 %0, i32 %1, double %2, i32 %3, i32 %4, double %5, i32* %6, i32* %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca double
%13 = alloca i32
%14 = alloca i32
%15 = alloca double
%16 = alloca i32*
%17 = alloca i32*
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca double
%24 = alloca double
%25 = alloca double
%26 = alloca double
%27 = alloca double
%28 = alloca double
%29 = alloca double
store i32 %0, i32* %10
store i32 %1, i32* %11
store double %2, double* %12
store i32 %3, i32* %13
store i32 %4, i32* %14
store double %5, double* %15
store i32* %6, i32** %16
store i32* %7, i32** %17
%30 = load i32, i32* %10
%31 = icmp sle i32 %30, 0
br i1 %31, label %46, label %32
32: ; preds = %8
%33 = load i32, i32* %11
%34 = icmp sle i32 %33, 0
br i1 %34, label %46, label %35
35: ; preds = %32
%36 = load i32, i32* %10
%37 = load i32, i32* %13
%38 = icmp sgt i32 %36, %37
br i1 %38, label %46, label %39
39: ; preds = %35
%40 = load i32, i32* %11
%41 = load i32, i32* %14
%42 = icmp sgt i32 %40, %41
br i1 %42, label %46, label %43
43: ; preds = %39
%44 = load double, double* %12
%45 = fcmp ole double %44, 0.000000e+00
br i1 %45, label %46, label %47
46: ; preds = %43, %39, %35, %32, %8
store i32 -1, i32* %9
br label %141
47: ; preds = %43
%48 = load double, double* %12
%49 = fcmp ogt double %48, 1.000000e+00
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = load double, double* %12
store double %51, double* %28
br label %55
52: ; preds = %47
%53 = load double, double* %12
%54 = fdiv double 1.000000e+00, %53
store double %54, double* %28
br label %55
55: ; preds = %52, %50
%56 = load double, double* %28
%57 = load double, double* %28
%58 = call double @llvm.floor.f64(double %57)
%59 = fsub double %56, %58
%60 = fcmp olt double %59, 1.000000e-02
%61 = zext i1 %60 to i32
store i32 %61, i32* %22
%62 = load i32, i32* %13
%63 = sitofp i32 %62 to double
%64 = load i32, i32* %10
%65 = sitofp i32 %64 to double
%66 = fdiv double %63, %65
%67 = call double @llvm.floor.f64(double %66)
%68 = fptosi double %67 to i32
store i32 %68, i32* %18
store i32 %68, i32* %20
%69 = load i32, i32* %14
%70 = sitofp i32 %69 to double
%71 = load i32, i32* %11
%72 = sitofp i32 %71 to double
%73 = fdiv double %70, %72
%74 = call double @llvm.floor.f64(double %73)
%75 = fptosi double %74 to i32
store i32 %75, i32* %19
store i32 %75, i32* %21
store double -1.000000e+00, double* %27
br label %76
76: ; preds = %55, %139
%77 = load i32, i32* %19
%78 = sitofp i32 %77 to double
%79 = load i32, i32* %18
%80 = sitofp i32 %79 to double
%81 = fdiv double %78, %80
%82 = load double, double* %12
%83 = fdiv double %81, %82
store double %83, double* %23
%84 = load double, double* %23
%85 = fcmp ogt double %84, 1.000000e+00
br i1 %85, label %86, label %88
86: ; preds = %76
%87 = load double, double* %23
store double %87, double* %24
br label %91
88: ; preds = %76
%89 = load double, double* %23
%90 = fdiv double 1.000000e+00, %89
store double %90, double* %24
br label %91
91: ; preds = %88, %86
%92 = load i32, i32* %21
%93 = sitofp i32 %92 to double
%94 = load i32, i32* %19
%95 = sitofp i32 %94 to double
%96 = fdiv double %93, %95
%97 = load i32, i32* %20
%98 = sitofp i32 %97 to double
%99 = load i32, i32* %18
%100 = sitofp i32 %99 to double
%101 = fdiv double %98, %100
%102 = call double @min(double %96, double %101)
store double %102, double* %29
%103 = load i32, i32* %22
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %106
105: ; preds = %91
store double 1.000000e+00, double* %25
br label %110
106: ; preds = %91
%107 = load double, double* %29
%108 = load double, double* %15
%109 = call double @pow(double %107, double %108)
store double %109, double* %25
br label %110
110: ; preds = %106, %105
%111 = load double, double* %24
%112 = load double, double* %25
%113 = fmul double %111, %112
store double %113, double* %26
%114 = load double, double* %26
%115 = load double, double* %27
%116 = fcmp olt double %114, %115
br i1 %116, label %120, label %117
117: ; preds = %110
%118 = load double, double* %27
%119 = fcmp oeq double %118, -1.000000e+00
br i1 %119, label %120, label %126
120: ; preds = %117, %110
%121 = load i32, i32* %18
%122 = load i32*, i32** %16
store i32 %121, i32* %122
%123 = load i32, i32* %19
%124 = load i32*, i32** %17
store i32 %123, i32* %124
%125 = load double, double* %26
store double %125, double* %27
br label %126
126: ; preds = %120, %117
%127 = load double, double* %23
%128 = fcmp olt double %127, 1.000000e+00
br i1 %128, label %129, label %132
129: ; preds = %126
%130 = load i32, i32* %18
%131 = add nsw i32 %130, -1
store i32 %131, i32* %18
br label %135
132: ; preds = %126
%133 = load i32, i32* %19
%134 = add nsw i32 %133, -1
store i32 %134, i32* %19
br label %135
135: ; preds = %132, %129
%136 = load double, double* %29
%137 = fcmp oge double %136, 2.000000e+00
br i1 %137, label %138, label %139
138: ; preds = %135
br label %140
139: ; preds = %135
br label %76
140: ; preds = %138
store i32 0, i32* %9
br label %141
141: ; preds = %140, %46
%142 = load i32, i32* %9
ret i32 %142
}

LLVM IR (-O2):
define i32 @pp_getscale(i32 %0, i32 %1, double %2, i32 %3, i32 %4, double %5, i32* nocapture writeonly %6, i32* nocapture writeonly %7) {
%9 = icmp slt i32 %0, 1
%10 = icmp slt i32 %1, 1
%11 = or i1 %9, %10
%12 = icmp sgt i32 %0, %3
%13 = or i1 %11, %12
br i1 %13, label %69, label %14
14: ; preds = %8
%15 = icmp sgt i32 %1, %4
%16 = fcmp ole double %2, 0.000000e+00
%17 = or i1 %16, %15
br i1 %17, label %69, label %18
18: ; preds = %14
%19 = fcmp ogt double %2, 1.000000e+00
%20 = fdiv double 1.000000e+00, %2
%21 = select i1 %19, double %2, double %20
%22 = tail call double @llvm.floor.f64(double %21)
%23 = fsub double %21, %22
%24 = fcmp olt double %23, 1.000000e-02
%25 = sitofp i32 %3 to double
%26 = sitofp i32 %0 to double
%27 = fdiv double %25, %26
%28 = tail call double @llvm.floor.f64(double %27)
%29 = fptosi double %28 to i32
%30 = sitofp i32 %4 to double
%31 = sitofp i32 %1 to double
%32 = fdiv double %30, %31
%33 = tail call double @llvm.floor.f64(double %32)
%34 = fptosi double %33 to i32
%35 = sitofp i32 %34 to double
%36 = sitofp i32 %29 to double
br label %37
37: ; preds = %60, %18
%38 = phi i32 [ %34, %18 ], [ %65, %60 ]
%39 = phi i32 [ %29, %18 ], [ %67, %60 ]
%40 = phi double [ -1.000000e+00, %18 ], [ %61, %60 ]
%41 = sitofp i32 %38 to double
%42 = sitofp i32 %39 to double
%43 = fdiv double %41, %42
%44 = fdiv double %43, %2
%45 = fcmp ogt double %44, 1.000000e+00
%46 = fdiv double 1.000000e+00, %44
%47 = select i1 %45, double %44, double %46
%48 = fdiv double %35, %41
%49 = fdiv double %36, %42
%50 = tail call double @min(double %48, double %49)
br i1 %24, label %53, label %51
51: ; preds = %37
%52 = tail call double @pow(double %50, double %5)
br label %53
53: ; preds = %37, %51
%54 = phi double [ %52, %51 ], [ 1.000000e+00, %37 ]
%55 = fmul double %47, %54
%56 = fcmp olt double %55, %40
%57 = fcmp oeq double %40, -1.000000e+00
%58 = or i1 %57, %56
br i1 %58, label %59, label %60
59: ; preds = %53
store i32 %39, i32* %6
store i32 %38, i32* %7
br label %60
60: ; preds = %53, %59
%61 = phi double [ %55, %59 ], [ %40, %53 ]
%62 = fcmp olt double %44, 1.000000e+00
%63 = xor i1 %62, true
%64 = sext i1 %63 to i32
%65 = add nsw i32 %38, %64
%66 = sext i1 %62 to i32
%67 = add nsw i32 %39, %66
%68 = fcmp ult double %50, 2.000000e+00
br i1 %68, label %37, label %69
69: ; preds = %60, %8, %14
%70 = phi i32 [ -1, %14 ], [ -1, %8 ], [ 0, %60 ]
ret i32 %70
}