#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 */
scalar_t__ LZSSBufferWrite(int, unsigned char *, int *, unsigned char);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, unsigned char *, int);
int LZSSExtract(unsigned char *Input, int InputSize, int fd) {
  unsigned char Buffer[0x1000];
  unsigned short BitBuffer = 0;
  int i = 0, k, BitCount = 8, BufferCount = 0;
  while (i < InputSize) {
    if (BitCount == 8) {
      BitBuffer = Input[i];
      BitCount = -1;
    } else if ((BitBuffer >> BitCount) & 0x01) {
      if (LZSSBufferWrite(fd, Buffer, &BufferCount, Input[i]))
        return 1;
    } else if ((i + 1) < InputSize) {
      int offset = ((Input[i] | ((Input[i + 1] & 0xF0) << 4)) - 0xFEE) & 0xFFF;
      int length = (Input[i + 1] & 0x0F) + 3;
      for (k = 0; k < length; k++) {
        if (LZSSBufferWrite(fd, Buffer, &BufferCount,
                            Buffer[(offset + k) & 0xFFF]))
          return 1;
      }
      i++;
    } else {
      fprintf(stderr, "Error: requesting data beyond end of input file.\n");
      return 1;
    }
    i++;
    BitCount++;
  }
  if (BufferCount) {
    if (write(fd, Buffer, BufferCount) != BufferCount) {
      fprintf(stderr, "Error writing to output file: %s", strerror(errno));
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<LZSSExtract>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movw   $0x0,-0x1022(%rbp)
movl   $0x0,-0x1028(%rbp)
movl   $0x8,-0x1030(%rbp)
movl   $0x0,-0x1034(%rbp)
mov    -0x1028(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1368 <LZSSExtract+0x228>
cmpl   $0x8,-0x1030(%rbp)
jne    11bd <LZSSExtract+0x7d>
mov    -0x10(%rbp),%rax
movslq -0x1028(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %ax,-0x1022(%rbp)
movl   $0xffffffff,-0x1030(%rbp)
ff ff ff
jmp    1345 <LZSSExtract+0x205>
movzwl -0x1022(%rbp),%eax
mov    -0x1030(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1218 <LZSSExtract+0xd8>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq -0x1028(%rbp),%rcx
lea    -0x1034(%rbp),%rdx
movzbl (%rax,%rcx,1),%ecx
call   1040 <LZSSBufferWrite@plt>
cmp    $0x0,%rax
je     1213 <LZSSExtract+0xd3>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <LZSSExtract+0x2a2>
jmp    1340 <LZSSExtract+0x200>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jge    1318 <LZSSExtract+0x1d8>
mov    -0x10(%rbp),%rax
movslq -0x1028(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
mov    -0x1028(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
movzbl (%rcx,%rdx,1),%ecx
and    $0xf0,%ecx
shl    $0x4,%ecx
or     %ecx,%eax
sub    $0xfee,%eax
and    $0xfff,%eax
mov    %eax,-0x1038(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1028(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
and    $0xf,%eax
add    $0x3,%eax
mov    %eax,-0x103c(%rbp)
movl   $0x0,-0x102c(%rbp)
mov    -0x102c(%rbp),%eax
cmp    -0x103c(%rbp),%eax
jge    1304 <LZSSExtract+0x1c4>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1038(%rbp),%eax
add    -0x102c(%rbp),%eax
and    $0xfff,%eax
cltq
lea    -0x1034(%rbp),%rdx
movzbl -0x1020(%rbp,%rax,1),%ecx
ff
call   1040 <LZSSBufferWrite@plt>
cmp    $0x0,%rax
je     12eb <LZSSExtract+0x1ab>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <LZSSExtract+0x2a2>
jmp    12f0 <LZSSExtract+0x1b0>
mov    -0x102c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x102c(%rbp)
jmp    1292 <LZSSExtract+0x152>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
jmp    133b <LZSSExtract+0x1fb>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
lea    0xcd8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <LZSSExtract+0x2a2>
jmp    1340 <LZSSExtract+0x200>
jmp    1345 <LZSSExtract+0x205>
mov    -0x1028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1028(%rbp)
mov    -0x1030(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1030(%rbp)
jmp    117c <LZSSExtract+0x3c>
cmpl   $0x0,-0x1034(%rbp)
je     13db <LZSSExtract+0x29b>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1034(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1034(%rbp),%eax
je     13d6 <LZSSExtract+0x296>
mov    0x2c43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1040(%rbp)
mov    0x2c2c(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1040(%rbp),%edi
mov    %rax,%rdx
lea    0xc6f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13e2 <LZSSExtract+0x2a2>
jmp    13db <LZSSExtract+0x29b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LZSSExtract>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
movl   $0x0,(%rsp)
test   %esi,%esi
jle    124a <LZSSExtract+0x10a>
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r15
xor    %r12d,%r12d
mov    $0x8,%r13d
xor    %r14d,%r14d
mov    %rdi,0x8(%rsp)
jmp    119f <LZSSExtract+0x5f>
nopw   0x0(%rax,%rax,1)
movslq %r14d,%rax
movzbl (%r15,%rax,1),%r12d
mov    $0xffffffff,%r13d
add    $0x1,%r14d
add    $0x1,%r13d
cmp    %ebp,%r14d
jge    124e <LZSSExtract+0x10e>
cmp    $0x8,%r13d
je     1180 <LZSSExtract+0x40>
movzwl %r12w,%eax
bt     %r13d,%eax
movslq %r14d,%rax
jae    11d0 <LZSSExtract+0x90>
movzbl (%r15,%rax,1),%ecx
mov    %ebx,%edi
lea    0x10(%rsp),%rsi
mov    %rsp,%rdx
call   1040 <LZSSBufferWrite@plt>
test   %rax,%rax
je     118e <LZSSExtract+0x4e>
jmp    12ab <LZSSExtract+0x16b>
lea    0x1(%rax),%r14
cmp    %ebp,%r14d
jge    1294 <LZSSExtract+0x154>
mov    %r12d,0x4(%rsp)
movzbl (%r15,%rax,1),%eax
movzbl (%r15,%r14,1),%r15d
mov    %r15d,%ecx
shl    $0x4,%ecx
and    $0xf00,%ecx
and    $0xf,%r15d
lea    (%rax,%rcx,1),%r12d
add    $0x12,%r12d
add    $0x3,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r12d,%eax
and    $0xfff,%eax
movzbl 0x10(%rsp,%rax,1),%ecx
mov    %ebx,%edi
lea    0x10(%rsp),%rsi
mov    %rsp,%rdx
call   1040 <LZSSBufferWrite@plt>
test   %rax,%rax
jne    12ab <LZSSExtract+0x16b>
add    $0x1,%r12d
add    $0xffffffff,%r15d
jne    1210 <LZSSExtract+0xd0>
mov    0x8(%rsp),%r15
mov    0x4(%rsp),%r12d
jmp    118e <LZSSExtract+0x4e>
xor    %ebp,%ebp
jmp    12b0 <LZSSExtract+0x170>
mov    (%rsp),%edx
xor    %ebp,%ebp
test   %edx,%edx
je     12b0 <LZSSExtract+0x170>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1030 <write@plt>
cmp    (%rsp),%eax
je     12b0 <LZSSExtract+0x170>
mov    0x2d71(%rip),%rax
mov    (%rax),%ebx
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xdac(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12ab <LZSSExtract+0x16b>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [50 x i8] c"Error: requesting data beyond end of input file.\0A\00"
@.str.1 = [33 x i8] c"Error writing to output file: %s\00"
@errno = global i32 0
define i32 @LZSSExtract(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i16
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i16 0, i16* %9
store i32 0, i32* %10
store i32 8, i32* %12
store i32 0, i32* %13
br label %16
16: ; preds = %111, %3
%17 = load i32, i32* %10
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %116
20: ; preds = %16
%21 = load i32, i32* %12
%22 = icmp eq i32 %21, 8
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i8*, i8** %5
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i16
store i16 %29, i16* %9
store i32 -1, i32* %12
br label %111
30: ; preds = %20
%31 = load i16, i16* %9
%32 = zext i16 %31 to i32
%33 = load i32, i32* %12
%34 = ashr i32 %32, %33
%35 = and i32 %34, 1
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %30
%38 = load i32, i32* %7
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%40 = load i8*, i8** %5
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = call i64 @LZSSBufferWrite(i32 %38, i8* %39, i32* %13, i8 zeroext %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %37
store i32 1, i32* %4
br label %133
48: ; preds = %37
br label %110
49: ; preds = %30
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
%52 = load i32, i32* %6
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %106
54: ; preds = %49
%55 = load i8*, i8** %5
%56 = load i32, i32* %10
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = load i8*, i8** %5
%62 = load i32, i32* %10
%63 = add nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %61, i64 %64
%66 = load i8, i8* %65
%67 = zext i8 %66 to i32
%68 = and i32 %67, 240
%69 = shl i32 %68, 4
%70 = or i32 %60, %69
%71 = sub nsw i32 %70, 4078
%72 = and i32 %71, 4095
store i32 %72, i32* %14
%73 = load i8*, i8** %5
%74 = load i32, i32* %10
%75 = add nsw i32 %74, 1
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %73, i64 %76
%78 = load i8, i8* %77
%79 = zext i8 %78 to i32
%80 = and i32 %79, 15
%81 = add nsw i32 %80, 3
store i32 %81, i32* %15
store i32 0, i32* %11
br label %82
82: ; preds = %100, %54
%83 = load i32, i32* %11
%84 = load i32, i32* %15
%85 = icmp slt i32 %83, %84
br i1 %85, label %86, label %103
86: ; preds = %82
%87 = load i32, i32* %7
%88 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%89 = load i32, i32* %14
%90 = load i32, i32* %11
%91 = add nsw i32 %89, %90
%92 = and i32 %91, 4095
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %93
%95 = load i8, i8* %94
%96 = call i64 @LZSSBufferWrite(i32 %87, i8* %88, i32* %13, i8 zeroext %95)
%97 = icmp ne i64 %96, 0
br i1 %97, label %98, label %99
98: ; preds = %86
store i32 1, i32* %4
br label %133
99: ; preds = %86
br label %100
100: ; preds = %99
%101 = load i32, i32* %11
%102 = add nsw i32 %101, 1
store i32 %102, i32* %11
br label %82
103: ; preds = %82
%104 = load i32, i32* %10
%105 = add nsw i32 %104, 1
store i32 %105, i32* %10
br label %109
106: ; preds = %49
%107 = load i32, i32* @stderr
%108 = call i32 (i32, i8*, ...) @fprintf(i32 %107, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %133
109: ; preds = %103
br label %110
110: ; preds = %109, %48
br label %111
111: ; preds = %110, %23
%112 = load i32, i32* %10
%113 = add nsw i32 %112, 1
store i32 %113, i32* %10
%114 = load i32, i32* %12
%115 = add nsw i32 %114, 1
store i32 %115, i32* %12
br label %16
116: ; preds = %16
%117 = load i32, i32* %13
%118 = icmp ne i32 %117, 0
br i1 %118, label %119, label %132
119: ; preds = %116
%120 = load i32, i32* %7
%121 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%122 = load i32, i32* %13
%123 = call i32 @write(i32 %120, i8* %121, i32 %122)
%124 = load i32, i32* %13
%125 = icmp ne i32 %123, %124
br i1 %125, label %126, label %131
126: ; preds = %119
%127 = load i32, i32* @stderr
%128 = load i32, i32* @errno
%129 = call i8* @strerror(i32 %128)
%130 = call i32 (i32, i8*, ...) @fprintf(i32 %127, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %129)
store i32 1, i32* %4
br label %133
131: ; preds = %119
br label %132
132: ; preds = %131, %116
store i32 0, i32* %4
br label %133
133: ; preds = %132, %126, %106, %98, %47
%134 = load i32, i32* %4
ret i32 %134
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"Error: requesting data beyond end of input file.\0A\00"
@.str.1 = [33 x i8] c"Error writing to output file: %s\00"
@errno = global i32 0
define i32 @LZSSExtract(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %82
9: ; preds = %3, %63
%10 = phi i16 [ %66, %63 ], [ 0, %3 ]
%11 = phi i32 [ %68, %63 ], [ 8, %3 ]
%12 = phi i32 [ %67, %63 ], [ 0, %3 ]
%13 = icmp eq i32 %11, 8
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = sext i32 %12 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = zext i8 %17 to i16
br label %63
19: ; preds = %9
%20 = zext i16 %10 to i32
%21 = shl nuw i32 1, %11
%22 = and i32 %21, %20
%23 = icmp eq i32 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = sext i32 %12 to i64
%26 = getelementptr inbounds i8, i8* %0, i64 %25
%27 = load i8, i8* %26
%28 = call i64 @LZSSBufferWrite(i32 %2, i8* %6, i32* %5, i8 zeroext %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %63, label %82
30: ; preds = %19
%31 = add nsw i32 %12, 1
%32 = icmp slt i32 %31, %1
br i1 %32, label %33, label %60
33: ; preds = %30
%34 = sext i32 %12 to i64
%35 = getelementptr inbounds i8, i8* %0, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = sext i32 %31 to i64
%39 = getelementptr inbounds i8, i8* %0, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = shl nuw nsw i32 %41, 4
%43 = and i32 %42, 3840
%44 = and i32 %41, 15
%45 = add nuw nsw i32 %37, 18
%46 = add nuw nsw i32 %45, %43
%47 = add nuw nsw i32 %44, 2
br label %51
48: ; preds = %51
%49 = add nuw nsw i32 %52, 1
%50 = icmp eq i32 %52, %47
br i1 %50, label %63, label %51
51: ; preds = %33, %48
%52 = phi i32 [ 0, %33 ], [ %49, %48 ]
%53 = add nuw nsw i32 %46, %52
%54 = and i32 %53, 4095
%55 = zext i32 %54 to i64
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = call i64 @LZSSBufferWrite(i32 %2, i8* %6, i32* %5, i8 zeroext %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %48, label %82
60: ; preds = %30
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %82
63: ; preds = %48, %24, %14
%64 = phi i32 [ %12, %14 ], [ %12, %24 ], [ %31, %48 ]
%65 = phi i32 [ -1, %14 ], [ %11, %24 ], [ %11, %48 ]
%66 = phi i16 [ %18, %14 ], [ %10, %24 ], [ %10, %48 ]
%67 = add nsw i32 %64, 1
%68 = add nsw i32 %65, 1
%69 = icmp slt i32 %67, %1
br i1 %69, label %9, label %70
70: ; preds = %63
%71 = load i32, i32* %5
%72 = icmp eq i32 %71, 0
br i1 %72, label %82, label %73
73: ; preds = %70
%74 = call i32 @write(i32 %2, i8* %6, i32 %71)
%75 = load i32, i32* %5
%76 = icmp eq i32 %74, %75
br i1 %76, label %82, label %77
77: ; preds = %73
%78 = load i32, i32* @stderr
%79 = load i32, i32* @errno
%80 = call i8* @strerror(i32 %79)
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %24, %51, %3, %70, %73, %77, %60
%83 = phi i32 [ 1, %60 ], [ 1, %77 ], [ 0, %73 ], [ 0, %70 ], [ 0, %3 ], [ 1, %51 ], [ 1, %24 ]
ret i32 %83
}