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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit_wrapper(int);
int /*<<< orphan*/ fwrite_err(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, unsigned long);
void WriteVarLength(FILE *outf, unsigned long value) {
  unsigned char buffer[4] = {0};
  unsigned char numbytes = 1;
  unsigned char ctr = 0;
  unsigned long temp = value;
  (assert(outf != NULL));
  if (value > 0x0FFFFFFFL) {
    printf("Error: Invalid variable length value write (%0lX) "
           "attempted\nAborting\n",
           value);
    exit_wrapper(1);
  }
  for (ctr = 4; ctr > 0; ctr--) {
    buffer[ctr - 1] |= (char)(temp & 0x7F);
    if ((temp >= 128) && (ctr > 1)) {
      buffer[ctr - 2] = 128;
      numbytes++;
    }
    temp = temp >> 7;
  }
  for (ctr = numbytes; ctr > 0; ctr--) {
    (assert((ctr > 0) && (ctr < 5)));
    fwrite_err(&(buffer[4 - ctr]), 1, 1, outf);
  }
}


x86 assembly (clang, -O0):
<WriteVarLength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
movb   $0x1,-0x15(%rbp)
movb   $0x0,-0x16(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0xfffffff,-0x10(%rbp)
0f
jbe    11ba <WriteVarLength+0x6a>
mov    -0x10(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit_wrapper@plt>
movb   $0x4,-0x16(%rbp)
movzbl -0x16(%rbp),%eax
cmp    $0x0,%eax
jle    1234 <WriteVarLength+0xe4>
mov    -0x20(%rbp),%rax
and    $0x7f,%rax
movsbl %al,%edx
movzbl -0x16(%rbp),%eax
sub    $0x1,%eax
cltq
movzbl -0x14(%rbp,%rax,1),%ecx
or     %edx,%ecx
mov    %cl,-0x14(%rbp,%rax,1)
cmpq   $0x80,-0x20(%rbp)
jb     121b <WriteVarLength+0xcb>
movzbl -0x16(%rbp),%eax
cmp    $0x1,%eax
jle    121b <WriteVarLength+0xcb>
movzbl -0x16(%rbp),%eax
sub    $0x2,%eax
cltq
movb   $0x80,-0x14(%rbp,%rax,1)
mov    -0x15(%rbp),%al
add    $0x1,%al
mov    %al,-0x15(%rbp)
mov    -0x20(%rbp),%rax
shr    $0x7,%rax
mov    %rax,-0x20(%rbp)
mov    -0x16(%rbp),%al
add    $0xff,%al
mov    %al,-0x16(%rbp)
jmp    11be <WriteVarLength+0x6e>
mov    -0x15(%rbp),%al
mov    %al,-0x16(%rbp)
movzbl -0x16(%rbp),%eax
cmp    $0x0,%eax
jle    12a4 <WriteVarLength+0x154>
movzbl -0x16(%rbp),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
jle    1266 <WriteVarLength+0x116>
movzbl -0x16(%rbp),%eax
cmp    $0x5,%eax
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movzbl -0x16(%rbp),%ecx
mov    $0x4,%eax
sub    %ecx,%eax
cltq
lea    -0x14(%rbp),%rdi
add    %rax,%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite_err@plt>
mov    -0x16(%rbp),%al
add    $0xff,%al
mov    %al,-0x16(%rbp)
jmp    123a <WriteVarLength+0xea>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteVarLength>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x0,0x4(%rsp)
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1050 <assert@plt>
cmp    $0x10000000,%r14
jb     119e <WriteVarLength+0x5e>
lea    0xe8c(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <exit_wrapper@plt>
mov    %r14d,%eax
and    $0x7f,%al
mov    %al,0x7(%rsp)
mov    %r14,%rax
shr    $0x7,%rax
or     $0x80,%al
mov    %al,0x6(%rsp)
jmp    11c9 <WriteVarLength+0x89>
mov    %r14d,%eax
and    $0x7f,%al
mov    %al,0x7(%rsp)
mov    %r14,%rax
shr    $0x7,%rax
cmp    $0x7f,%r14
jbe    11f0 <WriteVarLength+0xb0>
or     $0x80,%al
mov    %al,0x6(%rsp)
mov    $0x2,%r12d
cmp    $0x3fff,%r14
jbe    11fa <WriteVarLength+0xba>
mov    %r14,%rax
shr    $0xe,%rax
or     $0x80,%al
mov    %al,0x5(%rsp)
xor    %r12d,%r12d
cmp    $0x200000,%r14
setae  %r12b
mov    %r12d,%eax
shl    $0x7,%al
add    $0x3,%r12
jmp    1207 <WriteVarLength+0xc7>
mov    %al,0x6(%rsp)
mov    $0x1,%r12d
mov    %r14,%rax
shr    $0xe,%rax
mov    %al,0x5(%rsp)
xor    %eax,%eax
shr    $0x15,%r14
and    $0x7f,%r14b
or     %al,%r14b
mov    %r14b,0x4(%rsp)
lea    0x4(%rsp),%rbx
sub    %r12,%rbx
add    $0x4,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edi,%edi
cmp    $0x5,%r12
setb   %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1060 <fwrite_err@plt>
add    $0xffffffffffffffff,%r12
add    $0x1,%rbx
test   %r12b,%r12b
jne    1230 <WriteVarLength+0xf0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [70 x i8] c"Error: Invalid variable length value write (%0lX) attempted\0AAborting\0A\00"
define void @WriteVarLength(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca [4 x i8]
%6 = alloca i8
%7 = alloca i8
%8 = alloca i64
store i32* %0, i32** %3
store i64 %1, i64* %4
%9 = bitcast [4 x i8]* %5 to i8*
store i8 1, i8* %6
store i8 0, i8* %7
%10 = load i64, i64* %4
store i64 %10, i64* %8
%11 = load i32*, i32** %3
%12 = icmp ne i32* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i64, i64* %4
%16 = icmp ugt i64 %15, 268435455
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i64, i64* %4
%19 = call i32 @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0), i64 %18)
%20 = call i32 @exit_wrapper(i32 1)
br label %21
21: ; preds = %17, %2
store i8 4, i8* %7
br label %22
22: ; preds = %57, %21
%23 = load i8, i8* %7
%24 = zext i8 %23 to i32
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %60
26: ; preds = %22
%27 = load i64, i64* %8
%28 = and i64 %27, 127
%29 = trunc i64 %28 to i8
%30 = sext i8 %29 to i32
%31 = load i8, i8* %7
%32 = zext i8 %31 to i32
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = or i32 %37, %30
%39 = trunc i32 %38 to i8
store i8 %39, i8* %35
%40 = load i64, i64* %8
%41 = icmp uge i64 %40, 128
br i1 %41, label %42, label %54
42: ; preds = %26
%43 = load i8, i8* %7
%44 = zext i8 %43 to i32
%45 = icmp sgt i32 %44, 1
br i1 %45, label %46, label %54
46: ; preds = %42
%47 = load i8, i8* %7
%48 = zext i8 %47 to i32
%49 = sub nsw i32 %48, 2
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %50
store i8 -128, i8* %51
%52 = load i8, i8* %6
%53 = add i8 %52, 1
store i8 %53, i8* %6
br label %54
54: ; preds = %46, %42, %26
%55 = load i64, i64* %8
%56 = lshr i64 %55, 7
store i64 %56, i64* %8
br label %57
57: ; preds = %54
%58 = load i8, i8* %7
%59 = add i8 %58, -1
store i8 %59, i8* %7
br label %22
60: ; preds = %22
%61 = load i8, i8* %6
store i8 %61, i8* %7
br label %62
62: ; preds = %85, %60
%63 = load i8, i8* %7
%64 = zext i8 %63 to i32
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %88
66: ; preds = %62
%67 = load i8, i8* %7
%68 = zext i8 %67 to i32
%69 = icmp sgt i32 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load i8, i8* %7
%72 = zext i8 %71 to i32
%73 = icmp slt i32 %72, 5
br label %74
74: ; preds = %70, %66
%75 = phi i1 [ false, %66 ], [ %73, %70 ]
%76 = zext i1 %75 to i32
%77 = call i32 @assert(i32 %76)
%78 = load i8, i8* %7
%79 = zext i8 %78 to i32
%80 = sub nsw i32 4, %79
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 %81
%83 = load i32*, i32** %3
%84 = call i32 @fwrite_err(i8* %82, i32 1, i32 1, i32* %83)
br label %85
85: ; preds = %74
%86 = load i8, i8* %7
%87 = add i8 %86, -1
store i8 %87, i8* %7
br label %62
88: ; preds = %62
ret void
}

LLVM IR (-O2):
@.str = [70 x i8] c"Error: Invalid variable length value write (%0lX) attempted\0AAborting\0A\00"
define void @WriteVarLength(i32* %0, i64 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to [4 x i8]*
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = icmp ne i32* %0, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = icmp ugt i64 %1, 268435455
br i1 %9, label %10, label %20
10: ; preds = %2
%11 = tail call i32 @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0), i64 %1)
%12 = tail call i32 @exit_wrapper(i32 1)
%13 = trunc i64 %1 to i8
%14 = and i8 %13, 127
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
store i8 %14, i8* %15
%16 = lshr i64 %1, 7
%17 = trunc i64 %16 to i8
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%19 = or i8 %17, -128
store i8 %19, i8* %18
br label %46
20: ; preds = %2
%21 = trunc i64 %1 to i8
%22 = and i8 %21, 127
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
store i8 %22, i8* %23
%24 = icmp ugt i64 %1, 127
%25 = lshr i64 %1, 7
%26 = trunc i64 %25 to i8
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
br i1 %24, label %38, label %28
28: ; preds = %20
store i8 %26, i8* %27
br label %41
29: ; preds = %46, %41
%30 = phi i8 [ 0, %41 ], [ %52, %46 ]
%31 = phi i64 [ %42, %41 ], [ %53, %46 ]
%32 = lshr i64 %1, 21
%33 = trunc i64 %32 to i8
%34 = and i8 %33, 127
%35 = bitcast i32* %3 to i8*
%36 = or i8 %30, %34
store i8 %36, i8* %35
%37 = and i64 %31, 255
br label %54
38: ; preds = %20
%39 = or i8 %26, -128
store i8 %39, i8* %27
%40 = icmp ugt i64 %1, 16383
br i1 %40, label %46, label %41
41: ; preds = %28, %38
%42 = phi i64 [ 1, %28 ], [ 2, %38 ]
%43 = lshr i64 %1, 14
%44 = trunc i64 %43 to i8
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
store i8 %44, i8* %45
br label %29
46: ; preds = %38, %10
%47 = lshr i64 %1, 14
%48 = trunc i64 %47 to i8
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%50 = or i8 %48, -128
store i8 %50, i8* %49
%51 = icmp ugt i64 %1, 2097151
%52 = select i1 %51, i8 -128, i8 0
%53 = select i1 %51, i64 4, i64 3
br label %29
54: ; preds = %29, %54
%55 = phi i64 [ %37, %29 ], [ %62, %54 ]
%56 = icmp ult i64 %55, 5
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
%59 = sub nsw i64 4, %55
%60 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 %59
%61 = call i32 @fwrite_err(i8* %60, i32 1, i32 1, i32* %0)
%62 = add nsw i64 %55, -1
%63 = trunc i64 %62 to i8
%64 = icmp eq i8 %63, 0
br i1 %64, label %65, label %54
65: ; preds = %54
ret void
}