// File: mrlordkaj[P]i2cdevlib-pi/BMP085/BMP085.cpp
// Function: BMP085::getPressure()
int32_t BMP085::getPressure() {
    /*
    Datasheet forumla
        UP = raw pressure
        B6 = B5 - 4000
        X1 = (B2 * (B6 * B6 / 2^12)) / 2^11
        X2 = AC2 * B6 / 2^11
        X3 = X1 + X2
        B3 = ((AC1 * 4 + X3) << oss + 2) / 4
        X1 = AC3 * B6 / 2^13
        X2 = (B1 * (B6 * B6 / 2^12)) / 2^16
        X3 = ((X1 + X2) + 2) / 2^2
        B4 = AC4 * (unsigned long)(X3 + 32768) / 2^15
        B7 = ((unsigned long)UP - B3) * (50000 >> oss)
        if (B7 < 0x80000000) { p = (B7 * 2) / B4 }
        else { p = (B7 / B4) * 2 }
        X1 = (p / 2^8) * (p / 2^8)
        X1 = (X1 * 3038) / 2^16
        X2 = (-7357 * p) / 2^16
        p = p + (X1 + X2 + 3791) / 2^4
    */
    uint32_t up = getRawPressure();
    if(up == 0) return 0;
    uint8_t oss = (measureMode & 0xC0) >> 6;
    int32_t p;
    int32_t b6 = b5 - 4000;
    int32_t x1 = ((int32_t)b2 * ((b6 * b6) >> 12)) >> 11;
    int32_t x2 = ((int32_t)ac2 * b6) >> 11;
    int32_t x3 = x1 + x2;
    int32_t b3 = ((((int32_t)ac1 * 4 + x3) << oss) + 2) >> 2;
    x1 = ((int32_t)ac3 * b6) >> 13;
    x2 = ((int32_t)b1 * ((b6 * b6) >> 12)) >> 16;
    x3 = ((x1 + x2) + 2) >> 2;
    uint32_t b4 = ((uint32_t)ac4 * (uint32_t)(x3 + 32768)) >> 15;
    uint32_t b7 = ((uint32_t)up - b3) * (uint32_t)(50000UL >> oss);
    if (b7 < 0x80000000) {
        p = (b7 << 1) / b4;
    } else {
        p = (b7 / b4) << 1;
    }
    x1 = (p >> 8) * (p >> 8);
    x1 = (x1 * 3038) >> 16;
    x2 = (-7357 * p) >> 16;
    return p + ((x1 + x2 + (int32_t)3791) >> 4);
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x22470
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
jne 0x2f8b0
movl $0x0, 0x44(%rsp)
jmp 0x2fa16
movq 0x8(%rsp), %rax
movzbl 0x20(%rax), %ecx
andl $0xc0, %ecx
sarl $0x6, %ecx
movb %cl, 0x33(%rsp)
movl 0x1c(%rax), %ecx
subl $0xfa0, %ecx            # imm = 0xFA0
movl %ecx, 0x28(%rsp)
movswl 0xe(%rax), %ecx
movl 0x28(%rsp), %edx
imull 0x28(%rsp), %edx
sarl $0xc, %edx
imull %edx, %ecx
sarl $0xb, %ecx
movl %ecx, 0x24(%rsp)
movswl 0x8(%rax), %ecx
imull 0x28(%rsp), %ecx
sarl $0xb, %ecx
movl %ecx, 0x20(%rsp)
movl 0x24(%rsp), %ecx
addl 0x20(%rsp), %ecx
movl %ecx, 0x1c(%rsp)
movswl 0x6(%rax), %edx
shll $0x2, %edx
addl 0x1c(%rsp), %edx
movzbl 0x33(%rsp), %ecx
shll %cl, %edx
movl %edx, %ecx
addl $0x2, %ecx
sarl $0x2, %ecx
movl %ecx, 0x18(%rsp)
movswl 0xa(%rax), %ecx
imull 0x28(%rsp), %ecx
sarl $0xd, %ecx
movl %ecx, 0x24(%rsp)
movswl 0xc(%rax), %ecx
movl 0x28(%rsp), %edx
imull 0x28(%rsp), %edx
sarl $0xc, %edx
imull %edx, %ecx
sarl $0x10, %ecx
movl %ecx, 0x20(%rsp)
movl 0x24(%rsp), %ecx
addl 0x20(%rsp), %ecx
addl $0x2, %ecx
sarl $0x2, %ecx
movl %ecx, 0x1c(%rsp)
movzwl 0x16(%rax), %eax
movl 0x1c(%rsp), %ecx
addl $0x8000, %ecx           # imm = 0x8000
imull %ecx, %eax
shrl $0xf, %eax
movl %eax, 0x14(%rsp)
movl 0x34(%rsp), %eax
subl 0x18(%rsp), %eax
movzbl 0x33(%rsp), %ecx
movl %ecx, %ecx
movl $0xc350, %edx           # imm = 0xC350
shrq %cl, %rdx
movq %rdx, %rcx
imull %ecx, %eax
movl %eax, 0x10(%rsp)
cmpl $0x80000000, 0x10(%rsp) # imm = 0x80000000
jae 0x2f9b8
movl 0x10(%rsp), %eax
shll %eax
xorl %edx, %edx
divl 0x14(%rsp)
movl %eax, 0x2c(%rsp)
jmp 0x2f9c8
movl 0x10(%rsp), %eax
xorl %edx, %edx
divl 0x14(%rsp)
shll %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
sarl $0x8, %eax
movl 0x2c(%rsp), %ecx
sarl $0x8, %ecx
imull %ecx, %eax
movl %eax, 0x24(%rsp)
imull $0xbde, 0x24(%rsp), %eax # imm = 0xBDE
sarl $0x10, %eax
movl %eax, 0x24(%rsp)
imull $0xffffe343, 0x2c(%rsp), %eax # imm = 0xFFFFE343
sarl $0x10, %eax
movl %eax, 0x20(%rsp)
movl 0x2c(%rsp), %eax
movl 0x24(%rsp), %ecx
addl 0x20(%rsp), %ecx
addl $0xecf, %ecx            # imm = 0xECF
sarl $0x4, %ecx
addl %ecx, %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nop