/* utils.c generated by valac 0.56.19, the Vala compiler
 * generated from utils.vala, do not modify */

/* Copyright 2009-2015 -- pancake, ritesh */

#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <stdio.h>
#include <vala.h>
#include <glib/gstdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define _vala_code_node_unref0(var) ((var == NULL) ? NULL : (var = (vala_code_node_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _fclose0(var) ((var == NULL) ? NULL : (var = (fclose (var), NULL)))

VALA_EXTERN gchar** include_dirs;

VALA_EXTERN void notice (const gchar* msg);
VALA_EXTERN void warning (const gchar* msg);
VALA_EXTERN void error (const gchar* msg);
VALA_EXTERN gint array_length (ValaArrayType* array);
VALA_EXTERN gchar* get_enums_for (const gchar* str,
                      GList* includefiles);
static void _vala_array_add1 (gchar** * array,
                       gint* length,
                       gint* size,
                       gchar* value);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);

void
notice (const gchar* msg)
{
	FILE* _tmp0_;
	g_return_if_fail (msg != NULL);
	_tmp0_ = stderr;
	fprintf (_tmp0_, "\x1b[34;1mNOTICE\x1b[0m %s\n", msg);
}

void
warning (const gchar* msg)
{
	FILE* _tmp0_;
	g_return_if_fail (msg != NULL);
	_tmp0_ = stderr;
	fprintf (_tmp0_, "\x1b[33;1mWARNING\x1b[0m %s\n", msg);
}

void
error (const gchar* msg)
{
	FILE* _tmp0_;
	g_return_if_fail (msg != NULL);
	_tmp0_ = stderr;
	fprintf (_tmp0_, "\x1b[31;1mERROR\x1b[0m %s\n", msg);
	exit (1);
}

static gpointer
_vala_code_node_ref0 (gpointer self)
{
	return self ? vala_code_node_ref (self) : NULL;
}

gint
array_length (ValaArrayType* array)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_;
	gboolean _tmp2_;
	gint result;
	g_return_val_if_fail (array != NULL, -1);
	_tmp1_ = vala_array_type_get_fixed_length (array);
	_tmp2_ = _tmp1_;
	if (_tmp2_) {
		ValaExpression* _tmp3_;
		ValaExpression* _tmp4_;
		_tmp3_ = vala_array_type_get_length (array);
		_tmp4_ = _tmp3_;
		_tmp0_ = G_TYPE_CHECK_INSTANCE_TYPE (_tmp4_, VALA_TYPE_INTEGER_LITERAL);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		ValaIntegerLiteral* lit = NULL;
		ValaExpression* _tmp5_;
		ValaExpression* _tmp6_;
		ValaIntegerLiteral* _tmp7_;
		ValaIntegerLiteral* _tmp8_;
		const gchar* _tmp9_;
		const gchar* _tmp10_;
		_tmp5_ = vala_array_type_get_length (array);
		_tmp6_ = _tmp5_;
		_tmp7_ = _vala_code_node_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp6_, VALA_TYPE_INTEGER_LITERAL, ValaIntegerLiteral));
		lit = _tmp7_;
		_tmp8_ = lit;
		_tmp9_ = vala_integer_literal_get_value (_tmp8_);
		_tmp10_ = _tmp9_;
		result = atoi (_tmp10_);
		_vala_code_node_unref0 (lit);
		return result;
	}
	result = -1;
	return result;
}

