#include <setjmp.h>
#include <stdio.h>

/* Main program that extends example1, here the values of the 
 * buffer elements are shown.
 * The following registers are used:
 * Multipurpose registers:
 * JB_BX: ebx register
 * JB_SI: esi register
 * JB_DI: edi register
 * Special registers:
 * JB_BP: ebp, base pointer of stack
 * JB_SP: esp, stack pointer (top of stack)
 * JB_PC: epc, program counter, return address
 *
 * EAX, ECX und EDX gelten als flüchtige Register und werden
 * deshalb nicht(!) gespeichert.
 */
int main()
{
    //buffer for setjmp()/longjmp()
    //stores registers
    jmp_buf env;
    int i;

    //call setjmp
    //registers are stored to env, this is the point to that we go back
    i = setjmp(env);
    //setjmp() returns 0 in its first call, if we go back with longjmp()
    //the value is different
    printf("i = %d\n", i);

    //we exit after the first longjmp()
    if( i!= 0) exit(0);

    //we print the defined values here
    printf("\nBX: %d\n", env[0].__jmpbuf[JB_BX]);
    printf("SI: %d\n", env[0].__jmpbuf[JB_SI]);
    printf("DI: %d\n", env[0].__jmpbuf[JB_DI]);
    printf("BP: %d\n", env[0].__jmpbuf[JB_BP]);
    printf("SP: %d\n", env[0].__jmpbuf[JB_SP]);
    printf("PC: %d\n\n", env[0].__jmpbuf[JB_PC]);

    //we jump to the point that we saved with the first setjmp call
    //the return value is 2 (must be different from 0)
    longjmp(env, 2);
    
    //we cannot see it as we exit our program before
    printf("Can you see this line=\n");
}

