#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*/ uint8_t;
typedef int /*<<< orphan*/ uint64_t;
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, void *, size_t);
int /*<<< orphan*/ mvhd_to_be16(int /*<<< orphan*/);
int /*<<< orphan*/ mvhd_to_be32(int /*<<< orphan*/);
int /*<<< orphan*/ mvhd_to_be64(int /*<<< orphan*/);
void mvhd_next_struct_to_buffer(void *struct_memb, size_t memb_size,
                                bool req_endian, uint8_t **buffer) {
  uint8_t *buf_ptr = *buffer;
  memcpy(buf_ptr, struct_memb, memb_size);
  if (req_endian) {
    switch (memb_size) {
    case 2:
      *((uint16_t *)buf_ptr) = mvhd_to_be16(*(uint16_t *)(struct_memb));
      break;
    case 4:
      *((uint32_t *)buf_ptr) = mvhd_to_be32(*(uint32_t *)(struct_memb));
      break;
    case 8:
      *((uint64_t *)buf_ptr) = mvhd_to_be64(*(uint64_t *)(struct_memb));
      break;
    }
  }
  buf_ptr += memb_size;
  *buffer = buf_ptr;
}


x86 assembly (clang, -O0):
<mvhd_next_struct_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <memcpy@plt>
cmpl   $0x0,-0x14(%rbp)
je     1202 <mvhd_next_struct_to_buffer+0xc2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
sub    $0x2,%rax
je     11ba <mvhd_next_struct_to_buffer+0x7a>
jmp    1194 <mvhd_next_struct_to_buffer+0x54>
mov    -0x30(%rbp),%rax
sub    $0x4,%rax
je     11d2 <mvhd_next_struct_to_buffer+0x92>
jmp    11a7 <mvhd_next_struct_to_buffer+0x67>
mov    -0x30(%rbp),%rax
sub    $0x8,%rax
je     11ea <mvhd_next_struct_to_buffer+0xaa>
jmp    11fd <mvhd_next_struct_to_buffer+0xbd>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <mvhd_to_be16@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    11fd <mvhd_next_struct_to_buffer+0xbd>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1030 <mvhd_to_be32@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    11fd <mvhd_next_struct_to_buffer+0xbd>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1060 <mvhd_to_be64@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1202 <mvhd_next_struct_to_buffer+0xc2>
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mvhd_next_struct_to_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r12
mov    (%rcx),%r15
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
test   %ebp,%ebp
je     119c <mvhd_next_struct_to_buffer+0x5c>
cmp    $0x8,%rbx
je     1185 <mvhd_next_struct_to_buffer+0x45>
cmp    $0x4,%rbx
je     1190 <mvhd_next_struct_to_buffer+0x50>
cmp    $0x2,%rbx
jne    119c <mvhd_next_struct_to_buffer+0x5c>
mov    (%r12),%edi
call   1040 <mvhd_to_be16@plt>
jmp    1199 <mvhd_next_struct_to_buffer+0x59>
mov    (%r12),%edi
call   1060 <mvhd_to_be64@plt>
jmp    1199 <mvhd_next_struct_to_buffer+0x59>
mov    (%r12),%edi
call   1030 <mvhd_to_be32@plt>
mov    %eax,(%r15)
lea    (%r15,%rbx,4),%rax
mov    %rax,(%r14)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @mvhd_next_struct_to_buffer(i8* %0, i64 %1, i32 %2, i32** %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32**
%9 = alloca i32*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32** %3, i32*** %8
%10 = load i32**, i32*** %8
%11 = load i32*, i32** %10
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = call i32 @memcpy(i32* %12, i8* %13, i64 %14)
%16 = load i32, i32* %7
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %39
18: ; preds = %4
%19 = load i64, i64* %6
switch i64 %19, label %38 [
i64 2, label %20
i64 4, label %26
i64 8, label %32
]
20: ; preds = %18
%21 = load i8*, i8** %5
%22 = bitcast i8* %21 to i32*
%23 = load i32, i32* %22
%24 = call i32 @mvhd_to_be16(i32 %23)
%25 = load i32*, i32** %9
store i32 %24, i32* %25
br label %38
26: ; preds = %18
%27 = load i8*, i8** %5
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
%30 = call i32 @mvhd_to_be32(i32 %29)
%31 = load i32*, i32** %9
store i32 %30, i32* %31
br label %38
32: ; preds = %18
%33 = load i8*, i8** %5
%34 = bitcast i8* %33 to i32*
%35 = load i32, i32* %34
%36 = call i32 @mvhd_to_be64(i32 %35)
%37 = load i32*, i32** %9
store i32 %36, i32* %37
br label %38
38: ; preds = %18, %32, %26, %20
br label %39
39: ; preds = %38, %4
%40 = load i64, i64* %6
%41 = load i32*, i32** %9
%42 = getelementptr inbounds i32, i32* %41, i64 %40
store i32* %42, i32** %9
%43 = load i32*, i32** %9
%44 = load i32**, i32*** %8
store i32* %43, i32** %44
ret void
}

LLVM IR (-O2):
define void @mvhd_next_struct_to_buffer(i8* %0, i64 %1, i32 %2, i32** nocapture %3) {
%5 = load i32*, i32** %3
%6 = tail call i32 @memcpy(i32* %5, i8* %0, i64 %1)
%7 = icmp eq i32 %2, 0
br i1 %7, label %23, label %8
8: ; preds = %4
switch i64 %1, label %23 [
i64 2, label %9
i64 4, label %13
i64 8, label %17
]
9: ; preds = %8
%10 = bitcast i8* %0 to i32*
%11 = load i32, i32* %10
%12 = tail call i32 @mvhd_to_be16(i32 %11)
br label %21
13: ; preds = %8
%14 = bitcast i8* %0 to i32*
%15 = load i32, i32* %14
%16 = tail call i32 @mvhd_to_be32(i32 %15)
br label %21
17: ; preds = %8
%18 = bitcast i8* %0 to i32*
%19 = load i32, i32* %18
%20 = tail call i32 @mvhd_to_be64(i32 %19)
br label %21
21: ; preds = %17, %13, %9
%22 = phi i32 [ %12, %9 ], [ %16, %13 ], [ %20, %17 ]
store i32 %22, i32* %5
br label %23
23: ; preds = %21, %8, %4
%24 = getelementptr inbounds i32, i32* %5, i64 %1
store i32* %24, i32** %3
ret void
}