<commit_before>c_template = """
#include <libcgc.h>
#include <stdlib.h>
#include <boolector.h>
#include <string.h>

/* global output data */
char *output;
size_t output_size;

enum register_t
{
    eax = 0,
    ecx = 1,
    edx = 2,
    ebx = 3,
    esp = 4,
    ebp = 5,
    esi = 6,
    edi = 7
};

void die(const char *str) {
    transmit(2, str, strlen(str), NULL);
    _terminate(1);
}

void debug_str(const char *str) {
    transmit(2, str, strlen(str), NULL);
}

/*
 * Test file descriptor readiness.
 */

int fd_ready(int fd) {
  struct timeval tv;
  fd_set rfds;
  int readyfds = 0;

  FD_SET(fd, &rfds);

  tv.tv_sec = 1;
  tv.tv_usec = 0;

  int ret;
  ret = fdwait(fd + 1, &rfds, NULL, &tv, &readyfds);

  /* bail if fdwait fails */
  if (ret != 0) {
    return 0;
  }
  if (readyfds == 0)
    return 0;

  return 1;
}

size_t receive_n( int fd, unsigned char *dst, size_t n_bytes )
{
  size_t len = 0;
  size_t rx = 0;
  while(len < n_bytes) {
    if (receive(fd, dst + len, n_bytes - len, &rx) != 0) {
      len = 0;
      break;
    }
    len += rx;
  }

  return len;
}

/*
 * Receive n_bytes into no particular buffer.
 */
size_t blank_receive( int fd, size_t n_bytes )
{
  size_t len = 0;
  size_t rx = 0;
  char junk_byte;

  while (len < n_bytes) {
    if (!fd_ready(fd)) {
        return len;
    }
    if (receive(fd, &junk_byte, 1, &rx) != 0) {
      len = 0;
      break;
    }
    len += rx;
  }

  return len;
}

int send_all(int fd, const void *msg, size_t n_bytes)
{
  size_t len = 0;
  size_t tx = 0;
  while(len < n_bytes) {
    if (transmit(fd, (char *)msg + len, n_bytes - len, &tx) != 0) {
      return 1;
    }
    len += tx;
  }
  return 0;
}

char to_char(char *str) {
  int i;
  char r = '\\0';

  /* result can '0', '1' or 'x', if 'x' just 0 */
  for(i=0;i<8;i++)
    r |= ((str[7-i] - '0') & 1) << i;

  return r;
}

void to_bits(char *dst, char c) {
    int i;
    for(i=0;i<8;i++) {
        dst[i] = '0' + ((c & (1 << (7-i))) >> (7-i));
    }
}

void get_output(size_t n_bytes) {
    unsigned char *buf = malloc(n_bytes);
    if (!buf)
      die("receive buffer malloc failed");

    if (receive_n(1, buf, n_bytes) != n_bytes)
        goto error;

    // convert it to a bitstring
    //  
    if (output)
        output = realloc(output, output_size + (n_bytes * 8) + 1); 
    else
        output = malloc(n_bytes * 8 + 1); 

    char *bitbuf = output;
    if (!bitbuf)
      die("bitbuf malloc failed");

    size_t i;
    for(i=0;i<n_bytes;i++) {
        to_bits(bitbuf + output_size + (i * 8), buf[i]);
    }   

    bitbuf[output_size + n_bytes * 8] = '\\0';

    /* update output size */
    output_size = output_size + n_bytes * 8;

error:
    free(buf);
}

typedef struct type1_vals_ {
   unsigned int ipval;
   unsigned int regval;
} type1_vals;

typedef struct type2_vals_ {
   unsigned int region_addr;
   unsigned int region_size;
   unsigned int read_size;
} type2_vals;


/*
 * Negotiate a type 1 pov. Caller specifies an ip bit mask, a register bit mask
 * and a general purpose register number (see the list below).
 *
   0 - eax
   1 - ecx
   2 - edx
   3 - ebx
   4 - esp
   5 - ebp
   6 - esi
   7 - edi
 *
 * Returns 0 on success. On success, the t1vals structure holds required IP
 * and register values that must be found when the target CB crashes. At the
 * time of the crash the following must hold:<commit_msg>get_output should always block and should receive from fd 0 for now<commit_after>c_template = """
#include <libcgc.h>
#include <stdlib.h>
#include <boolector.h>
#include <string.h>

