#include "third_party/chibicc/test/test.h"

void Assert(long expected, long actual, char *code) {
  if (expected != actual) {
    fprintf(stderr, "%s => %ld expected but got %ld\n", code, expected, actual);
    exit(1);
  }
}

void Assert2(long expected, long actual, char *code, char *func, int line) {
  if (expected != actual) {
    fprintf(stderr, "%s:%d: %s => expected %ld but got %ld\n", func, line, code,
            expected, actual);
    exit(1);
  }
}

void Assert128(__int128 k, __int128 x, char *code, char *func, int line) {
  if (k != x) {
    fprintf(stderr, "%s:%d: %s => want %jjd but got %jjd\n", func, line, code,
            k, x);
    exit(1);
  }
}

static int static_fn() {
  return 5;
}

int ext1 = 5;
int *ext2 = &ext1;
int ext3 = 7;

int ext_fn1(int x) {
  return x;
}

int ext_fn2(int x) {
  return x;
}

int common_ext2 = 3;
static int common_local;

int false_fn() {
  return 512;
}

int true_fn() {
  return 513;
}

int char_fn() {
  return (2 << 8) + 3;
}

int short_fn() {
  return (2 << 16) + 5;
}

int uchar_fn() {
  return (2 << 10) - 1 - 4;
}

int ushort_fn() {
  return (2 << 20) - 1 - 7;
}

int schar_fn() {
  return (2 << 10) - 1 - 4;
}

int sshort_fn() {
  return (2 << 20) - 1 - 7;
}

int add_all(int n, ...) {
  va_list ap;
  va_start(ap, n);
  int sum = 0;
  for (int i = 0; i < n; i++) sum += va_arg(ap, int);
  va_end(ap);
  return sum;
}

float add_float(float x, float y) {
  return x + y;
}

double add_double(double x, double y) {
  return x + y;
}

int add10_int(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8,
              int x9, int x10) {
  return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10;
}

float add10_float(float x1, float x2, float x3, float x4, float x5, float x6,
                  float x7, float x8, float x9, float x10) {
  return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10;
}

double add10_double(double x1, double x2, double x3, double x4, double x5,
                    double x6, double x7, double x8, double x9, double x10) {
  return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10;
}

typedef struct {
  int a, b;
  short c;
  char d;
} Ty4;

typedef struct {
  int a;
  float b;
  double c;
} Ty5;

typedef struct {
  unsigned char a[3];
} Ty6;

typedef struct {
  long a, b, c;
} Ty7;

int struct_test4(Ty4 x, int n) {
  switch (n) {
    case 0:
      return x.a;
    case 1:
      return x.b;
    case 2:
      return x.c;
    default:
      return x.d;
  }
}

int struct_test5(Ty5 x, int n) {
  switch (n) {
    case 0:
      return x.a;
    case 1:
      return x.b;
    default:
      return x.c;
  }
}

int struct_test6(Ty6 x, int n) {
  return x.a[n];
}

int struct_test7(Ty7 x, int n) {
  switch (n) {
    case 0:
      return x.a;
    case 1:
      return x.b;
    default:
      return x.c;
  }
}

Ty4 struct_test24(void) {
  return (Ty4){10, 20, 30, 40};
}

Ty5 struct_test25(void) {
  return (Ty5){10, 20, 30};
}

Ty6 struct_test26(void) {
  return (Ty6){{10, 20, 30}};
}

typedef struct {
  unsigned char a[10];
} Ty20;

typedef struct {
  unsigned char a[20];
} Ty21;

Ty20 struct_test27(void) {
  return (Ty20){{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}};
}

Ty21 struct_test28(void) {
  return (Ty21){
      {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}};
}