#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 */
struct pcm {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ oops(struct pcm *, int, char *, unsigned int);
int /*<<< orphan*/ pcm_areas_copy(struct pcm *, unsigned int, char *,
                                  unsigned int, unsigned int);
int /*<<< orphan*/ pcm_mmap_begin(struct pcm *, void **, unsigned int *,
                                  unsigned int *);
int pcm_mmap_commit(struct pcm *, unsigned int, unsigned int);
int pcm_mmap_write_areas(struct pcm *pcm, char *src, unsigned int offset,
                         unsigned int size) {
  void *pcm_areas;
  int commit;
  unsigned int pcm_offset, frames, count = 0;
  while (size > 0) {
    frames = size;
    pcm_mmap_begin(pcm, &pcm_areas, &pcm_offset, &frames);
    pcm_areas_copy(pcm, pcm_offset, src, offset, frames);
    commit = pcm_mmap_commit(pcm, pcm_offset, frames);
    if (commit < 0) {
      oops(pcm, commit, "failed to commit %d frames\n", frames);
      return commit;
    }
    offset += commit;
    count += commit;
    size -= commit;
  }
  return count;
}


x86 assembly (clang, -O0):
<pcm_mmap_write_areas>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x0,-0x20(%rbp)
jbe    11f8 <pcm_mmap_write_areas+0xb8>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rcx
call   1040 <pcm_mmap_begin@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x34(%rbp),%r8d
call   1050 <pcm_areas_copy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1060 <pcm_mmap_commit@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d6 <pcm_mmap_write_areas+0x96>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x34(%rbp),%ecx
lea    0xe3a(%rip),%rdx
call   1030 <oops@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fe <pcm_mmap_write_areas+0xbe>
mov    -0x2c(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
jmp    115d <pcm_mmap_write_areas+0x1d>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pcm_mmap_write_areas>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %ecx,%ecx
je     11b5 <pcm_mmap_write_areas+0x75>
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rsi,%r12
mov    %rdi,%r14
xor    %r13d,%r13d
nop
mov    %ebp,0x8(%rsp)
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1040 <pcm_mmap_begin@plt>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%r8d
mov    %r14,%rdi
mov    %r12,%rdx
mov    %ebx,%ecx
call   1050 <pcm_areas_copy@plt>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %r14,%rdi
call   1060 <pcm_mmap_commit@plt>
mov    %eax,%r15d
test   %eax,%eax
js     11ba <pcm_mmap_write_areas+0x7a>
add    %r15d,%ebx
add    %r15d,%r13d
sub    %r15d,%ebp
jne    1160 <pcm_mmap_write_areas+0x20>
jmp    11d3 <pcm_mmap_write_areas+0x93>
xor    %r13d,%r13d
jmp    11d3 <pcm_mmap_write_areas+0x93>
mov    0x8(%rsp),%ecx
lea    0xe3b(%rip),%rdx
mov    %r14,%rdi
mov    %r15d,%esi
call   1030 <oops@plt>
mov    %r15d,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pcm = type { i32 }
@.str = [28 x i8] c"failed to commit %d frames\0A\00"
define i32 @pcm_mmap_write_areas(%struct.pcm* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.pcm*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store %struct.pcm* %0, %struct.pcm** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %14
br label %15
15: ; preds = %40, %4
%16 = load i32, i32* %9
%17 = icmp ugt i32 %16, 0
br i1 %17, label %18, label %50
18: ; preds = %15
%19 = load i32, i32* %9
store i32 %19, i32* %13
%20 = load %struct.pcm*, %struct.pcm** %6
%21 = call i32 @pcm_mmap_begin(%struct.pcm* %20, i8** %10, i32* %12, i32* %13)
%22 = load %struct.pcm*, %struct.pcm** %6
%23 = load i32, i32* %12
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = load i32, i32* %13
%27 = call i32 @pcm_areas_copy(%struct.pcm* %22, i32 %23, i8* %24, i32 %25, i32 %26)
%28 = load %struct.pcm*, %struct.pcm** %6
%29 = load i32, i32* %12
%30 = load i32, i32* %13
%31 = call i32 @pcm_mmap_commit(%struct.pcm* %28, i32 %29, i32 %30)
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %18
%35 = load %struct.pcm*, %struct.pcm** %6
%36 = load i32, i32* %11
%37 = load i32, i32* %13
%38 = call i32 @oops(%struct.pcm* %35, i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %11
store i32 %39, i32* %5
br label %52
40: ; preds = %18
%41 = load i32, i32* %11
%42 = load i32, i32* %8
%43 = add i32 %42, %41
store i32 %43, i32* %8
%44 = load i32, i32* %11
%45 = load i32, i32* %14
%46 = add i32 %45, %44
store i32 %46, i32* %14
%47 = load i32, i32* %11
%48 = load i32, i32* %9
%49 = sub i32 %48, %47
store i32 %49, i32* %9
br label %15
50: ; preds = %15
%51 = load i32, i32* %14
store i32 %51, i32* %5
br label %52
52: ; preds = %50, %34
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
%struct.pcm = type { i32 }
@.str = [28 x i8] c"failed to commit %d frames\0A\00"
define i32 @pcm_mmap_write_areas(%struct.pcm* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i8** %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = icmp eq i32 %3, 0
br i1 %11, label %32, label %12
12: ; preds = %4, %27
%13 = phi i32 [ %29, %27 ], [ 0, %4 ]
%14 = phi i32 [ %30, %27 ], [ %3, %4 ]
%15 = phi i32 [ %28, %27 ], [ %2, %4 ]
store i32 %14, i32* %7
%16 = call i32 @pcm_mmap_begin(%struct.pcm* %0, i8** %5, i32* %6, i32* %7)
%17 = load i32, i32* %6
%18 = load i32, i32* %7
%19 = call i32 @pcm_areas_copy(%struct.pcm* %0, i32 %17, i8* %1, i32 %15, i32 %18)
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = call i32 @pcm_mmap_commit(%struct.pcm* %0, i32 %20, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %12
%25 = load i32, i32* %7
%26 = call i32 @oops(%struct.pcm* %0, i32 %22, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %32
27: ; preds = %12
%28 = add i32 %22, %15
%29 = add i32 %22, %13
%30 = sub i32 %14, %22
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %12
32: ; preds = %27, %4, %24
%33 = phi i32 [ %22, %24 ], [ 0, %4 ], [ %29, %27 ]
ret i32 %33
}