/* global output data */
char *output;
size_t output_size;

enum register_t
{
    eax = 0,
    ecx = 1,
    edx = 2,
    ebx = 3,
    esp = 4,
    ebp = 5,
    esi = 6,
    edi = 7
};

void die(const char *str) {
    transmit(2, str, strlen(str), NULL);
    _terminate(1);
}

void debug_str(const char *str) {
    transmit(2, str, strlen(str), NULL);
}

/*
 * Test file descriptor readiness.
 */

int fd_ready(int fd) {
  struct timeval tv;
  fd_set rfds;
  int readyfds = 0;

  FD_SET(fd, &rfds);

  tv.tv_sec = 1;
  tv.tv_usec = 0;

  int ret;
  ret = fdwait(fd + 1, &rfds, NULL, &tv, &readyfds);

  /* bail if fdwait fails */
  if (ret != 0) {
    return 0;
  }
  if (readyfds == 0)
    return 0;

  return 1;
}

size_t receive_n( int fd, unsigned char *dst, size_t n_bytes )
{
  size_t len = 0;
  size_t rx = 0;
  while(len < n_bytes) {
    if (receive(fd, dst + len, n_bytes - len, &rx) != 0) {
      len = 0;
      break;
    }
    len += rx;
  }

  return len;
}

/*
 * Receive n_bytes into no particular buffer.
 */
size_t blank_receive( int fd, size_t n_bytes )
{
  size_t len = 0;
  size_t rx = 0;
  char junk_byte;

  while (len < n_bytes) {
    if (!fd_ready(fd)) {
        return len;
    }
    if (receive(fd, &junk_byte, 1, &rx) != 0) {
      len = 0;
      break;
    }
    len += rx;
  }

  return len;
}

int send_all(int fd, const void *msg, size_t n_bytes)
{
  size_t len = 0;
  size_t tx = 0;
  while(len < n_bytes) {
    if (transmit(fd, (char *)msg + len, n_bytes - len, &tx) != 0) {
      return 1;
    }
    len += tx;
  }
  return 0;
}

char to_char(char *str) {
  int i;
  char r = '\\0';

  /* result can '0', '1' or 'x', if 'x' just 0 */
  for(i=0;i<8;i++)
    r |= ((str[7-i] - '0') & 1) << i;

  return r;
}

void to_bits(char *dst, char c) {
    int i;
    for(i=0;i<8;i++) {
        dst[i] = '0' + ((c & (1 << (7-i))) >> (7-i));
    }
}

void get_output(size_t n_bytes) {
    unsigned char *buf = malloc(n_bytes);
    if (!buf)
      die("receive buffer malloc failed");

    receive_n(0, buf, n_bytes);

    // convert it to a bitstring
    //  
    if (output)
        output = realloc(output, output_size + (n_bytes * 8) + 1); 
    else
        output = malloc(n_bytes * 8 + 1); 

    char *bitbuf = output;
    if (!bitbuf)
      die("bitbuf malloc failed");

    size_t i;
    for(i=0;i<n_bytes;i++) {
        to_bits(bitbuf + output_size + (i * 8), buf[i]);
    }   

    bitbuf[output_size + n_bytes * 8] = '\\0';

    /* update output size */
    output_size = output_size + n_bytes * 8;

    free(buf);
}

typedef struct type1_vals_ {
   unsigned int ipval;
   unsigned int regval;
} type1_vals;

typedef struct type2_vals_ {
   unsigned int region_addr;
   unsigned int region_size;
   unsigned int read_size;
} type2_vals;


/*
 * Negotiate a type 1 pov. Caller specifies an ip bit mask, a register bit mask
 * and a general purpose register number (see the list below).
 *
   0 - eax
   1 - ecx
   2 - edx
   3 - ebx
   4 - esp
   5 - ebp
   6 - esi
   7 - edi
 *
 * Returns 0 on success. On success, the t1vals structure holds required IP
 * and register values that must be found when the target CB crashes. At the
 * time of the crash the following must hold:
{'fraction_non_alphanumeric': 0.1435116760271948, 'fraction_numerical': 0.022021874076263673, 'mean_word_length': 2.3204121687929344, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5209096', 'n_tokens_mistral': 2835, 'n_tokens_neox': 2675, 'n_words': 815}