#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 */
typedef int uint64_t;

/* Variables and functions */
double DBL_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int sqrt(int);
int /*<<< orphan*/ stderr;
int calculate_dimension(uint64_t size, double *pAspect, uint64_t *pWidth,
                        uint64_t *pHeight) {
  double aspect = *pAspect;
  uint64_t width = *pWidth, height = *pHeight;
  if (!size)
    return 1;
  if (aspect != DBL_MAX) {
    if (width || height) {
      fprintf(stderr, "Aspect with width or height is given\n");
      return 1;
    }
  } else {
    aspect = ((double)4 / 3);
  }
  if (width) {
    if (width != (width / 8) * 8) {
      fprintf(stderr, "Width must be multiple of 8\n");
      return 1;
    }
    if (height) {
      if (width * height > size * 8) {
        fprintf(stderr, "Width * height > input size\n");
        return 1;
      }
    } else {
      height = (size * 8) / width;
      if (height == 0) {
        fprintf(stderr, "Width is too large\n");
        return 1;
      }
    }
  } else {
    if (height) {
      width = (size * 8) / height;
      width = (width / 8) * 8;
      if (width == 0) {
        fprintf(stderr, "Height is too large\n");
        return 1;
      }
    } else {
      width = sqrt((size * 8) * aspect);
      width = (width / 8) * 8;
      if (width == 0)
        width = 8;
      height = (size * 8) / width;
    }
  }
  *pAspect = aspect;
  *pWidth = width;
  *pHeight = height;
  return 0;
}


