#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int cobs_decode(char *in_buf, int n, char *out_buf) {
  int out_ptr = 0;
  unsigned char total = 255;
  unsigned char ctr = 255;
  for (int i = 0; i < n; i++) {
    if (in_buf[i] == 0) {
      fprintf(stderr,
              "ERROR: "
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/"
              "serial/serial.c"
              " line %d: "
              "Invalid character"
              "\n",
              131);
      return -1;
    }
    if (ctr == total) {
      if (total < 255) {
        out_buf[out_ptr++] = 0;
      }
      total = in_buf[i];
      ctr = 1;
    } else {
      out_buf[out_ptr++] = in_buf[i];
      ctr++;
    }
  }
  if (ctr != total) {
    fprintf(stderr,
            "ERROR: "
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/"
            "serial/serial.c"
            " line %d: "
            "Generic decode error"
            "\n",
            153);
    return -2;
  }
  return out_ptr;
}


x86 assembly (clang, -O0):
<cobs_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movb   $0xff,-0x25(%rbp)
movb   $0xff,-0x26(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1207 <cobs_decode+0xf7>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1180 <cobs_decode+0x70>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    0xe96(%rip),%rsi
mov    $0x83,%edx
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <cobs_decode+0x133>
movzbl -0x26(%rbp),%eax
movzbl -0x25(%rbp),%ecx
cmp    %ecx,%eax
jne    11cc <cobs_decode+0xbc>
movzbl -0x25(%rbp),%eax
cmp    $0xff,%eax
jge    11b5 <cobs_decode+0xa5>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x24(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x25(%rbp)
movb   $0x1,-0x26(%rbp)
jmp    11f4 <cobs_decode+0xe4>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x26(%rbp),%al
add    $0x1,%al
mov    %al,-0x26(%rbp)
jmp    11f9 <cobs_decode+0xe9>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1139 <cobs_decode+0x29>
movzbl -0x26(%rbp),%eax
movzbl -0x25(%rbp),%ecx
cmp    %ecx,%eax
je     123d <cobs_decode+0x12d>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
mov    $0x99,%edx
call   1030 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1243 <cobs_decode+0x133>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cobs_decode>:
push   %rax
test   %esi,%esi
jle    1176 <cobs_decode+0x66>
mov    %esi,%r8d
xor    %esi,%esi
mov    $0xff,%r10b
mov    $0xff,%r9b
xor    %eax,%eax
jmp    1147 <cobs_decode+0x37>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %eax,%r11
add    $0x1,%eax
mov    %cl,(%rdx,%r11,1)
add    $0x1,%r10b
add    $0x1,%rsi
cmp    %rsi,%r8
je     119b <cobs_decode+0x8b>
movzbl (%rdi,%rsi,1),%ecx
test   %cl,%cl
je     117a <cobs_decode+0x6a>
cmp    %r9b,%r10b
jne    1130 <cobs_decode+0x20>
cmp    $0xff,%r10b
je     116e <cobs_decode+0x5e>
movslq %eax,%rcx
add    $0x1,%eax
movb   $0x0,(%rdx,%rcx,1)
movzbl (%rdi,%rsi,1),%r9d
mov    $0x1,%r10b
jmp    113e <cobs_decode+0x2e>
mov    %ecx,%r9d
mov    $0x1,%r10b
jmp    113e <cobs_decode+0x2e>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    $0x83,%edx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
cmp    %r9b,%r10b
je     11bf <cobs_decode+0xaf>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xeff(%rip),%rsi
mov    $0x99,%edx
call   1030 <fprintf@plt>
mov    $0xfffffffe,%eax
pop    %rcx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [175 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: Invalid character\0A\00"
@.str.1 = [178 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: Generic decode error\0A\00"
define i32 @cobs_decode(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8
%10 = alloca i8
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %8
store i8 -1, i8* %9
store i8 -1, i8* %10
store i32 0, i32* %11
br label %12
12: ; preds = %63, %3
%13 = load i32, i32* %11
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %66
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = load i32, i32* %11
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([175 x i8], [175 x i8]* @.str, i64 0, i64 0), i32 131)
store i32 -1, i32* %4
br label %77
27: ; preds = %16
%28 = load i8, i8* %10
%29 = zext i8 %28 to i32
%30 = load i8, i8* %9
%31 = zext i8 %30 to i32
%32 = icmp eq i32 %29, %31
br i1 %32, label %33, label %49
33: ; preds = %27
%34 = load i8, i8* %9
%35 = zext i8 %34 to i32
%36 = icmp slt i32 %35, 255
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i8*, i8** %7
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %37, %33
%44 = load i8*, i8** %5
%45 = load i32, i32* %11
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
store i8 %48, i8* %9
store i8 1, i8* %10
br label %62
49: ; preds = %27
%50 = load i8*, i8** %5
%51 = load i32, i32* %11
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53
%55 = load i8*, i8** %7
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
%58 = sext i32 %56 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 %54, i8* %59
%60 = load i8, i8* %10
%61 = add i8 %60, 1
store i8 %61, i8* %10
br label %62
62: ; preds = %49, %43
br label %63
63: ; preds = %62
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 1
store i32 %65, i32* %11
br label %12
66: ; preds = %12
%67 = load i8, i8* %10
%68 = zext i8 %67 to i32
%69 = load i8, i8* %9
%70 = zext i8 %69 to i32
%71 = icmp ne i32 %68, %70
br i1 %71, label %72, label %75
72: ; preds = %66
%73 = load i32, i32* @stderr
%74 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.1, i64 0, i64 0), i32 153)
store i32 -2, i32* %4
br label %77
75: ; preds = %66
%76 = load i32, i32* %8
store i32 %76, i32* %4
br label %77
77: ; preds = %75, %72, %24
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [175 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: Invalid character\0A\00"
@.str.1 = [178 x i8] c"ERROR: /scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/zbanks/flux/refs/heads/master/lib/serial/serial.c line %d: Generic decode error\0A\00"
define i32 @cobs_decode(i8* nocapture %0, i32 %1, i8* nocapture writeonly %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %43
5: ; preds = %3
%6 = zext i32 %1 to i64
br label %7
7: ; preds = %5, %32
%8 = phi i64 [ 0, %5 ], [ %36, %32 ]
%9 = phi i8 [ -1, %5 ], [ %35, %32 ]
%10 = phi i8 [ -1, %5 ], [ %34, %32 ]
%11 = phi i32 [ 0, %5 ], [ %33, %32 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %8
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([175 x i8], [175 x i8]* @.str, i64 0, i64 0), i32 131)
br label %43
18: ; preds = %7
%19 = icmp eq i8 %9, %10
br i1 %19, label %20, label %27
20: ; preds = %18
%21 = icmp eq i8 %9, -1
br i1 %21, label %32, label %22
22: ; preds = %20
%23 = add nsw i32 %11, 1
%24 = sext i32 %11 to i64
%25 = getelementptr inbounds i8, i8* %2, i64 %24
store i8 0, i8* %25
%26 = load i8, i8* %12
br label %32
27: ; preds = %18
%28 = add nsw i32 %11, 1
%29 = sext i32 %11 to i64
%30 = getelementptr inbounds i8, i8* %2, i64 %29
store i8 %13, i8* %30
%31 = add i8 %9, 1
br label %32
32: ; preds = %20, %22, %27
%33 = phi i32 [ %28, %27 ], [ %23, %22 ], [ %11, %20 ]
%34 = phi i8 [ %10, %27 ], [ %26, %22 ], [ %13, %20 ]
%35 = phi i8 [ %31, %27 ], [ 1, %22 ], [ 1, %20 ]
%36 = add nuw nsw i64 %8, 1
%37 = icmp eq i64 %36, %6
br i1 %37, label %38, label %7
38: ; preds = %32
%39 = icmp eq i8 %35, %34
br i1 %39, label %43, label %40
40: ; preds = %38
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([178 x i8], [178 x i8]* @.str.1, i64 0, i64 0), i32 153)
br label %43
43: ; preds = %3, %15, %38, %40
%44 = phi i32 [ -2, %40 ], [ -1, %15 ], [ %33, %38 ], [ 0, %3 ]
ret i32 %44
}