#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 */
double M_PI;
double cos(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *stderr;
void gen_costab(unsigned size) {
  FILE *f;
  int i;
  if (!(f = fopen("costab.c", "w"))) {
    fprintf(stderr, "cannot open file costab.c\n");
    exit(1);
  }
  fprintf(f,
          "#include \"costab.h\"\n\n/*\n * cosine table\n */\n\n"
          "const int16_t afsk_costab[%d] = {",
          size);
  for (i = 0; i < size; i++) {
    if (!(i & 7))
      fprintf(f, "\n\t");
    fprintf(f, "%6d", (int)(32767.0 * cos(i * (2.0 * M_PI) / size)));
    if (i != (size - 1))
      fprintf(f, ", ");
  }
  fprintf(f, "\n};\n\n");
}


x86 assembly (clang, -O0):
<gen_costab>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeb6(%rip),%rdi
lea    0xeb8(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    118e <gen_costab+0x4e>
mov    0x2e65(%rip),%rax
mov    (%rax),%rdi
lea    0xe96(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe92(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1267 <gen_costab+0x127>
mov    -0x14(%rbp),%eax
and    $0x7,%eax
cmp    $0x0,%eax
jne    11d7 <gen_costab+0x97>
mov    -0x10(%rbp),%rdi
lea    0xead(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
mov    0x2dfd(%rip),%rax
movsd  (%rax),%xmm1
addsd  %xmm1,%xmm1
mulsd  %xmm1,%xmm0
mov    -0x4(%rbp),%eax
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <cos@plt>
mov    -0x20(%rbp),%rdi
movaps %xmm0,%xmm1
movsd  0xde5(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     1254 <gen_costab+0x114>
mov    -0x10(%rbp),%rdi
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1259 <gen_costab+0x119>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11aa <gen_costab+0x6a>
mov    -0x10(%rbp),%rdi
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_costab>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xea4(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1279 <gen_costab+0x129>
mov    %rax,%r14
lea    0xeab(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
test   %ebx,%ebx
je     1252 <gen_costab+0x102>
mov    %ebx,%eax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,(%rsp)
neg    %ebx
mov    0x2e3c(%rip),%rbp
lea    0xecd(%rip),%r12
xor    %r15d,%r15d
jmp    11cf <gen_costab+0x7f>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15d
mov    %ebx,%eax
add    %r15d,%eax
je     1252 <gen_costab+0x102>
lea    (%rbx,%r15,1),%r13d
test   $0x7,%r15b
jne    11f2 <gen_costab+0xa2>
mov    $0x2,%esi
mov    $0x1,%edx
lea    0xe93(%rip),%rdi
mov    %r14,%rcx
call   1070 <fwrite@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
movsd  0x0(%rbp),%xmm1
addsd  %xmm1,%xmm1
mulsd  %xmm0,%xmm1
divsd  (%rsp),%xmm1
cvttsd2si %xmm1,%edi
call   1040 <cos@plt>
mulsd  0xde3(%rip),%xmm0
cvttsd2si %xmm0,%edx
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
cmp    $0xffffffff,%r13d
je     11c0 <gen_costab+0x70>
mov    $0x2,%esi
mov    $0x1,%edx
lea    0xe3f(%rip),%rdi
mov    %r14,%rcx
call   1070 <fwrite@plt>
jmp    11c0 <gen_costab+0x70>
lea    0xe2e(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %r14,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fwrite@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%rcx
lea    0xd89(%rip),%rdi
mov    $0x1a,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"costab.c\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [27 x i8] c"cannot open file costab.c\0A\00"
@.str.3 = [79 x i8] c"#include \22costab.h\22\0A\0A/*\0A * cosine table\0A */\0A\0Aconst int16_t afsk_costab[%d] = {\00"
@.str.4 = [3 x i8] c"\0A\09\00"
@.str.5 = [4 x i8] c"%6d\00"
@M_PI = global double 0.000000e+00
@.str.6 = [3 x i8] c", \00"
@.str.7 = [6 x i8] c"\0A};\0A\0A\00"
define void @gen_costab(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i32*, i32** @stderr
%9 = call i32 (i32*, i8*, ...) @fprintf(i32* %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = load i32*, i32** %3
%13 = load i32, i32* %2
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0), i32 %13)
store i32 0, i32* %4
br label %15
15: ; preds = %49, %11
%16 = load i32, i32* %4
%17 = load i32, i32* %2
%18 = icmp ult i32 %16, %17
br i1 %18, label %19, label %52
19: ; preds = %15
%20 = load i32, i32* %4
%21 = and i32 %20, 7
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = load i32*, i32** %3
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %23, %19
%27 = load i32*, i32** %3
%28 = load i32, i32* %4
%29 = sitofp i32 %28 to double
%30 = load double, double* @M_PI
%31 = fmul double 2.000000e+00, %30
%32 = fmul double %29, %31
%33 = load i32, i32* %2
%34 = uitofp i32 %33 to double
%35 = fdiv double %32, %34
%36 = fptosi double %35 to i32
%37 = call double @cos(i32 %36)
%38 = fmul double 3.276700e+04, %37
%39 = fptosi double %38 to i32
%40 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %4
%42 = load i32, i32* %2
%43 = sub i32 %42, 1
%44 = icmp ne i32 %41, %43
br i1 %44, label %45, label %48
45: ; preds = %26
%46 = load i32*, i32** %3
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %45, %26
br label %49
49: ; preds = %48
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %4
br label %15
52: ; preds = %15
%53 = load i32*, i32** %3
%54 = call i32 (i32*, i8*, ...) @fprintf(i32* %53, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"costab.c\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [27 x i8] c"cannot open file costab.c\0A\00"
@.str.3 = [79 x i8] c"#include \22costab.h\22\0A\0A/*\0A * cosine table\0A */\0A\0Aconst int16_t afsk_costab[%d] = {\00"
@.str.4 = [3 x i8] c"\0A\09\00"
@.str.5 = [4 x i8] c"%6d\00"
@M_PI = global double 0.000000e+00
@.str.6 = [3 x i8] c", \00"
@.str.7 = [6 x i8] c"\0A};\0A\0A\00"
define void @gen_costab(i32 %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32*, i32** @stderr
%6 = tail call i64 @fwrite(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i64 26, i64 1, i32* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%10 = icmp eq i32 %0, 0
br i1 %10, label %37, label %11
11: ; preds = %8
%12 = uitofp i32 %0 to double
%13 = add i32 %0, -1
br label %14
14: ; preds = %11, %34
%15 = phi i32 [ 0, %11 ], [ %35, %34 ]
%16 = and i32 %15, 7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i64 2, i64 1, i32* %2)
br label %20
20: ; preds = %18, %14
%21 = sitofp i32 %15 to double
%22 = load double, double* @M_PI
%23 = fmul double %22, 2.000000e+00
%24 = fmul double %23, %21
%25 = fdiv double %24, %12
%26 = fptosi double %25 to i32
%27 = tail call double @cos(i32 %26)
%28 = fmul double %27, 3.276700e+04
%29 = fptosi double %28 to i32
%30 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %15, %13
br i1 %31, label %34, label %32
32: ; preds = %20
%33 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i64 2, i64 1, i32* %2)
br label %34
34: ; preds = %20, %32
%35 = add nuw i32 %15, 1
%36 = icmp eq i32 %35, %0
br i1 %36, label %37, label %14
37: ; preds = %34, %8
%38 = tail call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i64 5, i64 1, i32* %2)
ret void
}