#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*/ int16_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ f2s16(float);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
int write(int, int /*<<< orphan*/ *, int);
int write_raw(const char *filename, float *input, size_t frames) {
  int16_t *buf;
  int rc = -1;
  int n = frames * 4;
  int i;
  buf = (int16_t *)malloc(n);
  for (i = 0; i < frames; i++) {
    buf[2 * i] = f2s16(input[i]);
    buf[2 * i + 1] = f2s16(input[i + frames]);
  }
  int fd = open(filename, O_WRONLY | O_CREAT, 0644);
  if (fd < 0) {
    fprintf(stderr, "cannot open file %s\n", filename);
    goto quit;
  }
  if (write(fd, buf, n) != n) {
    fprintf(stderr, "short write file %s\n", filename);
    goto quit;
  }
  rc = 0;
quit:
  close(fd);
  free(buf);
  return rc;
}


x86 assembly (clang, -O0):
<write_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1214 <write_raw+0xa4>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
call   1090 <f2s16@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    -0x18(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
call   1090 <f2s16@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a9 <write_raw+0x39>
mov    -0x8(%rbp),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    125f <write_raw+0xef>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdab(%rip),%rsi
call   1060 <fprintf@plt>
jmp    129c <write_raw+0x12c>
mov    -0x30(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <write@plt>
cmp    -0x28(%rbp),%eax
je     1295 <write_raw+0x125>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd8a(%rip),%rsi
call   1060 <fprintf@plt>
jmp    129c <write_raw+0x12c>
movl   $0x0,-0x24(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_raw>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,(%rsp)
lea    0x0(,%rdx,4),%r15d
mov    %r15d,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %r13,%r13
je     11d9 <write_raw+0x69>
lea    (%r12,%r13,4),%rbp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movss  (%r12,%r14,4),%xmm0
call   1090 <f2s16@plt>
mov    %eax,(%rbx,%r14,8)
movss  0x0(%rbp,%r14,4),%xmm0
call   1090 <f2s16@plt>
mov    %eax,0x4(%rbx,%r14,8)
add    $0x1,%r14
cmp    %r14,%r13
jne    11b0 <write_raw+0x40>
mov    0x2de8(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    (%rsp),%r14
mov    %r14,%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1220 <write_raw+0xb0>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
xor    %r12d,%r12d
cmp    %r15d,%eax
je     123e <write_raw+0xce>
lea    0xdf7(%rip),%rsi
jmp    1227 <write_raw+0xb7>
lea    0xdd9(%rip),%rsi
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%r12d
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"cannot open file %s\0A\00"
@.str.1 = [21 x i8] c"short write file %s\0A\00"
define i32 @write_raw(i8* %0, float* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca float*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store float* %1, float** %5
store i64 %2, i64* %6
store i32 -1, i32* %8
%12 = load i64, i64* %6
%13 = mul i64 %12, 4
%14 = trunc i64 %13 to i32
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %7
store i32 0, i32* %10
br label %18
18: ; preds = %49, %3
%19 = load i32, i32* %10
%20 = sext i32 %19 to i64
%21 = load i64, i64* %6
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %52
23: ; preds = %18
%24 = load float*, float** %5
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds float, float* %24, i64 %26
%28 = load float, float* %27
%29 = call i32 @f2s16(float %28)
%30 = load i32*, i32** %7
%31 = load i32, i32* %10
%32 = mul nsw i32 2, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %30, i64 %33
store i32 %29, i32* %34
%35 = load float*, float** %5
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = load i64, i64* %6
%39 = add i64 %37, %38
%40 = getelementptr inbounds float, float* %35, i64 %39
%41 = load float, float* %40
%42 = call i32 @f2s16(float %41)
%43 = load i32*, i32** %7
%44 = load i32, i32* %10
%45 = mul nsw i32 2, %44
%46 = add nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %43, i64 %47
store i32 %42, i32* %48
br label %49
49: ; preds = %23
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %18
52: ; preds = %18
%53 = load i8*, i8** %4
%54 = load i32, i32* @O_WRONLY
%55 = load i32, i32* @O_CREAT
%56 = or i32 %54, %55
%57 = call i32 @open(i8* %53, i32 %56, i32 420)
store i32 %57, i32* %11
%58 = load i32, i32* %11
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %52
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %4
%63 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %62)
br label %76
64: ; preds = %52
%65 = load i32, i32* %11
%66 = load i32*, i32** %7
%67 = load i32, i32* %9
%68 = call i32 @write(i32 %65, i32* %66, i32 %67)
%69 = load i32, i32* %9
%70 = icmp ne i32 %68, %69
br i1 %70, label %71, label %75
71: ; preds = %64
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %4
%74 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %73)
br label %76
75: ; preds = %64
store i32 0, i32* %8
br label %76
76: ; preds = %75, %71, %60
%77 = load i32, i32* %11
%78 = call i32 @close(i32 %77)
%79 = load i32*, i32** %7
%80 = call i32 @free(i32* %79)
%81 = load i32, i32* %8
ret i32 %81
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"cannot open file %s\0A\00"
@.str.1 = [21 x i8] c"short write file %s\0A\00"
define i32 @write_raw(i8* %0, float* nocapture %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = shl i32 %4, 2
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i32*
%8 = icmp eq i64 %2, 0
br i1 %8, label %24, label %9
9: ; preds = %3, %9
%10 = phi i64 [ %22, %9 ], [ 0, %3 ]
%11 = getelementptr inbounds float, float* %1, i64 %10
%12 = load float, float* %11
%13 = tail call i32 @f2s16(float %12)
%14 = shl nuw nsw i64 %10, 1
%15 = getelementptr inbounds i32, i32* %7, i64 %14
store i32 %13, i32* %15
%16 = add i64 %10, %2
%17 = getelementptr inbounds float, float* %1, i64 %16
%18 = load float, float* %17
%19 = tail call i32 @f2s16(float %18)
%20 = or i64 %14, 1
%21 = getelementptr inbounds i32, i32* %7, i64 %20
store i32 %19, i32* %21
%22 = add nuw i64 %10, 1
%23 = icmp eq i64 %22, %2
br i1 %23, label %24, label %9
24: ; preds = %9, %3
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %26, %25
%28 = tail call i32 @open(i8* %0, i32 %27, i32 420)
%29 = icmp slt i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %24
%31 = tail call i32 @write(i32 %28, i32* %7, i32 %5)
%32 = icmp eq i32 %31, %5
br i1 %32, label %37, label %33
33: ; preds = %30, %24
%34 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), %24 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %30 ]
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* %34, i8* %0)
br label %37
37: ; preds = %33, %30
%38 = phi i32 [ 0, %30 ], [ -1, %33 ]
%39 = tail call i32 @close(i32 %28)
%40 = tail call i32 @free(i32* %7)
ret i32 %38
}