#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void emit_c_code(const char *srcpath, const char *funame, const char *fubody) {
  FILE *out = fopen(srcpath, "w");
  if (!out) {
    fprintf(stderr, "failed to fopen %s - %s\n", srcpath, strerror(errno));
    exit(EXIT_FAILURE);
  }
  fprintf(out, "// generated %s file - DONT EDIT\n", srcpath);
  fputs("#include <stdio.h>\n", out);
  fputs("extern int somefun(int); // defined in the main program\n\n", out);
  fprintf(out, "int %s (int x, int y) {\n", funame);
  fprintf(out, "  printf(\"start of %s: x=%%d, y=%%d\\n\", x, y);\n", funame);
  fprintf(out,
          "//inserted body:\n"
          "%s\n"
          "//end of inserted body\n",
          fubody);
  fprintf(out, "} /* end of %s */\n", funame);
  fprintf(out, "/// end of generated %s file\n\n", srcpath);
  fclose(out);
}


x86 assembly (clang, -O0):
<emit_c_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11de <emit_c_code+0x7e>
mov    0x2e46(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe3e(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x20(%rbp),%rsi
lea    0xe42(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe6b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe85(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe98(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<emit_c_code>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe88(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1246 <emit_c_code+0xe6>
mov    %rax,%rbx
lea    0xe8b(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xe99(%rip),%rdi
mov    $0x13,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
lea    0xe94(%rip),%rdi
mov    $0x39,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
lea    0xeb5(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xeba(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xed6(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xeee(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xeed(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
mov    0x2d93(%rip),%rax
movslq (%rax),%rbx
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd9d(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"failed to fopen %s - %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [34 x i8] c"// generated %s file - DONT EDIT\0A\00"
@.str.3 = [20 x i8] c"#include <stdio.h>\0A\00"
@.str.4 = [58 x i8] c"extern int somefun(int); // defined in the main program\0A\0A\00"
@.str.5 = [25 x i8] c"int %s (int x, int y) {\0A\00"
@.str.6 = [48 x i8] c" printf(\22start of %s: x=%%d, y=%%d\\n\22, x, y);\0A\00"
@.str.7 = [44 x i8] c"//inserted body:\0A%s\0A//end of inserted body\0A\00"
@.str.8 = [19 x i8] c"} /* end of %s */\0A\00"
@.str.9 = [31 x i8] c"/// end of generated %s file\0A\0A\00"
define void @emit_c_code(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %22, label %12
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i32*
%16 = load i8*, i8** %4
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %3
%23 = load i32*, i32** %7
%24 = load i8*, i8** %4
%25 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i32*, i32** %7
%27 = call i32 @fputs(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32* %26)
%28 = load i32*, i32** %7
%29 = call i32 @fputs(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i32* %28)
%30 = load i32*, i32** %7
%31 = load i8*, i8** %5
%32 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %31)
%33 = load i32*, i32** %7
%34 = load i8*, i8** %5
%35 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0), i8* %34)
%36 = load i32*, i32** %7
%37 = load i8*, i8** %6
%38 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %37)
%39 = load i32*, i32** %7
%40 = load i8*, i8** %5
%41 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i8* %40)
%42 = load i32*, i32** %7
%43 = load i8*, i8** %4
%44 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i8* %43)
%45 = load i32*, i32** %7
%46 = call i32 @fclose(i32* %45)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"failed to fopen %s - %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [34 x i8] c"// generated %s file - DONT EDIT\0A\00"
@.str.3 = [20 x i8] c"#include <stdio.h>\0A\00"
@.str.4 = [58 x i8] c"extern int somefun(int); // defined in the main program\0A\0A\00"
@.str.5 = [25 x i8] c"int %s (int x, int y) {\0A\00"
@.str.6 = [48 x i8] c" printf(\22start of %s: x=%%d, y=%%d\\n\22, x, y);\0A\00"
@.str.7 = [44 x i8] c"//inserted body:\0A%s\0A//end of inserted body\0A\00"
@.str.8 = [19 x i8] c"} /* end of %s */\0A\00"
@.str.9 = [31 x i8] c"/// end of generated %s file\0A\0A\00"
define void @emit_c_code(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %15
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = sext i32 %7 to i64
%9 = inttoptr i64 %8 to i32*
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %3
%16 = tail call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%17 = tail call i64 @fwrite(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 19, i64 1, i32* %4)
%18 = tail call i64 @fwrite(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0), i64 57, i64 1, i32* %4)
%19 = tail call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %1)
%20 = tail call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0), i8* %1)
%21 = tail call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%22 = tail call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i8* %1)
%23 = tail call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i8* %0)
%24 = tail call i32 @fclose(i32* %4)
ret void
}