// File: yanniss[P]souffle-num-addon/./num256.cpp
// Function: sar_256
const char* sar_256(const char *x, const char *y) {
    thread_local static char out[STRING_LEN] = {"0x"};
    int256_t my_x(y);
    int256_t minus_zero = ((int256_t) 1) << 255;
    int256_t sign_bit = my_x & minus_zero;
    long my_y = strtol(x, NULL, 16);
    int256_t result;
    if (my_y >= 256) {
      if (sign_bit) result = -1;
      else result = 0;
    } else {  // there must be a better way to implement SAR w/ Boost, but who cares?
      for (result = my_x; my_y > 0; my_y--) {
        result = (result >> 1) | sign_bit;
      }
    }
    uint256_t unsigned_result(result);
    string str_result = unsigned_result.str(32, ios_base::hex);
    transform(str_result.begin(), str_result.end(), str_result.begin(), ::tolower);
    strcpy(out+2, str_result.c_str());
    return out;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x10(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x62850
movl $0x1, -0xa4(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa4(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x62c50
movl $0xff, -0xa8(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
callq 0x62bd0
leaq -0xe0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x62c90
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x109b0
movq %rax, -0xe8(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x62d00
cmpq $0x100, -0xe8(%rbp) # imm = 0x100
jl 0x32d5f
leaq -0xe0(%rbp), %rdi
callq 0x62d20
testb $0x1, %al
jne 0x32d1e
jmp 0x32d3d
movl $0xffffffff, -0x124(%rbp) # imm = 0xFFFFFFFF
leaq -0x120(%rbp), %rdi
leaq -0x124(%rbp), %rsi
callq 0x62d40
jmp 0x32d5a
movl $0x0, -0x128(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x62d40
jmp 0x32de0
leaq -0x120(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x62d80
cmpq $0x0, -0xe8(%rbp)
jle 0x32dde
movl $0x1, -0x194(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x194(%rbp), %rdx
callq 0x62e20
leaq -0x160(%rbp), %rdi
leaq -0x190(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x62db0
leaq -0x120(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x62ea0
movq -0xe8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0x32d6f
jmp 0x32de0
xorl %eax, %eax
movl %eax, %edx
leaq -0x1d0(%rbp), %rdi
movq %rdi, -0x238(%rbp)
leaq -0x120(%rbp), %rsi
callq 0x62910
movq -0x238(%rbp), %rsi
leaq -0x1f0(%rbp), %rdi
movq %rdi, -0x230(%rbp)
movl $0x20, %edx
movl $0x8, %ecx
callq 0x61dc0
movq -0x230(%rbp), %rdi
callq 0x10590
movq -0x230(%rbp), %rdi
movq %rax, -0x1f8(%rbp)
callq 0x103b0
movq -0x230(%rbp), %rdi
movq %rax, -0x200(%rbp)
callq 0x10590
movq %rax, -0x208(%rbp)
movq -0x1f8(%rbp), %rdi
movq -0x200(%rbp), %rsi
movq -0x208(%rbp), %rdx
movq 0x10d149(%rip), %rcx    # 0x13ffc0
callq 0x61e00
movq %rax, -0x228(%rbp)
jmp 0x32e85
movq -0x228(%rbp), %rax
movq %rax, -0x220(%rbp)
movq %fs:0x0, %rax
leaq -0xa0(%rax), %rax
addq $0x2, %rax
movq %rax, -0x248(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x102c0
movq -0x248(%rbp), %rdi
movq %rax, %rsi
callq 0x10610
movq %fs:0x0, %rax
leaq -0xa0(%rax), %rax
movq %rax, -0x240(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x10d98
movq -0x240(%rbp), %rax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x10d98
movq -0x210(%rbp), %rdi
callq 0x10bc0
nopw %cs:(%rax,%rax)