#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 */

/* Variables and functions */
int VIM_MARB_GetJbufEndAddr();
unsigned long VIM_MARB_WriteBuf(unsigned char *, int, int /*<<< orphan*/,
                                unsigned long, unsigned long);
unsigned long VIM_MARB_WriteJpegData(unsigned char *Start, unsigned long length,
                                     unsigned long pos, unsigned char end) {
  unsigned long dwEndAddr = VIM_MARB_GetJbufEndAddr() + 4;
  unsigned long writepos;
  unsigned char dat[4] = {0xd9, 0xff, 0xff, 0xd9};
  if (!end) {
    writepos = VIM_MARB_WriteBuf(Start, length, 0, dwEndAddr, pos);
    return writepos;
  } else {
    if (length & 0x1) {
      dat[0] = Start[length - 1];
      length = length - 1;
    }
    writepos = VIM_MARB_WriteBuf(Start, length, 0, dwEndAddr, pos);
    { writepos = VIM_MARB_WriteBuf(dat, 4, 0, dwEndAddr, writepos); }
    return writepos;
  }
}


x86 assembly (clang, -O0):
<VIM_MARB_WriteJpegData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %cl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %al,-0x21(%rbp)
mov    $0x0,%al
call   1030 <VIM_MARB_GetJbufEndAddr@plt>
add    $0x4,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    0xeb1(%rip),%eax
mov    %eax,-0x3c(%rbp)
cmpb   $0x0,-0x21(%rbp)
jne    1186 <VIM_MARB_WriteJpegData+0x66>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%r8
xor    %edx,%edx
call   1040 <VIM_MARB_WriteBuf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f7 <VIM_MARB_WriteJpegData+0xd7>
mov    -0x18(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     11b6 <VIM_MARB_WriteJpegData+0x96>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%r8
xor    %edx,%edx
call   1040 <VIM_MARB_WriteBuf@plt>
mov    %rax,-0x38(%rbp)
lea    -0x3c(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    $0x4,%esi
xor    %edx,%edx
call   1040 <VIM_MARB_WriteBuf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<VIM_MARB_WriteJpegData>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %eax,%eax
call   1030 <VIM_MARB_GetJbufEndAddr@plt>
add    $0x4,%eax
movslq %eax,%r15
movl   $0xd9ffffd9,0xc(%rsp)
d9
test   %ebp,%ebp
je     1198 <VIM_MARB_WriteJpegData+0x78>
test   $0x1,%bl
je     1162 <VIM_MARB_WriteJpegData+0x42>
mov    -0x1(%r12,%rbx,1),%al
add    $0xffffffffffffffff,%rbx
mov    %al,0xc(%rsp)
mov    %r12,%rdi
mov    %ebx,%esi
xor    %edx,%edx
mov    %r15,%rcx
mov    %r14,%r8
call   1040 <VIM_MARB_WriteBuf@plt>
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
xor    %edx,%edx
mov    %r15,%rcx
mov    %rax,%r8
call   1040 <VIM_MARB_WriteBuf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
mov    %ebx,%esi
xor    %edx,%edx
mov    %r15,%rcx
mov    %r14,%r8
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <VIM_MARB_WriteBuf@plt>

LLVM IR (-O0):
@__const.VIM_MARB_WriteJpegData.dat = [4 x i8] c"\D9\FF\FF\D9"
define i64 @VIM_MARB_WriteJpegData(i8* %0, i64 %1, i64 %2, i8 zeroext %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8
%10 = alloca i64
%11 = alloca i64
%12 = alloca [4 x i8]
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8 %3, i8* %9
%13 = call i32 (...) @VIM_MARB_GetJbufEndAddr()
%14 = add nsw i32 %13, 4
%15 = sext i32 %14 to i64
store i64 %15, i64* %10
%16 = bitcast [4 x i8]* %12 to i8*
%17 = load i8, i8* %9
%18 = icmp ne i8 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = trunc i64 %21 to i32
%23 = load i64, i64* %10
%24 = load i64, i64* %8
%25 = call i64 @VIM_MARB_WriteBuf(i8* %20, i32 %22, i32 0, i64 %23, i64 %24)
store i64 %25, i64* %11
%26 = load i64, i64* %11
store i64 %26, i64* %5
br label %52
27: ; preds = %4
%28 = load i64, i64* %7
%29 = and i64 %28, 1
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %27
%32 = load i8*, i8** %6
%33 = load i64, i64* %7
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
store i8 %36, i8* %37
%38 = load i64, i64* %7
%39 = sub i64 %38, 1
store i64 %39, i64* %7
br label %40
40: ; preds = %31, %27
%41 = load i8*, i8** %6
%42 = load i64, i64* %7
%43 = trunc i64 %42 to i32
%44 = load i64, i64* %10
%45 = load i64, i64* %8
%46 = call i64 @VIM_MARB_WriteBuf(i8* %41, i32 %43, i32 0, i64 %44, i64 %45)
store i64 %46, i64* %11
%47 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%48 = load i64, i64* %10
%49 = load i64, i64* %11
%50 = call i64 @VIM_MARB_WriteBuf(i8* %47, i32 4, i32 0, i64 %48, i64 %49)
store i64 %50, i64* %11
%51 = load i64, i64* %11
store i64 %51, i64* %5
br label %52
52: ; preds = %40, %19
%53 = load i64, i64* %5
ret i64 %53
}

LLVM IR (-O2):
define i64 @VIM_MARB_WriteJpegData(i8* %0, i64 %1, i64 %2, i8 zeroext %3) {
%5 = alloca i32
%6 = tail call i32 (...) @VIM_MARB_GetJbufEndAddr()
%7 = add nsw i32 %6, 4
%8 = sext i32 %7 to i64
%9 = bitcast i32* %5 to i8*
store i32 -637534247, i32* %5
%10 = icmp eq i8 %3, 0
br i1 %10, label %11, label %14
11: ; preds = %4
%12 = trunc i64 %1 to i32
%13 = tail call i64 @VIM_MARB_WriteBuf(i8* %0, i32 %12, i32 0, i64 %8, i64 %2)
br label %26
14: ; preds = %4
%15 = and i64 %1, 1
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = add i64 %1, -1
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
store i8 %20, i8* %9
br label %21
21: ; preds = %17, %14
%22 = phi i64 [ %18, %17 ], [ %1, %14 ]
%23 = trunc i64 %22 to i32
%24 = tail call i64 @VIM_MARB_WriteBuf(i8* %0, i32 %23, i32 0, i64 %8, i64 %2)
%25 = call i64 @VIM_MARB_WriteBuf(i8* %9, i32 4, i32 0, i64 %8, i64 %24)
br label %26
26: ; preds = %21, %11
%27 = phi i64 [ %25, %21 ], [ %13, %11 ]
ret i64 %27
}