#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*/ ylen;
typedef size_t uint32_t;
typedef size_t int32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fwrite(size_t *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, size_t);
void write_samples(const int *y, size_t ylen, const char *filepath) {
  FILE *f = fopen(filepath, "w");
  uint32_t ylen32 = ylen;
  size_t nwrite = fwrite(&ylen, sizeof(ylen), 1, f);
  assert(nwrite == 1);
  printf("num output samples: %zu\n", ylen);
  for (size_t i = 0; i < ylen; ++i) {
    int32_t i32 = *y++;
    nwrite = fwrite(&i32, sizeof(int32_t), 1, f);
    assert(nwrite == 1);
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<write_samples>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x10(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1227 <write_samples+0xd7>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x8(%rbp)
movslq (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x40(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11c5 <write_samples+0x75>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_samples>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    %rsi,(%rsp)
lea    0xe95(%rip),%rsi
mov    %rdx,%rdi
call   1060 <fopen@plt>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
xor    %edi,%edi
cmp    $0x1,%rax
sete   %dil
call   1050 <assert@plt>
mov    (%rsp),%r13
lea    0xe5d(%rip),%rdi
mov    %r13,%rsi
call   1040 <printf@plt>
test   %r13,%r13
je     11f6 <write_samples+0xa6>
xor    %ebx,%ebx
lea    0x8(%rsp),%r12
nopl   0x0(%rax)
movslq (%r14,%rbx,4),%rax
mov    %rax,0x8(%rsp)
mov    %r12,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
xor    %edi,%edi
cmp    $0x1,%rax
sete   %dil
call   1050 <assert@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11c0 <write_samples+0x70>
mov    %r15,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [25 x i8] c"num output samples: %zu\0A\00"
define void @write_samples(i32* %0, i64 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
store i32* %0, i32** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i64, i64* %5
store i64 %14, i64* %8
%15 = load i32*, i32** %7
%16 = call i64 @fwrite(i64* %5, i32 8, i32 1, i32* %15)
store i64 %16, i64* %9
%17 = load i64, i64* %9
%18 = icmp eq i64 %17, 1
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i64, i64* %5
%22 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %21)
store i64 0, i64* %10
br label %23
23: ; preds = %38, %3
%24 = load i64, i64* %10
%25 = load i64, i64* %5
%26 = icmp ult i64 %24, %25
br i1 %26, label %27, label %41
27: ; preds = %23
%28 = load i32*, i32** %4
%29 = getelementptr inbounds i32, i32* %28, i32 1
store i32* %29, i32** %4
%30 = load i32, i32* %28
%31 = sext i32 %30 to i64
store i64 %31, i64* %11
%32 = load i32*, i32** %7
%33 = call i64 @fwrite(i64* %11, i32 8, i32 1, i32* %32)
store i64 %33, i64* %9
%34 = load i64, i64* %9
%35 = icmp eq i64 %34, 1
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
br label %38
38: ; preds = %27
%39 = load i64, i64* %10
%40 = add i64 %39, 1
store i64 %40, i64* %10
br label %23
41: ; preds = %23
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [25 x i8] c"num output samples: %zu\0A\00"
define void @write_samples(i32* nocapture %0, i64 %1, i8* nocapture %2) {
%4 = alloca i64
%5 = alloca i64
store i64 %1, i64* %4
%6 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = call i64 @fwrite(i64* %4, i32 8, i32 1, i32* %6)
%8 = icmp eq i64 %7, 1
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = load i64, i64* %4
%12 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i64 %11)
%13 = icmp eq i64 %11, 0
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = bitcast i64* %5 to i8*
br label %18
16: ; preds = %18, %3
%17 = tail call i32 @fclose(i32* %6)
ret void
18: ; preds = %14, %18
%19 = phi i64 [ 0, %14 ], [ %28, %18 ]
%20 = phi i32* [ %0, %14 ], [ %21, %18 ]
%21 = getelementptr inbounds i32, i32* %20, i64 1
%22 = load i32, i32* %20
%23 = sext i32 %22 to i64
store i64 %23, i64* %5
%24 = call i64 @fwrite(i64* %5, i32 8, i32 1, i32* %6)
%25 = icmp eq i64 %24, 1
%26 = zext i1 %25 to i32
%27 = tail call i32 @assert(i32 %26)
%28 = add nuw i64 %19, 1
%29 = icmp eq i64 %28, %11
br i1 %29, label %16, label %18
}