// File: /workspace/github_c_makefile_star2_O2/BernardTatin[P]s7sh/s7.c
// Function: g_read_string
static s7_pointer g_read_string(s7_scheme *sc, s7_pointer args)
    {
        /* read-chars would be a better name -- read-string could mean CL-style read-from-string (like eval-string)
         *   similarly read-bytes could return a byte-vector (rather than r7rs's read-bytevector)
         *   and write-string -> write-chars, write-bytevector -> write-bytes
         */
#define H_read_string "(read-string k port) reads k characters from port into a new string and returns it."
#define Q_read_string s7_make_signature(sc, 3, s7_make_signature(sc, 2, sc->is_string_symbol, sc->is_eof_object_symbol), sc->is_integer_symbol, sc->is_input_port_symbol)
        s7_pointer k, port, s;
        s7_int i, nchars;
        uint8_t *str;

        k = car(args);
        if (!s7_is_integer(k))
            return(method_or_bust(sc, k, sc->read_string_symbol, args, T_INTEGER, 1));
        nchars = s7_integer(k);
        if (nchars < 0)
            return(wrong_type_argument_with_type(sc, sc->read_string_symbol, 1, wrap_integer1(sc, nchars), a_non_negative_integer_string));
        if (nchars > sc->max_string_length)
            return(out_of_range(sc, sc->read_string_symbol, small_int(1), wrap_integer1(sc, nchars), its_too_large_string));

        if (!is_null(cdr(args)))
            port = cadr(args);
        else
        {
            port = input_port_if_not_loading(sc);
            if (!port) return(eof_object);
        }
        if (!is_input_port(port))
            return(method_or_bust_with_type(sc, port, sc->read_string_symbol, list_2(sc, make_integer(sc, nchars), port), an_input_port_string, 2));
        if (port_is_closed(port))
            return(simple_wrong_type_argument_with_type(sc, sc->read_string_symbol, port, an_open_port_string));

        if (nchars == 0)
            return(make_empty_string(sc, 0, 0));

        s = make_empty_string(sc, nchars, 0);
        str = (uint8_t *)string_value(s);
        if (is_string_port(port))
        {
            s7_int pos, end, len;
            pos = port_position(port);
            end = port_data_size(port);
            len = end - pos;
            if (len > nchars) len = nchars;
            if (len <= 0) return(eof_object);
            memcpy((void *)str, (void *)(port_data(port) + pos), len);
            string_length(s) = len;
            str[len] = '\0';
            port_position(port) += len;
            return(s);
        }
        if (is_file_port(port))
        {
            size_t len;
            len = fread((void *)str, 1, nchars, port_file(port));
            str[len] = '\0';
            string_length(s) = len;
            return(s);
        }
        for (i = 0; i < nchars; i++)
        {
            int32_t c;
            c = port_read_character(port)(sc, port);
            if (c == EOF)
            {
                if (i == 0)
                    return(eof_object);
                string_length(s) = i;
                return(s);
            }
            str[i] = (uint8_t)c;
        }
        return(s);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rcx
movq %rdi, %rbx
movq 0x8(%rsi), %rsi
cmpb $0xb, (%rsi)
jne 0x4f827
movq 0x8(%rsi), %r15
testq %r15, %r15
js 0x4f84c
cmpq 0x2a8(%rbx), %r15
jle 0x4f894
movq 0x1458(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0xc6160(%rip), %rdx     # 0x115958
movq 0x1f00(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x1f00(%rbx), %rcx
movq 0xca22f(%rip), %r8      # 0x119a40
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcb8f
movq 0x1458(%rbx), %rdx
pushq $0xb
popq %r8
pushq $0x1
popq %r9
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcd70
movq 0x1458(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0xc60fa(%rip), %rdx     # 0x115958
movq 0x1f00(%rbx), %rax
movq %r15, 0x8(%rax)
movq 0x1f00(%rbx), %rcx
movq 0x140(%rbx), %r8
movq 0xd235a(%rip), %r9      # 0x121bd8
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9bbb
movq 0x10(%rcx), %rax
cmpq 0x110(%rbx), %rax
je 0x4f8a7
movq 0x8(%rax), %r14
jmp 0x4f8bb
movq %rbx, %rdi
callq 0xc7406
movq %rax, %r14
testq %rax, %rax
je 0x4fac6
cmpb $0x23, (%r14)
jne 0x4f8fd
movq 0x8(%r14), %rax
cmpb $0x1, 0x2(%rax)
jne 0x4f921
movq 0x1458(%rbx), %rax
movq 0x8(%rax), %rsi
movq 0x140(%rbx), %rcx
movq 0xd1a44(%rip), %r8      # 0x121328
movq %rbx, %rdi
movq %r14, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd456
movq 0x1458(%rbx), %r12
cmpq $0x7ff, %r15            # imm = 0x7FF
ja 0x4f994
leaq 0xc6038(%rip), %rax     # 0x115950
movq (%rax,%r15,8), %r13
jmp 0x4f9ce
testq %r15, %r15
je 0x4fa5b
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xd239
movq %rax, %r12
movq 0x10(%rax), %r13
movq 0x8(%r14), %rax
movl 0x4(%rax), %ecx
testl %ecx, %ecx
je 0x4fa75
cmpl $0x1, %ecx
jne 0x4fa94
movq 0x18(%r14), %rbx
movq 0x20(%r14), %rsi
subq %rsi, %rbx
cmpq %r15, %rbx
cmovgeq %r15, %rbx
testq %rbx, %rbx
jle 0x4fac6
addq 0x10(%r14), %rsi
movq %r13, %rdi
movq %rbx, %rdx
callq 0x83e0
movq %rbx, 0x8(%r12)
movb $0x0, (%r13,%rbx)
addq %rbx, 0x20(%r14)
jmp 0x4facd
movq 0x90(%rbx), %rax
cmpq 0x98(%rbx), %rax
ja 0x4f9b3
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %r13
movq $0xb, (%r13)
movq %r15, 0x8(%r13)
movq 0x90(%rbx), %rax
movq 0x110(%rbx), %r15
cmpq 0x98(%rbx), %rax
ja 0x4f9f4
movq %rbx, %rdi
callq 0x9c27
movq 0x90(%rbx), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0x90(%rbx)
movq -0x8(%rax), %rax
movl $0x200001, %edx         # imm = 0x200001
movq %rdx, (%rax)
movq %r14, 0x8(%rax)
movq %r15, 0x10(%rax)
movq 0x90(%rbx), %rcx
leaq -0x8(%rcx), %rsi
movq %rsi, 0x90(%rbx)
movq -0x8(%rcx), %rcx
movq %rdx, (%rcx)
movq %r13, 0x8(%rcx)
movq %rax, 0x10(%rcx)
movq 0xca1c5(%rip), %r8      # 0x119c00
pushq $0x2
popq %r9
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xcf9a
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd239
movq 0x8(%rax), %rcx
pushq $0x1
popq %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x8110
movb $0x0, (%r13,%rax)
movq %rax, 0x8(%r12)
jmp 0x4facd
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x4facd
movq 0x8(%r14), %rax
movq 0x48(%rax), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *(%rax)
cmpl $-0x1, %eax
je 0x4faba
movb %al, (%r13,%rbp)
incq %rbp
jmp 0x4fa96
testq %rbp, %rbp
je 0x4fac6
movq %rbp, 0x8(%r12)
jmp 0x4facd
movq 0xc5c93(%rip), %r12     # 0x115760
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq