// File: /workspace/github_c_makefile_star2_O0/wjmelements[P]evm/tst/evm.c
// Function: test_delegateCall
void test_delegateCall() {
    evmInit();

    // 5f365b818111156042577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01813560f81c8153803560f81c825390600101906002565b365ff3
#define PROGRAM_REVERSE \
        PUSH0, CALLDATASIZE, \
        JUMPDEST, \
        DUP2, DUP2, GT, ISZERO, PUSH1, 66, JUMPI, \
        PUSH32, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, ADD, \
        DUP2, CALLDATALOAD, PUSH1, 248, SHR, DUP2, MSTORE8, \
        DUP1, CALLDATALOAD, PUSH1, 248, SHR, DUP3, MSTORE8, \
        SWAP1, PUSH1, 1, ADD, SWAP1, \
        PUSH1, 2, JUMP, \
        JUMPDEST, \
        CALLDATASIZE, PUSH0, RETURN
    op_t reverseBytes[] = {
        PROGRAM_REVERSE
    };

    op_t createReverseBytes[] = {
        PUSH1, 70, DUP1, PUSH1, 9, RETURNDATASIZE, CODECOPY, RETURNDATASIZE, RETURN,
        PROGRAM_REVERSE
    };
#undef PROGRAM_REVERSE
    address_t from = AddressFromHex42("0x4a6f6B9fF1fc974096f9063a45Fd12bD5B928AD1");
    uint64_t gas = 0x10ad0;
    val_t value;
    value[0] = value[1] = value[2] = 0;
    data_t input;
    input.size = sizeof(createReverseBytes);
    input.content = createReverseBytes;
    result_t result = txCreate(from, gas, value, input);
    assert(UPPER(UPPER(result.status)) == 0);
    assert(LOWER(UPPER(result.status)) == 0x80d9b122);
    assert(UPPER(LOWER(result.status)) == 0xdc3a16fdc41f96cf);
    assert(LOWER(LOWER(result.status)) == 0x010ffe7e38d227c3);
    assert(result.returnData.size == sizeof(reverseBytes));
    assert(memcmp(result.returnData.content, reverseBytes, sizeof(reverseBytes)) == 0);
    assert(result.gasRemaining == 0);

#define PROGRAM_PROXY \
        CALLDATASIZE, RETURNDATASIZE, RETURNDATASIZE, CALLDATACOPY, \
        RETURNDATASIZE, \
        RETURNDATASIZE, RETURNDATASIZE, CALLDATASIZE, RETURNDATASIZE, PUSH20, ACCOUNT0, GAS, DELEGATECALL, \
        RETURNDATASIZE, DUP3, DUP1, RETURNDATACOPY, \
        SWAP1, RETURNDATASIZE, SWAP2, PUSH1, 43, JUMPI, REVERT, \
        JUMPDEST, RETURN
    op_t delegateCaller[] = {
        PROGRAM_PROXY
    };

    op_t createDelegateCaller[] = {
        PUSH1, 45,
        DUP1, PUSH1, 9, RETURNDATASIZE, CODECOPY, RETURNDATASIZE, RETURN,
        PROGRAM_PROXY
    };
#undef PROGRAM_PROXY

    gas = 0xf5b0;
    input.content = createDelegateCaller;
    input.size = sizeof(createDelegateCaller);

    result_t secondCreateResult = txCreate(from, gas, value, input);
    assert(UPPER(UPPER(secondCreateResult.status)) == 0);
    assert(LOWER(UPPER(secondCreateResult.status)) == 0x47784b21);
    assert(UPPER(LOWER(secondCreateResult.status)) == 0x780d1e1d5efcd005);
    assert(LOWER(LOWER(secondCreateResult.status)) == 0xc5fe542813b6d71e);
    assert(secondCreateResult.returnData.size == sizeof(delegateCaller));
    assert(memcmp(secondCreateResult.returnData.content, delegateCaller, sizeof(delegateCaller)) == 0);
    assert(secondCreateResult.gasRemaining == 0);

    address_t proxy = AddressFromUint256(&secondCreateResult.status);
    gas = 0x6fe8;
    input.content = createReverseBytes;
    input.size = sizeof(createReverseBytes);

    result_t reverseProxy = txCall(from, gas, proxy, value, input, NULL);
    assert(UPPER(UPPER(reverseProxy.status)) == 0);
    assert(LOWER(UPPER(reverseProxy.status)) == 0);
    assert(UPPER(LOWER(reverseProxy.status)) == 0);
    assert(LOWER(LOWER(reverseProxy.status)) == 1);
    assert(reverseProxy.returnData.size == input.size);
    for (size_t i = 0; i < input.size; i++) {
        assert(reverseProxy.returnData.content[i] == input.content[input.size - i - 1]);
    }
    assert(reverseProxy.gasRemaining == 16);

    evmFinalize();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movb $0x0, %al
callq 0xcfc0
leaq -0x50(%rbp), %rdi
leaq 0x14cb3(%rip), %rsi     # 0x1f6c0
movl $0x46, %edx
callq 0x30c0
leaq -0xa0(%rbp), %rdi
leaq 0x14ceb(%rip), %rsi     # 0x1f710
movl $0x4f, %edx
callq 0x30c0
leaq -0xb8(%rbp), %rdi
leaq 0x105c7(%rip), %rsi     # 0x1b004
addq $0x2, %rsi
callq 0x3380
movq $0x10ad0, -0xc0(%rbp) # imm = 0x10AD0
movl $0x0, -0xc4(%rbp)
movl $0x0, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movq $0x4f, -0xe0(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xc0(%rbp), %rsi
leaq -0xcc(%rbp), %rdx
movq -0xe0(%rbp), %rcx
movq -0xd8(%rbp), %r8
leaq -0x120(%rbp), %rdi
leaq -0xb8(%rbp), %rax
movq (%rax), %r9
movq %r9, (%rsp)
movq 0x8(%rax), %r9
movq %r9, 0x8(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0xe270
cmpq $0x0, -0x110(%rbp)
jne 0xaada
jmp 0xaaf9
leaq 0x1054e(%rip), %rdi     # 0x1b02f
leaq 0x10568(%rip), %rsi     # 0x1b050
movl $0x6ef, %edx            # imm = 0x6EF
leaq 0x13323(%rip), %rcx     # 0x1de17
callq 0x3040
movl $0x80d9b122, %eax       # imm = 0x80D9B122
cmpq %rax, -0x108(%rbp)
jne 0xab09
jmp 0xab28
leaq 0x1055b(%rip), %rdi     # 0x1b06b
leaq 0x10539(%rip), %rsi     # 0x1b050
movl $0x6f0, %edx            # imm = 0x6F0
leaq 0x132f4(%rip), %rcx     # 0x1de17
callq 0x3040
movabsq $-0x23c5e9023be06931, %rax # imm = 0xDC3A16FDC41F96CF
cmpq %rax, -0x100(%rbp)
jne 0xab3d
jmp 0xab5c
leaq 0x10551(%rip), %rdi     # 0x1b095
leaq 0x10505(%rip), %rsi     # 0x1b050
movl $0x6f1, %edx            # imm = 0x6F1
leaq 0x132c0(%rip), %rcx     # 0x1de17
callq 0x3040
movabsq $0x10ffe7e38d227c3, %rax # imm = 0x10FFE7E38D227C3
cmpq %rax, -0xf8(%rbp)
jne 0xab71
jmp 0xab90
leaq 0x1054f(%rip), %rdi     # 0x1b0c7
leaq 0x104d1(%rip), %rsi     # 0x1b050
movl $0x6f2, %edx            # imm = 0x6F2
leaq 0x1328c(%rip), %rcx     # 0x1de17
callq 0x3040
cmpq $0x46, -0x120(%rbp)
jne 0xab9c
jmp 0xabbb
leaq 0x1328d(%rip), %rdi     # 0x1de30
leaq 0x104a6(%rip), %rsi     # 0x1b050
movl $0x6f3, %edx            # imm = 0x6F3
leaq 0x13261(%rip), %rcx     # 0x1de17
callq 0x3040
movq -0x118(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl $0x46, %edx
callq 0x3090
cmpl $0x0, %eax
jne 0xabd7
jmp 0xabf6
leaq 0x13281(%rip), %rdi     # 0x1de5f
leaq 0x1046b(%rip), %rsi     # 0x1b050
movl $0x6f4, %edx            # imm = 0x6F4
leaq 0x13226(%rip), %rcx     # 0x1de17
callq 0x3040
cmpq $0x0, -0xf0(%rbp)
jne 0xac02
jmp 0xac21
leaq 0x1050c(%rip), %rdi     # 0x1b115
leaq 0x10440(%rip), %rsi     # 0x1b050
movl $0x6f5, %edx            # imm = 0x6F5
leaq 0x131fb(%rip), %rcx     # 0x1de17
callq 0x3040
leaq -0x150(%rbp), %rdi
leaq 0x14b31(%rip), %rsi     # 0x1f760
movl $0x2d, %edx
callq 0x30c0
leaq -0x190(%rbp), %rdi
leaq 0x14b49(%rip), %rsi     # 0x1f790
movl $0x36, %edx
callq 0x30c0
movq $0xf5b0, -0xc0(%rbp) # imm = 0xF5B0
leaq -0x190(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq $0x36, -0xe0(%rbp)
movq -0xc0(%rbp), %rsi
leaq -0xcc(%rbp), %rdx
movq -0xe0(%rbp), %rcx
movq -0xd8(%rbp), %r8
leaq -0x1d0(%rbp), %rdi
leaq -0xb8(%rbp), %rax
movq (%rax), %r9
movq %r9, (%rsp)
movq 0x8(%rax), %r9
movq %r9, 0x8(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0xe270
cmpq $0x0, -0x1c0(%rbp)
jne 0xacc7
jmp 0xace6
leaq 0x10dca(%rip), %rdi     # 0x1ba98
leaq 0x1037b(%rip), %rsi     # 0x1b050
movl $0x70e, %edx            # imm = 0x70E
leaq 0x13136(%rip), %rcx     # 0x1de17
callq 0x3040
cmpq $0x47784b21, -0x1b8(%rbp) # imm = 0x47784B21
jne 0xacf5
jmp 0xad14
leaq 0x10dc9(%rip), %rdi     # 0x1bac5
leaq 0x1034d(%rip), %rsi     # 0x1b050
movl $0x70f, %edx            # imm = 0x70F
leaq 0x13108(%rip), %rcx     # 0x1de17
callq 0x3040
movabsq $0x780d1e1d5efcd005, %rax # imm = 0x780D1E1D5EFCD005
cmpq %rax, -0x1b0(%rbp)
jne 0xad29
jmp 0xad48
leaq 0x10dcb(%rip), %rdi     # 0x1bafb
leaq 0x10319(%rip), %rsi     # 0x1b050
movl $0x710, %edx            # imm = 0x710
leaq 0x130d4(%rip), %rcx     # 0x1de17
callq 0x3040
movabsq $-0x3a01abd7ec4928e2, %rax # imm = 0xC5FE542813B6D71E
cmpq %rax, -0x1a8(%rbp)
jne 0xad5d
jmp 0xad7c
leaq 0x10dd5(%rip), %rdi     # 0x1bb39
leaq 0x102e5(%rip), %rsi     # 0x1b050
movl $0x711, %edx            # imm = 0x711
leaq 0x130a0(%rip), %rcx     # 0x1de17
callq 0x3040
cmpq $0x2d, -0x1d0(%rbp)
jne 0xad88
jmp 0xada7
leaq 0x1311b(%rip), %rdi     # 0x1deaa
leaq 0x102ba(%rip), %rsi     # 0x1b050
movl $0x712, %edx            # imm = 0x712
leaq 0x13075(%rip), %rcx     # 0x1de17
callq 0x3040
movq -0x1c8(%rbp), %rdi
leaq -0x150(%rbp), %rsi
movl $0x2d, %edx
callq 0x3090
cmpl $0x0, %eax
jne 0xadc6
jmp 0xade5
leaq 0x1311a(%rip), %rdi     # 0x1dee7
leaq 0x1027c(%rip), %rsi     # 0x1b050
movl $0x713, %edx            # imm = 0x713
leaq 0x13037(%rip), %rcx     # 0x1de17
callq 0x3040
cmpq $0x0, -0x1a0(%rbp)
jne 0xadf1
jmp 0xae10
leaq 0x10d7f(%rip), %rdi     # 0x1bb77
leaq 0x10251(%rip), %rsi     # 0x1b050
movl $0x714, %edx            # imm = 0x714
leaq 0x1300c(%rip), %rcx     # 0x1de17
callq 0x3040
leaq -0x1d0(%rbp), %rsi
addq $0x10, %rsi
leaq -0x1e8(%rbp), %rdi
callq 0x6b50
movq $0x6fe8, -0xc0(%rbp) # imm = 0x6FE8
leaq -0xa0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq $0x4f, -0xe0(%rbp)
movq -0xc0(%rbp), %rsi
leaq -0xcc(%rbp), %rdx
movq -0xe0(%rbp), %rcx
movq -0xd8(%rbp), %r8
leaq -0x228(%rbp), %rdi
leaq -0xb8(%rbp), %r10
leaq -0x1e8(%rbp), %rax
xorl %r9d, %r9d
movq (%r10), %r11
movq %r11, (%rsp)
movq 0x8(%r10), %r11
movq %r11, 0x8(%rsp)
movl 0x10(%r10), %r10d
movl %r10d, 0x10(%rsp)
movq (%rax), %r10
movq %r10, 0x18(%rsp)
movq 0x8(%rax), %r10
movq %r10, 0x20(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x28(%rsp)
callq 0xdaa0
cmpq $0x0, -0x218(%rbp)
jne 0xaec1
jmp 0xaee0
leaq 0x1307a(%rip), %rdi     # 0x1df42
leaq 0x10181(%rip), %rsi     # 0x1b050
movl $0x71c, %edx            # imm = 0x71C
leaq 0x12f3c(%rip), %rcx     # 0x1de17
callq 0x3040
cmpq $0x0, -0x210(%rbp)
jne 0xaeec
jmp 0xaf0b
leaq 0x13076(%rip), %rdi     # 0x1df69
leaq 0x10156(%rip), %rsi     # 0x1b050
movl $0x71d, %edx            # imm = 0x71D
leaq 0x12f11(%rip), %rcx     # 0x1de17
callq 0x3040
cmpq $0x0, -0x208(%rbp)
jne 0xaf17
jmp 0xaf36
leaq 0x13072(%rip), %rdi     # 0x1df90
leaq 0x1012b(%rip), %rsi     # 0x1b050
movl $0x71e, %edx            # imm = 0x71E
leaq 0x12ee6(%rip), %rcx     # 0x1de17
callq 0x3040
cmpq $0x1, -0x200(%rbp)
jne 0xaf42
jmp 0xaf61
leaq 0x1306e(%rip), %rdi     # 0x1dfb7
leaq 0x10100(%rip), %rsi     # 0x1b050
movl $0x71f, %edx            # imm = 0x71F
leaq 0x12ebb(%rip), %rcx     # 0x1de17
callq 0x3040
movq -0x228(%rbp), %rax
cmpq -0xe0(%rbp), %rax
jne 0xaf73
jmp 0xaf92
leaq 0x13064(%rip), %rdi     # 0x1dfde
leaq 0x100cf(%rip), %rsi     # 0x1b050
movl $0x720, %edx            # imm = 0x720
leaq 0x12e8a(%rip), %rcx     # 0x1de17
callq 0x3040
movq $0x0, -0x230(%rbp)
movq -0x230(%rbp), %rax
cmpq -0xe0(%rbp), %rax
jae 0xb017
movq -0x220(%rbp), %rax
movq -0x230(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq -0xd8(%rbp), %rcx
movq -0xe0(%rbp), %rdx
subq -0x230(%rbp), %rdx
subq $0x1, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0xafe2
jmp 0xb001
leaq 0x13020(%rip), %rdi     # 0x1e009
leaq 0x10060(%rip), %rsi     # 0x1b050
movl $0x722, %edx            # imm = 0x722
leaq 0x12e1b(%rip), %rcx     # 0x1de17
callq 0x3040
jmp 0xb003
movq -0x230(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x230(%rbp)
jmp 0xaf9d
cmpq $0x10, -0x1f8(%rbp)
jne 0xb023
jmp 0xb042
leaq 0x13027(%rip), %rdi     # 0x1e051
leaq 0x1001f(%rip), %rsi     # 0x1b050
movl $0x724, %edx            # imm = 0x724
leaq 0x12dda(%rip), %rcx     # 0x1de17
callq 0x3040
movb $0x0, %al
callq 0xd4d0
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nopw %cs:(%rax,%rax)