#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ generate_trap_protos(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ generate_traps_as_macros(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_head(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int);
void generate_trap_h(const char *path, int nr) {
  char buf[1024];
  FILE *f;
  snprintf(buf, sizeof(buf), "%s/trap%i.h", path, nr);
  f = fopen(buf, "w+");
  if (!f) {
    perror("fopen");
    exit(1);
  }
  print_head(f, "gen-syscall");
  fprintf(f, "#ifndef _MINT_TRAP%i_H\n", nr);
  fprintf(f, "#define _MINT_TRAP%i_H\n", nr);
  fprintf(f, "\n");
  fprintf(f, "#ifndef _COMPILER_H\n");
  fprintf(f, "#include <compiler.h>\n");
  fprintf(f, "#endif\n");
  fprintf(f, "\n");
  fprintf(f, "__BEGIN_DECLS\n");
  fprintf(f, "\n");
  if (1)
    generate_traps_as_macros(f, nr);
  else
    generate_trap_protos(f, nr);
  fprintf(f, "__END_DECLS\n");
  fprintf(f, "\n");
  fprintf(f, "#endif /* _MINT_TRAP%i_H */\n", nr);
  fclose(f);
}


x86 assembly (clang, -O0):
<generate_trap_h>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
mov    $0x400,%esi
lea    0xe53(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    11f0 <generate_trap_h+0x70>
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x418(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1090 <print_head@plt>
mov    -0x418(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <generate_traps_as_macros@plt>
mov    -0x418(%rbp),%rdi
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x418(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_trap_h>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebp
mov    %rdi,%rcx
lea    0xe4b(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %ebp,%r8d
call   1050 <snprintf@plt>
lea    0xe3d(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12f8 <generate_trap_h+0x158>
mov    %rax,%rbx
lea    0xe2b(%rip),%rsi
mov    %rax,%rdi
call   10a0 <print_head@plt>
lea    0xe28(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xe2d(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
lea    0xe25(%rip),%rdi
mov    $0x14,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
lea    0xe21(%rip),%rdi
mov    $0x16,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
lea    0xe1f(%rip),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
lea    0xe01(%rip),%rdi
mov    $0xe,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <generate_traps_as_macros@plt>
lea    0xde0(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
lea    0xdc7(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd10(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"%s/trap%i.h\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [12 x i8] c"gen-syscall\00"
@.str.4 = [24 x i8] c"#ifndef _MINT_TRAP%i_H\0A\00"
@.str.5 = [24 x i8] c"#define _MINT_TRAP%i_H\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [21 x i8] c"#ifndef _COMPILER_H\0A\00"
@.str.8 = [23 x i8] c"#include <compiler.h>\0A\00"
@.str.9 = [8 x i8] c"#endif\0A\00"
@.str.10 = [15 x i8] c"__BEGIN_DECLS\0A\00"
@.str.11 = [13 x i8] c"__END_DECLS\0A\00"
@.str.12 = [29 x i8] c"#endif /* _MINT_TRAP%i_H */\0A\00"
define void @generate_trap_h(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32*, i32** %6
%20 = call i32 @print_head(i32* %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32*, i32** %6
%22 = load i32, i32* %4
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = load i32*, i32** %6
%25 = load i32, i32* %4
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %25)
%27 = load i32*, i32** %6
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%29 = load i32*, i32** %6
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%31 = load i32*, i32** %6
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
%33 = load i32*, i32** %6
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
%35 = load i32*, i32** %6
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%37 = load i32*, i32** %6
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%39 = load i32*, i32** %6
%40 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%41 = load i32*, i32** %6
%42 = load i32, i32* %4
%43 = call i32 @generate_traps_as_macros(i32* %41, i32 %42)
%44 = load i32*, i32** %6
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0))
%46 = load i32*, i32** %6
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32*, i32** %6
%49 = load i32, i32* %4
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %48, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.12, i64 0, i64 0), i32 %49)
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s/trap%i.h\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [12 x i8] c"gen-syscall\00"
@.str.4 = [24 x i8] c"#ifndef _MINT_TRAP%i_H\0A\00"
@.str.5 = [24 x i8] c"#define _MINT_TRAP%i_H\0A\00"
@.str.7 = [21 x i8] c"#ifndef _COMPILER_H\0A\00"
@.str.8 = [23 x i8] c"#include <compiler.h>\0A\00"
@.str.9 = [8 x i8] c"#endif\0A\00"
@.str.10 = [15 x i8] c"__BEGIN_DECLS\0A\00"
@.str.11 = [13 x i8] c"__END_DECLS\0A\00"
@.str.12 = [29 x i8] c"#endif /* _MINT_TRAP%i_H */\0A\00"
define void @generate_trap_h(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = call i32 @print_head(i32* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%13 = call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %1)
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %1)
%15 = call i32 @fputc(i32 10, i32* %6)
%16 = call i64 @fwrite(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i64 20, i64 1, i32* %6)
%17 = call i64 @fwrite(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i64 22, i64 1, i32* %6)
%18 = call i64 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i64 7, i64 1, i32* %6)
%19 = call i32 @fputc(i32 10, i32* %6)
%20 = call i64 @fwrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0), i64 14, i64 1, i32* %6)
%21 = call i32 @fputc(i32 10, i32* %6)
%22 = call i32 @generate_traps_as_macros(i32* %6, i32 %1)
%23 = call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0), i64 12, i64 1, i32* %6)
%24 = call i32 @fputc(i32 10, i32* %6)
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.12, i64 0, i64 0), i32 %1)
%26 = call i32 @fclose(i32* %6)
ret void
}