Thanks Richard,
for sake of completeness (and anyone in the future looks for a simple
hello world example), here's what my code roughly looks like:
push_cfun(f); // f is the function who calls malloc
static void
call_hello_world(gimple* g)
{
gimple_stmt_iterator gsi = gsi_start(g);
const char* _string = "hello world\n";
const unsigned _size = strlen(_string) + 1;
// build string literal adds the addr expression
tree _string_cst = build_string_literal (_size, _string);
tree _var_decl = build_decl(UNKNOWN_LOCATION, VAR_DECL,
get_identifier("hellostring"), TREE_TYPE(_string_cst));
gassign *assign_stmt = gimple_build_assign(_var_decl, _string_cst);
gsi_insert_after(&gsi, assign_stmt, GSI_NEW_STMT);
gcall *call_stmt =
gimple_build_call(builtin_decl_explicit(BUILT_IN_PRINTF), 1, _var_decl);
gsi_insert_after (&gsi, call_stmt, GSI_NEW_STMT);
}
pop_cfun();
(TODO_update_ssa | TODO_rebuild_cgraph_edges), /* todo_flags_finish */