// File: /workspace/github_c_makefile_star2_O2/BernardTatin[P]s7sh/s7.c
// Function: read_file
static s7_pointer read_file(s7_scheme *sc, FILE *fp, const char *name, s7_int max_size, const char *caller)
    {
        s7_pointer port;
#if (!MS_WINDOWS)
        s7_int size;
#endif
        s7_int port_loc;
        block_t *b;

        new_cell(sc, port, T_INPUT_PORT);
        port_loc = s7_gc_protect_1(sc, port);
        b = mallocate_port(sc);
        port_block(port) = b;
        port_port(port) = (port_t *)block_data(b);
        port_set_closed(port, false);
        port_original_input_string(port) = sc->nil;
        /* if we're constantly opening files, and each open saves the file name in permanent memory, we gradually core-up. */
        port_filename_length(port) = safe_strlen(name);
        port_set_filename(sc, port, name, port_filename_length(port));
        port_line_number(port) = 1;  /* first line is numbered 1 */
        port_file_number(port) = 0;
        add_input_port(sc, port);

#if (!MS_WINDOWS)
        /* this doesn't work in MS C */
        fseek(fp, 0, SEEK_END);
        size = ftell(fp);
        rewind(fp);

        /* pseudo files (under /proc for example) have size=0, but we can read them, so don't assume a 0 length file is empty */

        if ((size > 0) &&                          /* if (size != 0) we get (open-input-file "/dev/tty") -> (open "/dev/tty") read 0 bytes of an expected -1? */
                ((max_size < 0) || (size < max_size))) /* load uses max_size = -1 */
        {
            size_t bytes;
            block_t *block;
            uint8_t *content;

            block = mallocate(sc, size + 2);
            content = (uint8_t *)(block_data(block));
            bytes = fread(content, sizeof(uint8_t), size, fp);
            if (bytes != (size_t)size)
            {
                if (sc->output_port != sc->F)
                {
                    char tmp[256];
                    int32_t len;
                    len = snprintf(tmp, 256, "(%s \"%s\") read %ld bytes of an expected %" print_s7_int "?", caller, name, (long)bytes, size);
                    port_write_string(sc->output_port)(sc, tmp, len, sc->output_port);
                }
                size = bytes;
            }
            content[size] = '\0';
            content[size + 1] = '\0';
            fclose(fp);

            port_file(port) = NULL; /* make valgrind happy */
            port_type(port) = STRING_PORT;
            port_data(port) = content;
            port_data_block(port) = block;
            port_data_size(port) = size;
            port_position(port) = 0;
            port_needs_free(port) = true;
            port_needs_unprotect(port) = false;
            port_port(port)->pf = &input_string_functions_1;
        }
        else
        {
            port_file(port) = fp;
            port_type(port) = FILE_PORT;
            port_data(port) = NULL;
            port_data_block(port) = NULL;
            port_data_size(port) = 0;
            port_position(port) = 0;
            port_needs_free(port) = false;
            port_port(port)->pf = &input_file_functions;
        }
#else
        /* _stat64 is no better than the fseek/ftell route, and
         *    GetFileSizeEx and friends requires Windows.h which makes hash of everything else.
         *    fread until done takes too long on big files, so use a file port
         */
        port_file(port) = fp;
        port_type(port) = FILE_PORT;
        port_needs_free(port) = false;
        port_data(port) = NULL;
        port_data_block(port) = NULL;
        port_data_size(port) = 0;
        port_position(port) = 0;
        port_port(port)->pf = &input_file_functions;
#endif

        s7_gc_unprotect_at(sc, port_loc);
        return(port);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %r8, 0x28(%rsp)
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
movq 0x90(%rdi), %rax
cmpq 0x98(%rdi), %rax
ja 0x73eec
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r14
movq $0x23, (%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x89a9
movq %rax, %rbp
movq %rbx, %rdi
callq 0xda55
movq %rax, 0x28(%r14)
movq (%rax), %rax
movq %rax, 0x8(%r14)
movb $0x0, 0x2(%rax)
movq 0x110(%rbx), %rax
movq 0x8(%r14), %rcx
movq %rax, 0x40(%rcx)
movq %r13, %rdi
callq 0x8d06
movq 0x8(%r14), %rcx
movq %rax, 0x30(%rcx)
movq 0x8(%r14), %rax
movq 0x30(%rax), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, 0x20(%rsp)
movq %r13, %rdx
callq 0xdaaa
movq 0x8(%r14), %rax
movl $0x1, 0x20(%rax)
movq 0x8(%r14), %rax
andl $0x0, 0x24(%rax)
movq 0x748(%rbx), %rdi
movq %r14, %rsi
callq 0xb999
pushq $0x2
popq %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x8500
movq %r12, %rdi
callq 0x83a0
movq %rax, %r13
movq %r12, %rdi
callq 0x81e0
testq %r13, %r13
setle %al
cmpq %r15, %r13
setae %cl
orb %al, %cl
jne 0x73fea
movq %rbp, 0x18(%rsp)
leaq 0x2(%r13), %rsi
movq %rbx, %rdi
callq 0x9037
movq %rax, 0x10(%rsp)
movq (%rax), %r15
pushq $0x1
popq %rsi
movq %r15, %rdi
movq %r13, %rdx
movq %r12, %rcx
callq 0x8110
cmpq %r13, %rax
jne 0x74023
movq %r13, %rbp
jmp 0x7407e
movq 0x8(%r14), %rax
movq %r12, 0x8(%rax)
movq 0x8(%r14), %rax
andl $0x0, 0x4(%rax)
andq $0x0, 0x10(%r14)
movq 0x8(%r14), %rax
andq $0x0, 0x38(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r14)
movq 0x8(%r14), %rax
movb $0x0, (%rax)
leaq 0xa14e2(%rip), %rax     # 0x115500
jmp 0x740d5
movq %rax, %rbp
movq 0x188(%rbx), %rax
cmpq 0x120(%rbx), %rax
je 0x7407e
movq %r13, (%rsp)
leaq 0x6e789(%rip), %rdx     # 0xe27ca
leaq 0x30(%rsp), %r13
movl $0x100, %esi            # imm = 0x100
movq %r13, %rdi
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0x8210
movq 0x188(%rbx), %rcx
movq 0x8(%rcx), %rdx
movq 0x48(%rdx), %r8
movslq %eax, %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x10(%r8)
andw $0x0, (%r15,%rbp)
movq %r12, %rdi
callq 0x8180
movq 0x8(%r14), %rax
andq $0x0, 0x8(%rax)
movq 0x8(%r14), %rax
movl $0x1, 0x4(%rax)
movq %r15, 0x10(%r14)
movq 0x8(%r14), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x38(%rax)
movq %rbp, 0x18(%r14)
andq $0x0, 0x20(%r14)
movq 0x8(%r14), %rax
movb $0x1, (%rax)
movq 0x8(%r14), %rax
movb $0x0, 0x1(%rax)
leaq 0xa13e0(%rip), %rax     # 0x1154b0
movq 0x18(%rsp), %rbp
movq 0x8(%r14), %rcx
movq %rax, 0x48(%rcx)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x8b00
movq %r14, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq