#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 */
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fl2sh(long, float *, short *, double, short);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(short *);
long fwrite(short *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void write_samples(float *sig, long no_samples, char *name) {
  FILE *fp;
  short *buf;
  if ((fp = fopen(name, "w")) == NULL) {
    fprintf(stderr, "\ncannot open output file %s\n\n", name);
    exit(-1);
  }
  if ((buf = (short *)calloc((size_t)no_samples, sizeof(short))) == NULL) {
    fprintf(stderr, "cannot allocate enough memory to buffer samples!\n");
    exit(-1);
  }
  fl2sh(no_samples, sig, buf, 1 ? 0.5 : 0.0, (short)0xFFFF);
  if (fwrite(buf, sizeof(short), (size_t)no_samples, fp) != no_samples) {
    fprintf(stderr, "could not write all samples to file %s!\n", name);
    exit(-1);
  }
  free(buf);
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_samples>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11d7 <write_samples+0x57>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1214 <write_samples+0x94>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
movsd  0xdd8(%rip),%xmm0
mov    $0xffffffff,%ecx
call   1040 <fl2sh@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x2,%esi
call   10a0 <fwrite@plt>
cmp    -0x10(%rbp),%rax
je     1277 <write_samples+0xf7>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_samples>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
lea    0xe6f(%rip),%rsi
mov    %rdx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     120a <write_samples+0x8a>
mov    %rax,%r15
mov    %r12,%rdi
mov    $0x2,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     121c <write_samples+0x9c>
mov    %rax,%rbx
movsd  0xe3a(%rip),%xmm0
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
mov    $0xffffffff,%ecx
call   1040 <fl2sh@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
cmp    %r12,%rax
jne    123d <write_samples+0xbd>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <fclose@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
jmp    124d <write_samples+0xcd>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"\0Acannot open output file %s\0A\0A\00"
@.str.2 = [50 x i8] c"cannot allocate enough memory to buffer samples!\0A\00"
@.str.3 = [41 x i8] c"could not write all samples to file %s!\0A\00"
define void @write_samples(float* %0, i64 %1, i8* %2) {
%4 = alloca float*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i16*
store float* %0, float** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %6
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %3
%18 = load i64, i64* %5
%19 = call i64 @calloc(i64 %18, i32 2)
%20 = inttoptr i64 %19 to i16*
store i16* %20, i16** %8
%21 = icmp eq i16* %20, null
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %17
%27 = load i64, i64* %5
%28 = load float*, float** %4
%29 = load i16*, i16** %8
%30 = call i32 @fl2sh(i64 %27, float* %28, i16* %29, double 5.000000e-01, i16 signext -1)
%31 = load i16*, i16** %8
%32 = load i64, i64* %5
%33 = load i32*, i32** %7
%34 = call i64 @fwrite(i16* %31, i32 2, i64 %32, i32* %33)
%35 = load i64, i64* %5
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %6
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %26
%43 = load i16*, i16** %8
%44 = call i32 @free(i16* %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 = [30 x i8] c"\0Acannot open output file %s\0A\0A\00"
@.str.2 = [50 x i8] c"cannot allocate enough memory to buffer samples!\0A\00"
@.str.3 = [41 x i8] c"could not write all samples to file %s!\0A\00"
define void @write_samples(float* %0, i64 %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %2, 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 %10
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %3
%11 = tail call i64 @calloc(i64 %1, i32 2)
%12 = inttoptr i64 %11 to i16*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %10
%19 = tail call i32 @fl2sh(i64 %1, float* %0, i16* %12, double 5.000000e-01, i16 signext -1)
%20 = tail call i64 @fwrite(i16* %12, i32 2, i64 %1, i32* %4)
%21 = icmp eq i64 %20, %1
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %18
%27 = tail call i32 @free(i16* %12)
%28 = tail call i32 @fclose(i32* %4)
ret void
}