static void
_vala_array_add1 (gchar** * array,
                  gint* length,
                  gint* size,
                  gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

gchar*
get_enums_for (const gchar* str,
               GList* includefiles)
{
	gchar* enums_exec = NULL;
	gchar* enums_out = NULL;
	gchar* _tmp0_;
	gchar* cc = NULL;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	gboolean _tmp8_ = FALSE;
	const gchar* _tmp9_;
	gchar** cc_args = NULL;
	const gchar* _tmp12_;
	gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* _tmp16_;
	const gchar* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	gchar** _tmp20_;
	gint cc_args_length1;
	gint _cc_args_size_;
	gchar** _tmp21_;
	gint _tmp21__length1;
	const gchar* _tmp52_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (str != NULL, NULL);
	_tmp0_ = g_strdup ("");
	enums_out = _tmp0_;
	{
		gint _tmp1_ = 0;
		gchar* _tmp2_ = NULL;
		gint _tmp3_;
		_tmp3_ = g_file_open_tmp ("vbeXXXXXX", &_tmp2_, &_inner_error0_);
		_g_free0 (enums_exec);
		enums_exec = _tmp2_;
		_tmp1_ = _tmp3_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_FILE_ERROR) {
				goto __catch0_g_file_error;
			}
			_g_free0 (enums_out);
			_g_free0 (enums_exec);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		close (_tmp1_);
	}
	goto __finally0;
	__catch0_g_file_error:
	{
		GError* e = NULL;
		GError* _tmp4_;
		const gchar* _tmp5_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp4_ = e;
		_tmp5_ = _tmp4_->message;
		error (_tmp5_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (enums_out);
		_g_free0 (enums_exec);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	_tmp6_ = g_getenv ("CC");
	_tmp7_ = g_strdup (_tmp6_);
	cc = _tmp7_;
	_tmp9_ = cc;
	if (_tmp9_ == NULL) {
		_tmp8_ = TRUE;
	} else {
		const gchar* _tmp10_;
		_tmp10_ = cc;
		_tmp8_ = g_strcmp0 (_tmp10_, "") == 0;
	}
	if (_tmp8_) {
		gchar* _tmp11_;
		_tmp11_ = g_strdup ("cc");
		_g_free0 (cc);
		cc = _tmp11_;
	}
	_tmp12_ = cc;
	_tmp13_ = g_strdup (_tmp12_);
	_tmp14_ = g_strdup ("-x");
	_tmp15_ = g_strdup ("c");
	_tmp16_ = g_strdup ("-o");
	_tmp17_ = enums_exec;
	_tmp18_ = g_strdup (_tmp17_);
	_tmp19_ = g_strdup ("-");
	_tmp20_ = g_new0 (gchar*, 6 + 1);
	_tmp20_[0] = _tmp13_;
	_tmp20_[1] = _tmp14_;
	_tmp20_[2] = _tmp15_;
	_tmp20_[3] = _tmp16_;
	_tmp20_[4] = _tmp18_;
	_tmp20_[5] = _tmp19_;
	cc_args = _tmp20_;
	cc_args_length1 = 6;
	_cc_args_size_ = cc_args_length1;
	_tmp21_ = include_dirs;
	_tmp21__length1 = _vala_array_length (include_dirs);
	{
		gchar** i_collection = NULL;
		gint i_collection_length1 = 0;
		gint _i_collection_size_ = 0;
		gint i_it = 0;
		i_collection = _tmp21_;
		i_collection_length1 = _tmp21__length1;
		for (i_it = 0; i_it < i_collection_length1; i_it = i_it + 1) {
			gchar* _tmp22_;
			gchar* i = NULL;
			_tmp22_ = g_strdup (i_collection[i_it]);
			i = _tmp22_;
			{
				const gchar* _tmp23_;
				gchar* _tmp24_;
				_tmp23_ = i;
				_tmp24_ = g_strconcat ("-I", _tmp23_, NULL);
				_vala_array_add1 (&cc_args, &cc_args_length1, &_cc_args_size_, _tmp24_);
				_g_free0 (i);
			}
		}
	}
	{
		GPid cc_pid = 0;
		gint cc_stdinfd = 0;
		gchar** _tmp25_;
		gint _tmp25__length1;
		GPid _tmp26_ = 0;
		gint _tmp27_ = 0;
		FILE* cc_stdin = NULL;
		FILE* _tmp28_;
		FILE* _tmp29_;
		FILE* _tmp38_;
		gint status = 0;
		gint _tmp39_ = 0;
		const gchar* _tmp42_;
		gchar* _tmp43_;
		gchar** _tmp44_;
		gchar** _tmp45_;
		gint _tmp45__length1;
		gchar* _tmp46_ = NULL;
		gint _tmp47_ = 0;
		_tmp25_ = cc_args;
		_tmp25__length1 = cc_args_length1;
		g_spawn_async_with_pipes (NULL, _tmp25_, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &_tmp26_, &_tmp27_, NULL, NULL, &_inner_error0_);
		cc_pid = _tmp26_;
		cc_stdinfd = _tmp27_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_SPAWN_ERROR) {
				goto __catch1_g_spawn_error;
			}
			cc_args = (_vala_array_free (cc_args, cc_args_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (cc);
			_g_free0 (enums_out);
			_g_free0 (enums_exec);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp28_ = fdopen (cc_stdinfd, "w");
		cc_stdin = _tmp28_;
		_tmp29_ = cc_stdin;
		if (_tmp29_ == NULL) {
			const gchar* _tmp30_;
			gchar* _tmp31_;
			gchar* _tmp32_;
			GError* _tmp33_;
			GError* _tmp34_;
			_tmp30_ = cc;
			_tmp31_ = g_strdup_printf ("Cannot open %s's stdin", _tmp30_);
			_tmp32_ = _tmp31_;
			_tmp33_ = g_error_new_literal (G_SPAWN_ERROR, G_SPAWN_ERROR_IO, _tmp32_);
			_tmp34_ = _tmp33_;
			_g_free0 (_tmp32_);
			_inner_error0_ = _tmp34_;
			_fclose0 (cc_stdin);
			if (_inner_error0_->domain == G_SPAWN_ERROR) {
				goto __catch1_g_spawn_error;
			}
			cc_args = (_vala_array_free (cc_args, cc_args_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (cc);
			_g_free0 (enums_out);
			_g_free0 (enums_exec);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		{
			GList* i_collection = NULL;
			GList* i_it = NULL;
			i_collection = includefiles;
			for (i_it = i_collection; i_it != NULL; i_it = i_it->next) {
				gchar* _tmp35_;
				gchar* i = NULL;
				_tmp35_ = g_strdup ((const gchar*) i_it->data);
				i = _tmp35_;
				{
					FILE* _tmp36_;
					const gchar* _tmp37_;
					_tmp36_ = cc_stdin;
					_tmp37_ = i;
					fprintf (_tmp36_, "#include <%s>\n", _tmp37_);
					_g_free0 (i);
				}
			}
		}
		_tmp38_ = cc_stdin;
		fprintf (_tmp38_, "int main(){%s;return 0;}\n", str);
		_fclose0 (cc_stdin);
		cc_stdin = NULL;
		waitpid ((pid_t) cc_pid, &_tmp39_, 0);
		status = _tmp39_;
		g_spawn_close_pid (cc_pid);
		if (status != 0) {
			const gchar* _tmp40_;
			GError* _tmp41_;
			_tmp40_ = cc;
			_tmp41_ = g_error_new (G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "%s exited with status %d", _tmp40_, status);
			_inner_error0_ = _tmp41_;
			_fclose0 (cc_stdin);
			if (_inner_error0_->domain == G_SPAWN_ERROR) {
				goto __catch1_g_spawn_error;
			}
			cc_args = (_vala_array_free (cc_args, cc_args_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (cc);
			_g_free0 (enums_out);
			_g_free0 (enums_exec);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp42_ = enums_exec;
		_tmp43_ = g_strdup (_tmp42_);
		_tmp44_ = g_new0 (gchar*, 1 + 1);
		_tmp44_[0] = _tmp43_;
		_tmp45_ = _tmp44_;
		_tmp45__length1 = 1;
		g_spawn_sync (NULL, _tmp45_, NULL, 0, NULL, NULL, &_tmp46_, NULL, &_tmp47_, &_inner_error0_);
		_g_free0 (enums_out);
		enums_out = _tmp46_;
		status = _tmp47_;
		_tmp45_ = (_vala_array_free (_tmp45_, _tmp45__length1, (GDestroyNotify) g_free), NULL);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_fclose0 (cc_stdin);
			if (_inner_error0_->domain == G_SPAWN_ERROR) {
				goto __catch1_g_spawn_error;
			}
			cc_args = (_vala_array_free (cc_args, cc_args_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (cc);
			_g_free0 (enums_out);
			_g_free0 (enums_exec);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		if (status != 0) {
			GError* _tmp48_;
			_tmp48_ = g_error_new (G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "enums helper exited with status %d", status);
			_inner_error0_ = _tmp48_;
			_fclose0 (cc_stdin);
			if (_inner_error0_->domain == G_SPAWN_ERROR) {
				goto __catch1_g_spawn_error;
			}
			cc_args = (_vala_array_free (cc_args, cc_args_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (cc);
			_g_free0 (enums_out);
			_g_free0 (enums_exec);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_fclose0 (cc_stdin);
	}
	goto __finally1;
	__catch1_g_spawn_error:
	{
		GError* e = NULL;
		const gchar* _tmp49_;
		GError* _tmp50_;
		const gchar* _tmp51_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp49_ = enums_exec;
		g_unlink (_tmp49_);
		_tmp50_ = e;
		_tmp51_ = _tmp50_->message;
		error (_tmp51_);
		_g_error_free0 (e);
	}
	__finally1:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		cc_args = (_vala_array_free (cc_args, cc_args_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (cc);
		_g_free0 (enums_out);
		_g_free0 (enums_exec);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	_tmp52_ = enums_exec;
	g_unlink (_tmp52_);
	result = enums_out;
	cc_args = (_vala_array_free (cc_args, cc_args_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (cc);
	_g_free0 (enums_exec);
	return result;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

