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

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ assert(int);
size_t bytes_per_sample(char);
int /*<<< orphan*/ close(int);
char *enter_dirfile(char const *);
int /*<<< orphan*/ exit_dirfile(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *, char,
                           size_t);
int open(char const *, int, int);
scalar_t__ write(int, void const *, size_t);
int dirfile_write_raw_channel(const char *dirfilename, const char *channelname,
                              size_t samples_per_frame, size_t nframes,
                              char typechar, const void *data) {
  int status = -1;
  assert(data != NULL);
  size_t nbytes = bytes_per_sample(typechar) * samples_per_frame * nframes;
  assert(nbytes > 0);
  char *oldwd = enter_dirfile(dirfilename);
  if (oldwd == NULL)
    goto error;
  int fd = open(channelname, O_WRONLY | O_CREAT | O_EXCL,
                S_IRUSR | S_IRGRP | S_IROTH);
  ssize_t n = write(fd, data, nbytes);
  close(fd);
  if (n < 0)
    goto error;
  FILE *fp = fopen("format", "a");
  fprintf(fp, "%s RAW %c %d\n", channelname, typechar, samples_per_frame);
  fclose(fp);
  status = 0;
error:
  exit_dirfile(oldwd);
  return status;
}


x86 assembly (clang, -O0):
<dirfile_write_raw_channel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %r8b,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %al,-0x21(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movsbl -0x21(%rbp),%edi
call   1030 <bytes_per_sample@plt>
imul   -0x18(%rbp),%rax
imul   -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%rdi
call   10c0 <enter_dirfile@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1221 <dirfile_write_raw_channel+0x81>
jmp    12d4 <dirfile_write_raw_channel+0x134>
mov    -0x10(%rbp),%rdi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
or     (%rax),%esi
mov    0x2d9a(%rip),%rax
or     (%rax),%esi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
mov    0x2d80(%rip),%rax
or     (%rax),%edx
mov    0x2d6f(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x58(%rbp)
mov    -0x4c(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x0,-0x58(%rbp)
jge    128f <dirfile_write_raw_channel+0xef>
jmp    12d4 <dirfile_write_raw_channel+0x134>
lea    0xd6a(%rip),%rdi
lea    0xd6a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x21(%rbp),%al
mov    -0x18(%rbp),%r8
lea    0xd4d(%rip),%rsi
movsbl %al,%ecx
call   1080 <fprintf@plt>
mov    -0x60(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x48(%rbp),%rdi
call   1090 <exit_dirfile@plt>
mov    -0x34(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirfile_write_raw_channel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r13
mov    %r8d,%ebx
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %rsi,0x10(%rsp)
mov    %rdi,%r14
xor    %edi,%edi
test   %r9,%r9
setne  %dil
call   1070 <assert@plt>
mov    %ebx,0x4(%rsp)
mov    %ebx,%edi
call   1030 <bytes_per_sample@plt>
imul   %r15,%rbp
imul   %rax,%rbp
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1070 <assert@plt>
mov    %r14,%rdi
call   10c0 <enter_dirfile@plt>
mov    %rax,%r12
mov    $0xffffffff,%r14d
test   %rax,%rax
je     12b0 <dirfile_write_raw_channel+0x110>
mov    %r15,0x8(%rsp)
mov    0x2d99(%rip),%rax
mov    0x2dda(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2daf(%rip),%rax
or     (%rax),%esi
mov    0x2d8e(%rip),%rax
mov    0x2d97(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d84(%rip),%rax
or     (%rax),%edx
mov    0x10(%rsp),%rdi
mov    %rdi,%rbx
call   10a0 <open@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1040 <write@plt>
mov    %rax,%rbp
mov    %r15d,%edi
call   1060 <close@plt>
test   %rbp,%rbp
js     12b0 <dirfile_write_raw_channel+0x110>
lea    0xd86(%rip),%rdi
lea    0xd86(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbp
movsbl 0x4(%rsp),%ecx
lea    0xd74(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    0x8(%rsp),%r8
call   1080 <fprintf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
mov    %r12,%rdi
call   1090 <exit_dirfile@plt>
mov    %r14d,%eax
add    $0x18,%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
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [7 x i8] c"format\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [14 x i8] c"%s RAW %c %d\0A\00"
define i32 @dirfile_write_raw_channel(i8* %0, i8* %1, i64 %2, i64 %3, i8 signext %4, i8* %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i64
%18 = alloca i32*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i8 %4, i8* %11
store i8* %5, i8** %12
store i32 -1, i32* %13
%19 = load i8*, i8** %12
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i8, i8* %11
%24 = call i64 @bytes_per_sample(i8 signext %23)
%25 = load i64, i64* %9
%26 = mul i64 %24, %25
%27 = load i64, i64* %10
%28 = mul i64 %26, %27
store i64 %28, i64* %14
%29 = load i64, i64* %14
%30 = icmp ugt i64 %29, 0
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i8*, i8** %7
%34 = call i8* @enter_dirfile(i8* %33)
store i8* %34, i8** %15
%35 = load i8*, i8** %15
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %38
37: ; preds = %6
br label %69
38: ; preds = %6
%39 = load i8*, i8** %8
%40 = load i32, i32* @O_WRONLY
%41 = load i32, i32* @O_CREAT
%42 = or i32 %40, %41
%43 = load i32, i32* @O_EXCL
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IRUSR
%46 = load i32, i32* @S_IRGRP
%47 = or i32 %45, %46
%48 = load i32, i32* @S_IROTH
%49 = or i32 %47, %48
%50 = call i32 @open(i8* %39, i32 %44, i32 %49)
store i32 %50, i32* %16
%51 = load i32, i32* %16
%52 = load i8*, i8** %12
%53 = load i64, i64* %14
%54 = call i64 @write(i32 %51, i8* %52, i64 %53)
store i64 %54, i64* %17
%55 = load i32, i32* %16
%56 = call i32 @close(i32 %55)
%57 = load i64, i64* %17
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %38
br label %69
60: ; preds = %38
%61 = call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %61, i32** %18
%62 = load i32*, i32** %18
%63 = load i8*, i8** %8
%64 = load i8, i8* %11
%65 = load i64, i64* %9
%66 = call i32 @fprintf(i32* %62, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %63, i8 signext %64, i64 %65)
%67 = load i32*, i32** %18
%68 = call i32 @fclose(i32* %67)
store i32 0, i32* %13
br label %69
69: ; preds = %60, %59, %37
%70 = load i8*, i8** %15
%71 = call i32 @exit_dirfile(i8* %70)
%72 = load i32, i32* %13
ret i32 %72
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [7 x i8] c"format\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [14 x i8] c"%s RAW %c %d\0A\00"
define i32 @dirfile_write_raw_channel(i8* %0, i8* %1, i64 %2, i64 %3, i8 signext %4, i8* %5) {
%7 = icmp ne i8* %5, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i64 @bytes_per_sample(i8 signext %4)
%11 = mul i64 %3, %2
%12 = mul i64 %11, %10
%13 = icmp ne i64 %12, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i8* @enter_dirfile(i8* %0)
%17 = icmp eq i8* %16, null
br i1 %17, label %37, label %18
18: ; preds = %6
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_EXCL
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %25, %24
%27 = load i32, i32* @S_IROTH
%28 = or i32 %26, %27
%29 = tail call i32 @open(i8* %1, i32 %23, i32 %28)
%30 = tail call i64 @write(i32 %29, i8* %5, i64 %12)
%31 = tail call i32 @close(i32 %29)
%32 = icmp slt i64 %30, 0
br i1 %32, label %37, label %33
33: ; preds = %18
%34 = tail call i32* @fopen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%35 = tail call i32 @fprintf(i32* %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8 signext %4, i64 %2)
%36 = tail call i32 @fclose(i32* %34)
br label %37
37: ; preds = %18, %6, %33
%38 = phi i32 [ -1, %6 ], [ -1, %18 ], [ 0, %33 ]
%39 = tail call i32 @exit_dirfile(i8* %16)
ret i32 %38
}