x86 assembly (clang, -O0):
<calculate_dimension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    116c <calculate_dimension+0x4c>
movl   $0x1,-0x4(%rbp)
jmp    1374 <calculate_dimension+0x254>
movsd  -0x28(%rbp),%xmm0
mov    0x2e60(%rip),%rax
ucomisd (%rax),%xmm0
jne    118d <calculate_dimension+0x6d>
jp     118d <calculate_dimension+0x6d>
jmp    11c7 <calculate_dimension+0xa7>
cmpl   $0x0,-0x2c(%rbp)
jne    11a1 <calculate_dimension+0x81>
cmpl   $0x0,-0x30(%rbp)
je     11c2 <calculate_dimension+0xa2>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1374 <calculate_dimension+0x254>
jmp    11d4 <calculate_dimension+0xb4>
movsd  0xe31(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12a7 <calculate_dimension+0x187>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
shl    $0x3,%ecx
cmp    %ecx,%eax
je     1220 <calculate_dimension+0x100>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1374 <calculate_dimension+0x254>
cmpl   $0x0,-0x30(%rbp)
je     1265 <calculate_dimension+0x145>
mov    -0x2c(%rbp),%eax
imul   -0x30(%rbp),%eax
mov    -0x8(%rbp),%ecx
shl    $0x3,%ecx
cmp    %ecx,%eax
jle    1260 <calculate_dimension+0x140>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1374 <calculate_dimension+0x254>
jmp    12a2 <calculate_dimension+0x182>
mov    -0x8(%rbp),%eax
shl    $0x3,%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    129d <calculate_dimension+0x17d>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1374 <calculate_dimension+0x254>
jmp    12a2 <calculate_dimension+0x182>
jmp    134e <calculate_dimension+0x22e>
cmpl   $0x0,-0x30(%rbp)
je     12ff <calculate_dimension+0x1df>
mov    -0x8(%rbp),%eax
shl    $0x3,%eax
cltd
idivl  -0x30(%rbp)
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
shl    $0x3,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    12fa <calculate_dimension+0x1da>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1374 <calculate_dimension+0x254>
jmp    1349 <calculate_dimension+0x229>
mov    -0x8(%rbp),%eax
shl    $0x3,%eax
cvtsi2sd %eax,%xmm0
mulsd  -0x28(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <sqrt@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
shl    $0x3,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    133c <calculate_dimension+0x21c>
movl   $0x8,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x3,%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,-0x30(%rbp)
jmp    134e <calculate_dimension+0x22e>
movsd  -0x28(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_dimension>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0x1,%ebp
test   %edi,%edi
je     124e <calculate_dimension+0x12e>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebx
movsd  (%rsi),%xmm1
mov    (%rdx),%ecx
mov    (%r14),%esi
mov    0x2e84(%rip),%rax
ucomisd (%rax),%xmm1
jne    115c <calculate_dimension+0x3c>
jnp    1176 <calculate_dimension+0x56>
or     %esi,%ecx
je     1203 <calculate_dimension+0xe3>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
jmp    11f4 <calculate_dimension+0xd4>
test   %ecx,%ecx
je     11b3 <calculate_dimension+0x93>
lea    0x7(%rcx),%eax
test   %ecx,%ecx
cmovns %ecx,%eax
and    $0xfffffff8,%eax
cmp    %eax,%ecx
jne    11e4 <calculate_dimension+0xc4>
test   %esi,%esi
je     125d <calculate_dimension+0x13d>
mov    %esi,%eax
imul   %ecx,%eax
shl    $0x3,%ebx
cmp    %ebx,%eax
jle    1278 <calculate_dimension+0x158>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe9a(%rip),%rsi
jmp    11f4 <calculate_dimension+0xd4>
test   %esi,%esi
je     11fb <calculate_dimension+0xdb>
shl    $0x3,%ebx
mov    %ebx,%eax
cltd
idiv   %esi
mov    %eax,%ecx
lea    0x7(%rcx),%eax
test   %ecx,%ecx
cmovs  %eax,%ecx
cmp    $0xe,%eax
ja     1275 <calculate_dimension+0x155>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe9a(%rip),%rsi
jmp    11f4 <calculate_dimension+0xd4>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1030 <fprintf@plt>
jmp    124e <calculate_dimension+0x12e>
movsd  0xdfd(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
shl    $0x3,%ebx
cvtsi2sd %ebx,%xmm0
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <sqrt@plt>
movsd  0x8(%rsp),%xmm0
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovs  %ecx,%eax
and    $0xfffffff8,%eax
cmp    $0xf,%ecx
mov    $0x8,%ecx
cmovae %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
mov    %eax,%esi
movsd  %xmm0,(%r12)
mov    %ecx,(%r15)
mov    %esi,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
shl    $0x3,%ebx
mov    %ebx,%eax
cltd
idiv   %ecx
test   %eax,%eax
je     1282 <calculate_dimension+0x162>
mov    %eax,%esi
movsd  0xd8d(%rip),%xmm0
jmp    1240 <calculate_dimension+0x120>
and    $0xfffffff8,%ecx
movsd  0xd80(%rip),%xmm0
jmp    1240 <calculate_dimension+0x120>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
jmp    11f4 <calculate_dimension+0xd4>

LLVM IR (-O0):
@DBL_MAX = global double 0.000000e+00
@stderr = global i32 0
@.str = [38 x i8] c"Aspect with width or height is given\0A\00"
@.str.1 = [29 x i8] c"Width must be multiple of 8\0A\00"
@.str.2 = [29 x i8] c"Width * height > input size\0A\00"
@.str.3 = [20 x i8] c"Width is too large\0A\00"
@.str.4 = [21 x i8] c"Height is too large\0A\00"
define i32 @calculate_dimension(i32 %0, double* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca double
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store double* %1, double** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%13 = load double*, double** %7
%14 = load double, double* %13
store double %14, double* %10
%15 = load i32*, i32** %8
%16 = load i32, i32* %15
store i32 %16, i32* %11
%17 = load i32*, i32** %9
%18 = load i32, i32* %17
store i32 %18, i32* %12
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %4
store i32 1, i32* %5
br label %119
22: ; preds = %4
%23 = load double, double* %10
%24 = load double, double* @DBL_MAX
%25 = fcmp une double %23, %24
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i32, i32* %11
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29, %26
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %119
35: ; preds = %29
br label %37
36: ; preds = %22
store double 0x3FF5555555555555, double* %10
br label %37
37: ; preds = %36, %35
%38 = load i32, i32* %11
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %75
40: ; preds = %37
%41 = load i32, i32* %11
%42 = load i32, i32* %11
%43 = sdiv i32 %42, 8
%44 = mul nsw i32 %43, 8
%45 = icmp ne i32 %41, %44
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %5
br label %119
49: ; preds = %40
%50 = load i32, i32* %12
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %63
52: ; preds = %49
%53 = load i32, i32* %11
%54 = load i32, i32* %12
%55 = mul nsw i32 %53, %54
%56 = load i32, i32* %6
%57 = mul nsw i32 %56, 8
%58 = icmp sgt i32 %55, %57
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %5
br label %119
62: ; preds = %52
br label %74
63: ; preds = %49
%64 = load i32, i32* %6
%65 = mul nsw i32 %64, 8
%66 = load i32, i32* %11
%67 = sdiv i32 %65, %66
store i32 %67, i32* %12
%68 = load i32, i32* %12
%69 = icmp eq i32 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %63
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %5
br label %119
73: ; preds = %63
br label %74
74: ; preds = %73, %62
br label %112
75: ; preds = %37
%76 = load i32, i32* %12
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %92
78: ; preds = %75
%79 = load i32, i32* %6
%80 = mul nsw i32 %79, 8
%81 = load i32, i32* %12
%82 = sdiv i32 %80, %81
store i32 %82, i32* %11
%83 = load i32, i32* %11
%84 = sdiv i32 %83, 8
%85 = mul nsw i32 %84, 8
store i32 %85, i32* %11
%86 = load i32, i32* %11
%87 = icmp eq i32 %86, 0
br i1 %87, label %88, label %91
88: ; preds = %78
%89 = load i32, i32* @stderr
%90 = call i32 @fprintf(i32 %89, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %5
br label %119
91: ; preds = %78
br label %111
92: ; preds = %75
%93 = load i32, i32* %6
%94 = mul nsw i32 %93, 8
%95 = sitofp i32 %94 to double
%96 = load double, double* %10
%97 = fmul double %95, %96
%98 = fptosi double %97 to i32
%99 = call i32 @sqrt(i32 %98)
store i32 %99, i32* %11
%100 = load i32, i32* %11
%101 = sdiv i32 %100, 8
%102 = mul nsw i32 %101, 8
store i32 %102, i32* %11
%103 = load i32, i32* %11
%104 = icmp eq i32 %103, 0
br i1 %104, label %105, label %106
105: ; preds = %92
store i32 8, i32* %11
br label %106
106: ; preds = %105, %92
%107 = load i32, i32* %6
%108 = mul nsw i32 %107, 8
%109 = load i32, i32* %11
%110 = sdiv i32 %108, %109
store i32 %110, i32* %12
br label %111
111: ; preds = %106, %91
br label %112
112: ; preds = %111, %74
%113 = load double, double* %10
%114 = load double*, double** %7
store double %113, double* %114
%115 = load i32, i32* %11
%116 = load i32*, i32** %8
store i32 %115, i32* %116
%117 = load i32, i32* %12
%118 = load i32*, i32** %9
store i32 %117, i32* %118
store i32 0, i32* %5
br label %119
119: ; preds = %112, %88, %70, %59, %46, %32, %21
%120 = load i32, i32* %5
ret i32 %120
}

LLVM IR (-O2):
@DBL_MAX = global double 0.000000e+00
@stderr = global i32 0
@.str = [38 x i8] c"Aspect with width or height is given\0A\00"
@.str.1 = [29 x i8] c"Width must be multiple of 8\0A\00"
@.str.2 = [29 x i8] c"Width * height > input size\0A\00"
@.str.3 = [20 x i8] c"Width is too large\0A\00"
@.str.4 = [21 x i8] c"Height is too large\0A\00"
define i32 @calculate_dimension(i32 %0, double* nocapture %1, i32* nocapture %2, i32* nocapture %3) {
%5 = load double, double* %1
%6 = load i32, i32* %2
%7 = load i32, i32* %3
%8 = icmp eq i32 %0, 0
br i1 %8, label %73, label %9
9: ; preds = %4
%10 = load double, double* @DBL_MAX
%11 = fcmp une double %5, %10
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = icmp ne i32 %6, 0
%14 = icmp ne i32 %7, 0
%15 = select i1 %13, i1 true, i1 %14
br i1 %15, label %16, label %56
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %73
19: ; preds = %9
%20 = icmp eq i32 %6, 0
br i1 %20, label %44, label %21
21: ; preds = %19
%22 = sdiv i32 %6, 8
%23 = shl nsw i32 %22, 3
%24 = icmp eq i32 %6, %23
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %73
28: ; preds = %21
%29 = icmp eq i32 %7, 0
br i1 %29, label %37, label %30
30: ; preds = %28
%31 = mul nsw i32 %7, %6
%32 = shl nsw i32 %0, 3
%33 = icmp sgt i32 %31, %32
br i1 %33, label %34, label %69
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %73
37: ; preds = %28
%38 = shl nsw i32 %0, 3
%39 = sdiv i32 %38, %6
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %69
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = tail call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %73
44: ; preds = %19
%45 = icmp eq i32 %7, 0
br i1 %45, label %56, label %46
46: ; preds = %44
%47 = shl nsw i32 %0, 3
%48 = sdiv i32 %47, %7
%49 = sdiv i32 %48, 8
%50 = shl nsw i32 %49, 3
%51 = add i32 %48, 7
%52 = icmp ult i32 %51, 15
br i1 %52, label %53, label %69
53: ; preds = %46
%54 = load i32, i32* @stderr
%55 = tail call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %73
56: ; preds = %12, %44
%57 = phi double [ 0x3FF5555555555555, %44 ], [ %5, %12 ]
%58 = shl nsw i32 %0, 3
%59 = sitofp i32 %58 to double
%60 = fmul double %57, %59
%61 = fptosi double %60 to i32
%62 = tail call i32 @sqrt(i32 %61)
%63 = sdiv i32 %62, 8
%64 = shl nsw i32 %63, 3
%65 = add i32 %62, 7
%66 = icmp ult i32 %65, 15
%67 = select i1 %66, i32 8, i32 %64
%68 = sdiv i32 %58, %67
br label %69
69: ; preds = %56, %46, %30, %37
%70 = phi double [ 0x3FF5555555555555, %30 ], [ 0x3FF5555555555555, %37 ], [ 0x3FF5555555555555, %46 ], [ %57, %56 ]
%71 = phi i32 [ %6, %30 ], [ %6, %37 ], [ %50, %46 ], [ %67, %56 ]
%72 = phi i32 [ %7, %30 ], [ %39, %37 ], [ %7, %46 ], [ %68, %56 ]
store double %70, double* %1
store i32 %71, i32* %2
store i32 %72, i32* %3
br label %73
73: ; preds = %4, %69, %53, %41, %34, %25, %16
%74 = phi i32 [ 1, %16 ], [ 1, %25 ], [ 1, %34 ], [ 0, %69 ], [ 1, %41 ], [ 1, %53 ], [ 1, %4 ]
ret i32 %74
}