#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 uint8_t;

/* Variables and functions */
int _datasize(int *, int);
int _footsize(int *, int);
int /*<<< orphan*/ memcpy(int *, int *, int);
void dlmallocfork_save(void *_source, int source_max, void *_target,
                       int target_max) {
  uint8_t *source = (uint8_t *)_source;
  uint8_t *target = (uint8_t *)_target;
  int TOP_FOOT_SIZE = _footsize(source, source_max);
  int i = 0;
  int datalen = _datasize(source, source_max);
  target[0] = (datalen >> 24) & 0xFF;
  target[1] = (datalen >> 16) & 0xFF;
  target[2] = (datalen >> 8) & 0xFF;
  target[3] = (datalen >> 0) & 0xFF;
  memcpy(&target[4], source, target_max - (4 + TOP_FOOT_SIZE));
  memcpy(&target[target_max - TOP_FOOT_SIZE],
         &source[source_max - (TOP_FOOT_SIZE - i)], TOP_FOOT_SIZE);
}


x86 assembly (clang, -O0):
<dlmallocfork_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <_footsize@plt>
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <_datasize@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x3c(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x3c(%rbp),%ecx
sar    $0x0,%ecx
and    $0xff,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x30(%rbp),%rdi
add    $0x10,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x34(%rbp),%eax
add    $0x4,%eax
sub    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%eax
sub    -0x34(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%eax
mov    -0x34(%rbp),%ecx
sub    -0x38(%rbp),%ecx
sub    %ecx,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
call   1050 <memcpy@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dlmallocfork_save>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
call   1040 <_footsize@plt>
mov    %eax,%r12d
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <_datasize@plt>
mov    %eax,%ecx
movzbl %ah,%edx
movzbl %al,%esi
shr    $0x18,%eax
mov    %eax,(%rbx)
shr    $0x10,%ecx
movzbl %cl,%eax
mov    %eax,0x4(%rbx)
mov    %edx,0x8(%rbx)
mov    %esi,0xc(%rbx)
lea    0x10(%rbx),%rdi
sub    %r12d,%r15d
lea    -0x4(%r15),%edx
mov    %r14,%rsi
call   1050 <memcpy@plt>
movslq %r15d,%rax
lea    (%rbx,%rax,4),%rdi
sub    %r12d,%ebp
movslq %ebp,%rax
lea    (%r14,%rax,4),%rsi
mov    %r12d,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <memcpy@plt>

LLVM IR (-O0):
define void @dlmallocfork_save(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%14 = load i8*, i8** %5
%15 = bitcast i8* %14 to i32*
store i32* %15, i32** %9
%16 = load i8*, i8** %7
%17 = bitcast i8* %16 to i32*
store i32* %17, i32** %10
%18 = load i32*, i32** %9
%19 = load i32, i32* %6
%20 = call i32 @_footsize(i32* %18, i32 %19)
store i32 %20, i32* %11
store i32 0, i32* %12
%21 = load i32*, i32** %9
%22 = load i32, i32* %6
%23 = call i32 @_datasize(i32* %21, i32 %22)
store i32 %23, i32* %13
%24 = load i32, i32* %13
%25 = ashr i32 %24, 24
%26 = and i32 %25, 255
%27 = load i32*, i32** %10
%28 = getelementptr inbounds i32, i32* %27, i64 0
store i32 %26, i32* %28
%29 = load i32, i32* %13
%30 = ashr i32 %29, 16
%31 = and i32 %30, 255
%32 = load i32*, i32** %10
%33 = getelementptr inbounds i32, i32* %32, i64 1
store i32 %31, i32* %33
%34 = load i32, i32* %13
%35 = ashr i32 %34, 8
%36 = and i32 %35, 255
%37 = load i32*, i32** %10
%38 = getelementptr inbounds i32, i32* %37, i64 2
store i32 %36, i32* %38
%39 = load i32, i32* %13
%40 = ashr i32 %39, 0
%41 = and i32 %40, 255
%42 = load i32*, i32** %10
%43 = getelementptr inbounds i32, i32* %42, i64 3
store i32 %41, i32* %43
%44 = load i32*, i32** %10
%45 = getelementptr inbounds i32, i32* %44, i64 4
%46 = load i32*, i32** %9
%47 = load i32, i32* %8
%48 = load i32, i32* %11
%49 = add nsw i32 4, %48
%50 = sub nsw i32 %47, %49
%51 = call i32 @memcpy(i32* %45, i32* %46, i32 %50)
%52 = load i32*, i32** %10
%53 = load i32, i32* %8
%54 = load i32, i32* %11
%55 = sub nsw i32 %53, %54
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %52, i64 %56
%58 = load i32*, i32** %9
%59 = load i32, i32* %6
%60 = load i32, i32* %11
%61 = load i32, i32* %12
%62 = sub nsw i32 %60, %61
%63 = sub nsw i32 %59, %62
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %58, i64 %64
%66 = load i32, i32* %11
%67 = call i32 @memcpy(i32* %57, i32* %65, i32 %66)
ret void
}

LLVM IR (-O2):
define void @dlmallocfork_save(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = bitcast i8* %0 to i32*
%6 = bitcast i8* %2 to i32*
%7 = tail call i32 @_footsize(i32* %5, i32 %1)
%8 = tail call i32 @_datasize(i32* %5, i32 %1)
%9 = lshr i32 %8, 24
store i32 %9, i32* %6
%10 = lshr i32 %8, 16
%11 = and i32 %10, 255
%12 = getelementptr inbounds i8, i8* %2, i64 4
%13 = bitcast i8* %12 to i32*
store i32 %11, i32* %13
%14 = lshr i32 %8, 8
%15 = and i32 %14, 255
%16 = getelementptr inbounds i8, i8* %2, i64 8
%17 = bitcast i8* %16 to i32*
store i32 %15, i32* %17
%18 = and i32 %8, 255
%19 = getelementptr inbounds i8, i8* %2, i64 12
%20 = bitcast i8* %19 to i32*
store i32 %18, i32* %20
%21 = getelementptr inbounds i8, i8* %2, i64 16
%22 = bitcast i8* %21 to i32*
%23 = add i32 %3, -4
%24 = sub i32 %23, %7
%25 = tail call i32 @memcpy(i32* %22, i32* %5, i32 %24)
%26 = sub nsw i32 %3, %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %6, i64 %27
%29 = sub nsw i32 %1, %7
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %5, i64 %30
%32 = tail call i32 @memcpy(i32* %28, i32* %31, i32 %7)
ret void
}