#include <stddef.h>

#include "stacktraverse.h"

void *
getreturnaddr(int level)
{
    switch(level) {
    case 0: {
        void *fp = __builtin_frame_address(1);
        if (!fp) return NULL;
        return __builtin_return_address(1);
    }
    case 1: {
        void *fp = __builtin_frame_address(2);
        if (!fp) return NULL;
        return __builtin_return_address(2);
    }
    case 2: {
        void *fp = __builtin_frame_address(3);
        if (!fp) return NULL;
        return __builtin_return_address(3);
    }
    case 3: {
        void *fp = __builtin_frame_address(4);
        if (!fp) return NULL;
        return __builtin_return_address(4);
    }
    case 4: {
        void *fp = __builtin_frame_address(5);
        if (!fp) return NULL;
        return __builtin_return_address(5);
    }
    case 5: {
        void *fp = __builtin_frame_address(6);
        if (!fp) return NULL;
        return __builtin_return_address(6);
    }
    case 6: {
        void *fp = __builtin_frame_address(7);
        if (!fp) return NULL;
        return __builtin_return_address(7);
    }
    case 7: {
        void *fp = __builtin_frame_address(8);
        if (!fp) return NULL;
        return __builtin_return_address(8);
    }
    case 8: {
        void *fp = __builtin_frame_address(9);
        if (!fp) return NULL;
        return __builtin_return_address(9);
    }
    case 9: {
        void *fp = __builtin_frame_address(10);
        if (!fp) return NULL;
        return __builtin_return_address(10);
    }
    case 10: {
        void *fp = __builtin_frame_address(11);
        if (!fp) return NULL;
        return __builtin_return_address(11);
    }
    case 11: {
        void *fp = __builtin_frame_address(12);
        if (!fp) return NULL;
        return __builtin_return_address(12);
    }
    case 12: {
        void *fp = __builtin_frame_address(13);
        if (!fp) return NULL;
        return __builtin_return_address(13);
    }
    case 13: {
        void *fp = __builtin_frame_address(14);
        if (!fp) return NULL;
        return __builtin_return_address(14);
    }
    case 14: {
        void *fp = __builtin_frame_address(15);
        if (!fp) return NULL;
        return __builtin_return_address(15);
    }
    case 15: {
        void *fp = __builtin_frame_address(16);
        if (!fp) return NULL;
        return __builtin_return_address(16);
    }
    case 16: {
        void *fp = __builtin_frame_address(17);
        if (!fp) return NULL;
        return __builtin_return_address(17);
    }
    case 17: {
        void *fp = __builtin_frame_address(18);
        if (!fp) return NULL;
        return __builtin_return_address(18);
    }
    case 18: {
        void *fp = __builtin_frame_address(19);
        if (!fp) return NULL;
        return __builtin_return_address(19);
    }
    case 19: {
        void *fp = __builtin_frame_address(20);
        if (!fp) return NULL;
        return __builtin_return_address(20);
    }
    case 20: {
        void *fp = __builtin_frame_address(21);
        if (!fp) return NULL;
        return __builtin_return_address(21);
    }
    case 21: {
        void *fp = __builtin_frame_address(22);
        if (!fp) return NULL;
        return __builtin_return_address(22);
    }
    case 22: {
        void *fp = __builtin_frame_address(23);
        if (!fp) return NULL;
        return __builtin_return_address(23);
    }
    case 23: {
        void *fp = __builtin_frame_address(24);
        if (!fp) return NULL;
        return __builtin_return_address(24);
    }
    case 24: {
        void *fp = __builtin_frame_address(25);
        if (!fp) return NULL;
        return __builtin_return_address(25);
    }
    case 25: {
        void *fp = __builtin_frame_address(26);
        if (!fp) return NULL;
        return __builtin_return_address(26);
    }
    case 26: {
        void *fp = __builtin_frame_address(27);
        if (!fp) return NULL;
        return __builtin_return_address(27);
    }
    case 27: {
        void *fp = __builtin_frame_address(28);
        if (!fp) return NULL;
        return __builtin_return_address(28);
    }
    case 28: {
        void *fp = __builtin_frame_address(29);
        if (!fp) return NULL;
        return __builtin_return_address(29);
    }
    case 29: {
        void *fp = __builtin_frame_address(30);
        if (!fp) return NULL;
        return __builtin_return_address(30);
    }
    case 30: {
        void *fp = __builtin_frame_address(31);
        if (!fp) return NULL;
        return __builtin_return_address(31);
    }
    case 31: {
        void *fp = __builtin_frame_address(32);
        if (!fp) return NULL;
        return __builtin_return_address(32);
    }
    default: return NULL;
    }
}

#include <stddef.h>

#include "stacktraverse.h"

void *
getframeaddr(int level)
{
    switch(level) {
    case 0: return __builtin_frame_address(1);
    case 1: return __builtin_frame_address(2);
    case 2: return __builtin_frame_address(3);
    case 3: return __builtin_frame_address(4);
    case 4: return __builtin_frame_address(5);
    case 5: return __builtin_frame_address(6);
    case 6: return __builtin_frame_address(7);
    case 7: return __builtin_frame_address(8);
    case 8: return __builtin_frame_address(9);
    case 9: return __builtin_frame_address(10);
    case 10: return __builtin_frame_address(11);
    case 11: return __builtin_frame_address(12);
    case 12: return __builtin_frame_address(13);
    case 13: return __builtin_frame_address(14);
    case 14: return __builtin_frame_address(15);
    case 15: return __builtin_frame_address(16);
    case 16: return __builtin_frame_address(17);
    case 17: return __builtin_frame_address(18);
    case 18: return __builtin_frame_address(19);
    case 19: return __builtin_frame_address(20);
    case 20: return __builtin_frame_address(21);
    case 21: return __builtin_frame_address(22);
    case 22: return __builtin_frame_address(23);
    case 23: return __builtin_frame_address(24);
    case 24: return __builtin_frame_address(25);
    case 25: return __builtin_frame_address(26);
    case 26: return __builtin_frame_address(27);
    case 27: return __builtin_frame_address(28);
    case 28: return __builtin_frame_address(29);
    case 29: return __builtin_frame_address(30);
    case 30: return __builtin_frame_address(31);
    case 31: return __builtin_frame_address(32);
    default: return NULL;
    }
}
