#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 long long uint64_t;
typedef long long uint32_t;

/* Variables and functions */
void hdd_image_calc_chs(uint32_t *c, uint32_t *h, uint32_t *s, uint32_t size) {
  uint64_t ts = ((uint64_t)size) << 11LL;
  uint32_t spt, heads, cyl, cth;
  if (ts > 65535 * 16 * 255)
    ts = 65535 * 16 * 255;
  if (ts >= 65535 * 16 * 63) {
    spt = 255;
    heads = 16;
    cth = (uint32_t)(ts / spt);
  } else {
    spt = 17;
    cth = (uint32_t)(ts / spt);
    heads = (cth + 1023) / 1024;
    if (heads < 4)
      heads = 4;
    if ((cth >= (heads * 1024)) || (heads > 16)) {
      spt = 31;
      heads = 16;
      cth = (uint32_t)(ts / spt);
    }
    if (cth >= (heads * 1024)) {
      spt = 63;
      heads = 16;
      cth = (uint32_t)(ts / spt);
    }
  }
  cyl = cth / heads;
  *c = cyl;
  *h = heads;
  *s = spt;
}


x86 assembly (clang, -O0):
<hdd_image_calc_chs>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0xb,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0xfeff010,-0x28(%rbp)
0f
jle    1136 <hdd_image_calc_chs+0x36>
movq   $0xfeff010,-0x28(%rbp)
0f
cmpq   $0x3effc10,-0x28(%rbp)
03
jl     1167 <hdd_image_calc_chs+0x67>
movq   $0xff,-0x30(%rbp)
movq   $0x10,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cqto
idivq  -0x30(%rbp)
mov    %rax,-0x48(%rbp)
jmp    121e <hdd_image_calc_chs+0x11e>
movq   $0x11,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cqto
idivq  -0x30(%rbp)
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    $0x3ff,%rax
mov    $0x400,%ecx
cqto
idiv   %rcx
mov    %rax,-0x38(%rbp)
cmpq   $0x4,-0x38(%rbp)
jge    11a8 <hdd_image_calc_chs+0xa8>
movq   $0x4,-0x38(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0xa,%rcx
cmp    %rcx,%rax
jge    11c8 <hdd_image_calc_chs+0xc8>
cmpq   $0x10,-0x38(%rbp)
jle    11e6 <hdd_image_calc_chs+0xe6>
movq   $0x1f,-0x30(%rbp)
movq   $0x10,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cqto
idivq  -0x30(%rbp)
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x38(%rbp),%rcx
shl    $0xa,%rcx
cmp    %rcx,%rax
jl     1219 <hdd_image_calc_chs+0x119>
movq   $0x3f,-0x30(%rbp)
movq   $0x10,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cqto
idivq  -0x30(%rbp)
mov    %rax,-0x48(%rbp)
jmp    121e <hdd_image_calc_chs+0x11e>
mov    -0x48(%rbp),%rax
cqto
idivq  -0x38(%rbp)
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<hdd_image_calc_chs>:
push   %rbx
mov    %rdx,%r8
shl    $0xb,%rcx
cmp    $0xfeff010,%rcx
mov    $0xfeff010,%r9d
cmovl  %rcx,%r9
cmp    $0x3effc10,%r9
jl     1142 <hdd_image_calc_chs+0x42>
mov    %r9d,%ecx
mov    $0x80808081,%eax
imul   %rcx,%rax
shr    $0x27,%rax
mov    $0x10,%ebx
mov    $0xff,%r10d
jmp    1222 <hdd_image_calc_chs+0x122>
movabs $0x7878787878787879,%rcx
78 78 78
mov    %r9,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x3,%rdx
lea    (%rdx,%rax,1),%rbx
add    $0x3ff,%rbx
lea    (%rdx,%rax,1),%rcx
add    $0x7fe,%rcx
test   %rbx,%rbx
cmovns %rbx,%rcx
lea    (%rdx,%rax,1),%r11
sar    $0xa,%rcx
cmp    $0x5,%rcx
mov    $0x4,%ebx
cmovge %rcx,%rbx
mov    %rbx,%rax
shl    $0xa,%rax
cmp    %rax,%r11
setge  %al
cmp    $0x11,%rbx
setae  %cl
or     %al,%cl
movabs $0x8421084210842109,%rdx
08 21 84
mov    %r9,%rax
imul   %rdx
mov    %rdx,%rax
add    %r9,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x4,%rax
add    %rdx,%rax
test   %cl,%cl
mov    $0x1f,%ecx
mov    $0x11,%r10d
cmovne %rcx,%r10
mov    $0x10,%ecx
cmovne %rcx,%rbx
cmove  %r11,%rax
mov    %rbx,%rcx
shl    $0xa,%rcx
cmp    %rcx,%rax
jl     1222 <hdd_image_calc_chs+0x122>
movabs $0x8208208208208209,%rcx
20 08 82
mov    %r9,%rax
imul   %rcx
mov    %rdx,%rax
add    %r9,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x5,%rax
add    %rcx,%rax
mov    $0x3f,%r10d
mov    $0x10,%ebx
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     1235 <hdd_image_calc_chs+0x135>
cqto
idiv   %rbx
jmp    1239 <hdd_image_calc_chs+0x139>
xor    %edx,%edx
div    %ebx
mov    %rax,(%rdi)
mov    %rbx,(%rsi)
mov    %r10,(%r8)
pop    %rbx
ret

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

LLVM IR (-O2):
define void @hdd_image_calc_chs(i64* nocapture writeonly %0, i64* nocapture writeonly %1, i64* nocapture writeonly %2, i64 %3) {
%5 = shl i64 %3, 11
%6 = icmp slt i64 %5, 267382800
%7 = select i1 %6, i64 %5, i64 267382800
%8 = icmp sgt i64 %7, 66059279
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = trunc i64 %7 to i32
%11 = udiv i32 %10, 255
%12 = zext i32 %11 to i64
br label %31
13: ; preds = %4
%14 = sdiv i64 %7, 17
%15 = add nsw i64 %14, 1023
%16 = sdiv i64 %15, 1024
%17 = icmp sgt i64 %16, 4
%18 = select i1 %17, i64 %16, i64 4
%19 = shl nuw nsw i64 %18, 10
%20 = icmp sge i64 %14, %19
%21 = icmp ugt i64 %18, 16
%22 = or i1 %21, %20
%23 = sdiv i64 %7, 31
%24 = select i1 %22, i64 31, i64 17
%25 = select i1 %22, i64 16, i64 %18
%26 = select i1 %22, i64 %23, i64 %14
%27 = shl nuw nsw i64 %25, 10
%28 = icmp slt i64 %26, %27
br i1 %28, label %31, label %29
29: ; preds = %13
%30 = sdiv i64 %7, 63
br label %31
31: ; preds = %13, %29, %9
%32 = phi i64 [ 255, %9 ], [ 63, %29 ], [ %24, %13 ]
%33 = phi i64 [ 16, %9 ], [ 16, %29 ], [ %25, %13 ]
%34 = phi i64 [ %12, %9 ], [ %30, %29 ], [ %26, %13 ]
%35 = sdiv i64 %34, %33
store i64 %35, i64* %0
store i64 %33, i64* %1
store i64 %32, i64* %2
ret void
}