#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 enum state { ____Placeholder_state } state;

/* Variables and functions */
int EBADMSG;
int EIO;
int EMSGSIZE;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int read(int const, char *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int teleinfo_read_frame_ext(const int fd, char *const buffer,
                            const size_t buflen, int *error_counter) {
  char *p = buffer;
  size_t s = 0;
  char c;
  enum state { INIT, FRAME_BEGIN, FRAME_END, MSG_BEGIN, MSG_END };
  enum state current_state = INIT;
  int error_count = 0;
  int bytes_in_init_mode = 0;
  do {
    int res = read(fd, &c, 1);
    if (!res) {
      syslog(LOG_ERR, "unable to read from source\n");
      return EIO;
    }
    switch (c) {
    case '\x02':
      if (current_state != INIT) {
        error_count++;
      }
      current_state = FRAME_BEGIN;
      p = buffer;
      s = 0;
      break;
    case '\x0a':
      if (current_state != INIT) {
        if ((current_state != FRAME_BEGIN) && (current_state != MSG_END)) {
          error_count++;
          current_state = INIT;
        } else {
          current_state = MSG_BEGIN;
          if (s < buflen) {
            *p++ = c;
            s++;
          } else {
            return EMSGSIZE;
          }
        }
      }
      break;
    case '\x0d':
      if (current_state != INIT) {
        if (current_state != MSG_BEGIN) {
          error_count++;
          current_state = INIT;
        } else {
          current_state = MSG_END;
          if (s < buflen) {
            *p++ = c;
            s++;
          } else {
            return EMSGSIZE;
          }
        }
      }
      break;
    case '\x03':
      if (current_state != INIT) {
        if (current_state != MSG_END) {
          error_count++;
          current_state = INIT;
        } else {
          current_state = FRAME_END;
        }
      }
      break;
    case '\x04':
      syslog(LOG_INFO, "frame have been interrupted by EOT, resetting frame");
      current_state = INIT;
      break;
    default:
      switch (current_state) {
      case INIT:
        break;
      case FRAME_BEGIN:
        current_state = INIT;
        error_count++;
        break;
      case FRAME_END:
        break;
      case MSG_BEGIN:
        if (s < buflen) {
          *p++ = c;
          s++;
        } else {
          return EMSGSIZE;
        }
        break;
      case MSG_END:
        current_state = INIT;
        error_count++;
        break;
      }
    }
    if (current_state == INIT)
      bytes_in_init_mode++;
  } while ((current_state != FRAME_END) && (error_count < 10) &&
           (bytes_in_init_mode < (26 * 32) * 2));
  if (error_counter != NULL) {
    *error_counter = error_count;
  }
  if (current_state == FRAME_END) {
    return 0;
  } else {
    syslog(LOG_INFO, "too many error while reading, giving up");
    return EBADMSG;
  }
}


x86 assembly (clang, -O0):
<teleinfo_read_frame_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    11a0 <teleinfo_read_frame_ext+0x80>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xeba(%rip),%rsi
call   1040 <syslog@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    149f <teleinfo_read_frame_ext+0x37f>
movsbl -0x31(%rbp),%eax
add    $0xfffffffe,%eax
mov    %eax,%ecx
mov    %rcx,-0x50(%rbp)
sub    $0xb,%eax
ja     1364 <teleinfo_read_frame_ext+0x244>
mov    -0x50(%rbp),%rax
lea    0xe3f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpl   $0x0,-0x38(%rbp)
je     11dd <teleinfo_read_frame_ext+0xbd>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
jmp    1406 <teleinfo_read_frame_ext+0x2e6>
cmpl   $0x0,-0x38(%rbp)
je     1281 <teleinfo_read_frame_ext+0x161>
cmpl   $0x1,-0x38(%rbp)
je     122c <teleinfo_read_frame_ext+0x10c>
cmpl   $0x4,-0x38(%rbp)
je     122c <teleinfo_read_frame_ext+0x10c>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
jmp    127c <teleinfo_read_frame_ext+0x15c>
movl   $0x3,-0x38(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1266 <teleinfo_read_frame_ext+0x146>
mov    -0x31(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1277 <teleinfo_read_frame_ext+0x157>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    149f <teleinfo_read_frame_ext+0x37f>
jmp    127c <teleinfo_read_frame_ext+0x15c>
jmp    1281 <teleinfo_read_frame_ext+0x161>
jmp    1406 <teleinfo_read_frame_ext+0x2e6>
cmpl   $0x0,-0x38(%rbp)
je     1304 <teleinfo_read_frame_ext+0x1e4>
cmpl   $0x3,-0x38(%rbp)
je     12af <teleinfo_read_frame_ext+0x18f>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
jmp    12ff <teleinfo_read_frame_ext+0x1df>
movl   $0x4,-0x38(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    12e9 <teleinfo_read_frame_ext+0x1c9>
mov    -0x31(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12fa <teleinfo_read_frame_ext+0x1da>
mov    0x2cf8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    149f <teleinfo_read_frame_ext+0x37f>
jmp    12ff <teleinfo_read_frame_ext+0x1df>
jmp    1304 <teleinfo_read_frame_ext+0x1e4>
jmp    1406 <teleinfo_read_frame_ext+0x2e6>
cmpl   $0x0,-0x38(%rbp)
je     133e <teleinfo_read_frame_ext+0x21e>
cmpl   $0x4,-0x38(%rbp)
je     1332 <teleinfo_read_frame_ext+0x212>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
jmp    1339 <teleinfo_read_frame_ext+0x219>
movl   $0x2,-0x38(%rbp)
jmp    133e <teleinfo_read_frame_ext+0x21e>
jmp    1406 <teleinfo_read_frame_ext+0x2e6>
mov    0x2c7e(%rip),%rax
mov    (%rax),%edi
lea    0xd0d(%rip),%rsi
call   1040 <syslog@plt>
movl   $0x0,-0x38(%rbp)
jmp    1406 <teleinfo_read_frame_ext+0x2e6>
mov    -0x38(%rbp),%eax
mov    %rax,-0x58(%rbp)
sub    $0x4,%rax
ja     1401 <teleinfo_read_frame_ext+0x2e1>
mov    -0x58(%rbp),%rax
lea    0xcb0(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    1401 <teleinfo_read_frame_ext+0x2e1>
movl   $0x0,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1401 <teleinfo_read_frame_ext+0x2e1>
jmp    1401 <teleinfo_read_frame_ext+0x2e1>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    13db <teleinfo_read_frame_ext+0x2bb>
mov    -0x31(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    13ec <teleinfo_read_frame_ext+0x2cc>
mov    0x2c06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    149f <teleinfo_read_frame_ext+0x37f>
jmp    1401 <teleinfo_read_frame_ext+0x2e1>
movl   $0x0,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1406 <teleinfo_read_frame_ext+0x2e6>
cmpl   $0x0,-0x38(%rbp)
jne    1419 <teleinfo_read_frame_ext+0x2f9>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    141e <teleinfo_read_frame_ext+0x2fe>
xor    %eax,%eax
cmpl   $0x2,-0x38(%rbp)
mov    %al,-0x59(%rbp)
je     1449 <teleinfo_read_frame_ext+0x329>
xor    %eax,%eax
cmpl   $0xa,-0x3c(%rbp)
mov    %al,-0x59(%rbp)
jge    1449 <teleinfo_read_frame_ext+0x329>
cmpl   $0x680,-0x40(%rbp)
setl   %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    115c <teleinfo_read_frame_ext+0x3c>
cmpq   $0x0,-0x20(%rbp)
je     1468 <teleinfo_read_frame_ext+0x348>
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x2,-0x38(%rbp)
jne    147e <teleinfo_read_frame_ext+0x35e>
movl   $0x0,-0x4(%rbp)
jmp    149f <teleinfo_read_frame_ext+0x37f>
mov    0x2b43(%rip),%rax
mov    (%rax),%edi
lea    0xc06(%rip),%rsi
call   1040 <syslog@plt>
mov    0x2b46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<teleinfo_read_frame_ext>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x18(%rsp)
mov    %rdx,0x10(%rsp)
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
mov    %rax,0x8(%rsp)
lea    0xeb5(%rip),%r13
mov    %rsi,0x20(%rsp)
xor    %r15d,%r15d
xor    %r14d,%r14d
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %ebp,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
je     12c7 <teleinfo_read_frame_ext+0x1a7>
movsbl 0x7(%rsp),%eax
mov    %eax,%ecx
add    $0xfffffffe,%ecx
cmp    $0xb,%ecx
ja     11e3 <teleinfo_read_frame_ext+0xc3>
movslq 0x0(%r13,%rcx,4),%rcx
add    %r13,%rcx
jmp    *%rcx
cmp    $0x1,%r15d
sbb    $0xffffffff,%r14d
mov    $0x1,%r15d
mov    0x20(%rsp),%rbx
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    127b <teleinfo_read_frame_ext+0x15b>
test   %r15d,%r15d
je     1274 <teleinfo_read_frame_ext+0x154>
cmp    $0x4,%r15d
jne    1270 <teleinfo_read_frame_ext+0x150>
jmp    12fd <teleinfo_read_frame_ext+0x1dd>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
call   1040 <syslog@plt>
jmp    1274 <teleinfo_read_frame_ext+0x154>
cmp    $0x4,%r15d
ja     127b <teleinfo_read_frame_ext+0x15b>
mov    %r15d,%ecx
lea    0xe39(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
test   %r15d,%r15d
je     1274 <teleinfo_read_frame_ext+0x154>
cmp    $0x1,%r15d
je     1211 <teleinfo_read_frame_ext+0xf1>
cmp    $0x4,%r15d
jne    1270 <teleinfo_read_frame_ext+0x150>
mov    0x8(%rsp),%rcx
cmp    0x10(%rsp),%rcx
jae    12f4 <teleinfo_read_frame_ext+0x1d4>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%rcx
mov    %rcx,0x8(%rsp)
mov    $0x3,%r15d
jmp    127b <teleinfo_read_frame_ext+0x15b>
test   %r15d,%r15d
je     1274 <teleinfo_read_frame_ext+0x154>
cmp    $0x3,%r15d
jne    1270 <teleinfo_read_frame_ext+0x150>
mov    0x8(%rsp),%rcx
cmp    0x10(%rsp),%rcx
jae    12f4 <teleinfo_read_frame_ext+0x1d4>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%rcx
mov    %rcx,0x8(%rsp)
mov    $0x4,%r15d
jmp    127b <teleinfo_read_frame_ext+0x15b>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14d
add    $0x1,%r12d
xor    %r15d,%r15d
cmp    $0x2,%r15d
je     1294 <teleinfo_read_frame_ext+0x174>
cmp    $0x9,%r14d
jg     1294 <teleinfo_read_frame_ext+0x174>
cmp    $0x680,%r12d
jl     1160 <teleinfo_read_frame_ext+0x40>
mov    0x18(%rsp),%rax
test   %rax,%rax
je     12a1 <teleinfo_read_frame_ext+0x181>
mov    %r14d,(%rax)
xor    %eax,%eax
cmp    $0x2,%r15d
je     12e5 <teleinfo_read_frame_ext+0x1c5>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1040 <syslog@plt>
mov    0x2d1b(%rip),%rax
jmp    12e3 <teleinfo_read_frame_ext+0x1c3>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
call   1040 <syslog@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ced(%rip),%rax
jmp    12e3 <teleinfo_read_frame_ext+0x1c3>
mov    $0x2,%r15d
jmp    1294 <teleinfo_read_frame_ext+0x174>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [28 x i8] c"unable to read from source\0A\00"
@EIO = global i32 0
@EMSGSIZE = global i32 0
@LOG_INFO = global i32 0
@.str.1 = [52 x i8] c"frame have been interrupted by EOT, resetting frame\00"
@.str.2 = [40 x i8] c"too many error while reading, giving up\00"
@EBADMSG = global i32 0
define i32 @teleinfo_read_frame_ext(i32 %0, i8* %1, i64 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
%17 = load i8*, i8** %7
store i8* %17, i8** %10
store i64 0, i64* %11
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
br label %18
18: ; preds = %144, %4
%19 = load i32, i32* %6
%20 = call i32 @read(i32 %19, i8* %12, i32 1)
store i32 %20, i32* %16
%21 = load i32, i32* %16
%22 = icmp ne i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %18
%24 = load i32, i32* @LOG_ERR
%25 = call i32 @syslog(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EIO
store i32 %26, i32* %5
br label %160
27: ; preds = %18
%28 = load i8, i8* %12
%29 = sext i8 %28 to i32
switch i32 %29, label %104 [
i32 2, label %30
i32 10, label %38
i32 13, label %65
i32 3, label %89
i32 4, label %101
]
30: ; preds = %27
%31 = load i32, i32* %13
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %14
%35 = add nsw i32 %34, 1
store i32 %35, i32* %14
br label %36
36: ; preds = %33, %30
store i32 1, i32* %13
%37 = load i8*, i8** %7
store i8* %37, i8** %10
store i64 0, i64* %11
br label %128
38: ; preds = %27
%39 = load i32, i32* %13
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %64
41: ; preds = %38
%42 = load i32, i32* %13
%43 = icmp ne i32 %42, 1
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i32, i32* %13
%46 = icmp ne i32 %45, 4
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* %14
%49 = add nsw i32 %48, 1
store i32 %49, i32* %14
store i32 0, i32* %13
br label %63
50: ; preds = %44, %41
store i32 3, i32* %13
%51 = load i64, i64* %11
%52 = load i64, i64* %8
%53 = icmp ult i64 %51, %52
br i1 %53, label %54, label %60
54: ; preds = %50
%55 = load i8, i8* %12
%56 = load i8*, i8** %10
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %10
store i8 %55, i8* %56
%58 = load i64, i64* %11
%59 = add i64 %58, 1
store i64 %59, i64* %11
br label %62
60: ; preds = %50
%61 = load i32, i32* @EMSGSIZE
store i32 %61, i32* %5
br label %160
62: ; preds = %54
br label %63
63: ; preds = %62, %47
br label %64
64: ; preds = %63, %38
br label %128
65: ; preds = %27
%66 = load i32, i32* %13
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %88
68: ; preds = %65
%69 = load i32, i32* %13
%70 = icmp ne i32 %69, 3
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32, i32* %14
%73 = add nsw i32 %72, 1
store i32 %73, i32* %14
store i32 0, i32* %13
br label %87
74: ; preds = %68
store i32 4, i32* %13
%75 = load i64, i64* %11
%76 = load i64, i64* %8
%77 = icmp ult i64 %75, %76
br i1 %77, label %78, label %84
78: ; preds = %74
%79 = load i8, i8* %12
%80 = load i8*, i8** %10
%81 = getelementptr inbounds i8, i8* %80, i32 1
store i8* %81, i8** %10
store i8 %79, i8* %80
%82 = load i64, i64* %11
%83 = add i64 %82, 1
store i64 %83, i64* %11
br label %86
84: ; preds = %74
%85 = load i32, i32* @EMSGSIZE
store i32 %85, i32* %5
br label %160
86: ; preds = %78
br label %87
87: ; preds = %86, %71
br label %88
88: ; preds = %87, %65
br label %128
89: ; preds = %27
%90 = load i32, i32* %13
%91 = icmp ne i32 %90, 0
br i1 %91, label %92, label %100
92: ; preds = %89
%93 = load i32, i32* %13
%94 = icmp ne i32 %93, 4
br i1 %94, label %95, label %98
95: ; preds = %92
%96 = load i32, i32* %14
%97 = add nsw i32 %96, 1
store i32 %97, i32* %14
store i32 0, i32* %13
br label %99
98: ; preds = %92
store i32 2, i32* %13
br label %99
99: ; preds = %98, %95
br label %100
100: ; preds = %99, %89
br label %128
101: ; preds = %27
%102 = load i32, i32* @LOG_INFO
%103 = call i32 @syslog(i32 %102, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %13
br label %128
104: ; preds = %27
%105 = load i32, i32* %13
switch i32 %105, label %127 [
i32 0, label %106
i32 1, label %107
i32 2, label %110
i32 3, label %111
i32 4, label %124
]
106: ; preds = %104
br label %127
107: ; preds = %104
store i32 0, i32* %13
%108 = load i32, i32* %14
%109 = add nsw i32 %108, 1
store i32 %109, i32* %14
br label %127
110: ; preds = %104
br label %127
111: ; preds = %104
%112 = load i64, i64* %11
%113 = load i64, i64* %8
%114 = icmp ult i64 %112, %113
br i1 %114, label %115, label %121
115: ; preds = %111
%116 = load i8, i8* %12
%117 = load i8*, i8** %10
%118 = getelementptr inbounds i8, i8* %117, i32 1
store i8* %118, i8** %10
store i8 %116, i8* %117
%119 = load i64, i64* %11
%120 = add i64 %119, 1
store i64 %120, i64* %11
br label %123
121: ; preds = %111
%122 = load i32, i32* @EMSGSIZE
store i32 %122, i32* %5
br label %160
123: ; preds = %115
br label %127
124: ; preds = %104
store i32 0, i32* %13
%125 = load i32, i32* %14
%126 = add nsw i32 %125, 1
store i32 %126, i32* %14
br label %127
127: ; preds = %104, %124, %123, %110, %107, %106
br label %128
128: ; preds = %127, %101, %100, %88, %64, %36
%129 = load i32, i32* %13
%130 = icmp eq i32 %129, 0
br i1 %130, label %131, label %134
131: ; preds = %128
%132 = load i32, i32* %15
%133 = add nsw i32 %132, 1
store i32 %133, i32* %15
br label %134
134: ; preds = %131, %128
br label %135
135: ; preds = %134
%136 = load i32, i32* %13
%137 = icmp ne i32 %136, 2
br i1 %137, label %138, label %144
138: ; preds = %135
%139 = load i32, i32* %14
%140 = icmp slt i32 %139, 10
br i1 %140, label %141, label %144
141: ; preds = %138
%142 = load i32, i32* %15
%143 = icmp slt i32 %142, 1664
br label %144
144: ; preds = %141, %138, %135
%145 = phi i1 [ false, %138 ], [ false, %135 ], [ %143, %141 ]
br i1 %145, label %18, label %146
146: ; preds = %144
%147 = load i32*, i32** %9
%148 = icmp ne i32* %147, null
br i1 %148, label %149, label %152
149: ; preds = %146
%150 = load i32, i32* %14
%151 = load i32*, i32** %9
store i32 %150, i32* %151
br label %152
152: ; preds = %149, %146
%153 = load i32, i32* %13
%154 = icmp eq i32 %153, 2
br i1 %154, label %155, label %156
155: ; preds = %152
store i32 0, i32* %5
br label %160
156: ; preds = %152
%157 = load i32, i32* @LOG_INFO
%158 = call i32 @syslog(i32 %157, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%159 = load i32, i32* @EBADMSG
store i32 %159, i32* %5
br label %160
160: ; preds = %156, %155, %121, %84, %60, %23
%161 = load i32, i32* %5
ret i32 %161
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [28 x i8] c"unable to read from source\0A\00"
@EIO = global i32 0
@EMSGSIZE = global i32 0
@LOG_INFO = global i32 0
@.str.1 = [52 x i8] c"frame have been interrupted by EOT, resetting frame\00"
@.str.2 = [40 x i8] c"too many error while reading, giving up\00"
@EBADMSG = global i32 0
define i32 @teleinfo_read_frame_ext(i32 %0, i8* writeonly %1, i64 %2, i32* writeonly %3) {
%5 = alloca i8
br label %6
6: ; preds = %66, %4
%7 = phi i8* [ %1, %4 ], [ %67, %66 ]
%8 = phi i64 [ 0, %4 ], [ %68, %66 ]
%9 = phi i32 [ 0, %4 ], [ %69, %66 ]
%10 = phi i32 [ 0, %4 ], [ %70, %66 ]
%11 = phi i32 [ 0, %4 ], [ %71, %66 ]
%12 = call i32 @read(i32 %0, i8* %5, i32 1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %6
%15 = load i32, i32* @LOG_ERR
%16 = tail call i32 @syslog(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EIO
br label %88
18: ; preds = %6
%19 = load i8, i8* %5
%20 = sext i8 %19 to i32
switch i32 %20, label %51 [
i32 2, label %21
i32 10, label %25
i32 13, label %35
i32 3, label %45
i32 4, label %48
]
21: ; preds = %18
%22 = icmp ne i32 %9, 0
%23 = zext i1 %22 to i32
%24 = add nsw i32 %10, %23
br label %66
25: ; preds = %18
switch i32 %9, label %26 [
i32 0, label %63
i32 4, label %28
i32 1, label %28
]
26: ; preds = %25
%27 = add nsw i32 %10, 1
br label %63
28: ; preds = %25, %25
%29 = icmp ult i64 %8, %2
br i1 %29, label %30, label %33
30: ; preds = %28
%31 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %19, i8* %7
%32 = add nuw i64 %8, 1
br label %66
33: ; preds = %28
%34 = load i32, i32* @EMSGSIZE
br label %88
35: ; preds = %18
switch i32 %9, label %36 [
i32 0, label %63
i32 3, label %38
]
36: ; preds = %35
%37 = add nsw i32 %10, 1
br label %63
38: ; preds = %35
%39 = icmp ult i64 %8, %2
br i1 %39, label %40, label %43
40: ; preds = %38
%41 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %19, i8* %7
%42 = add nuw i64 %8, 1
br label %66
43: ; preds = %38
%44 = load i32, i32* @EMSGSIZE
br label %88
45: ; preds = %18
switch i32 %9, label %46 [
i32 0, label %63
i32 4, label %77
]
46: ; preds = %45
%47 = add nsw i32 %10, 1
br label %63
48: ; preds = %18
%49 = load i32, i32* @LOG_INFO
%50 = tail call i32 @syslog(i32 %49, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
br label %63
51: ; preds = %18
switch i32 %9, label %66 [
i32 4, label %61
i32 1, label %52
i32 3, label %54
i32 0, label %63
]
52: ; preds = %51
%53 = add nsw i32 %10, 1
br label %63
54: ; preds = %51
%55 = icmp ult i64 %8, %2
br i1 %55, label %56, label %59
56: ; preds = %54
%57 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %19, i8* %7
%58 = add nuw i64 %8, 1
br label %66
59: ; preds = %54
%60 = load i32, i32* @EMSGSIZE
br label %88
61: ; preds = %51
%62 = add nsw i32 %10, 1
br label %63
63: ; preds = %51, %25, %26, %35, %36, %45, %46, %48, %61, %52
%64 = phi i32 [ %10, %25 ], [ %27, %26 ], [ %10, %35 ], [ %37, %36 ], [ %10, %45 ], [ %47, %46 ], [ %10, %48 ], [ %62, %61 ], [ %53, %52 ], [ %10, %51 ]
%65 = add nsw i32 %11, 1
br label %66
66: ; preds = %51, %21, %30, %40, %56, %63
%67 = phi i8* [ %7, %63 ], [ %1, %21 ], [ %31, %30 ], [ %41, %40 ], [ %57, %56 ], [ %7, %51 ]
%68 = phi i64 [ %8, %63 ], [ 0, %21 ], [ %32, %30 ], [ %42, %40 ], [ %58, %56 ], [ %8, %51 ]
%69 = phi i32 [ 0, %63 ], [ 1, %21 ], [ 3, %30 ], [ 4, %40 ], [ 3, %56 ], [ %9, %51 ]
%70 = phi i32 [ %64, %63 ], [ %24, %21 ], [ %10, %30 ], [ %10, %40 ], [ %10, %56 ], [ %10, %51 ]
%71 = phi i32 [ %65, %63 ], [ %11, %21 ], [ %11, %30 ], [ %11, %40 ], [ %11, %56 ], [ %11, %51 ]
%72 = icmp ne i32 %69, 2
%73 = icmp slt i32 %70, 10
%74 = select i1 %72, i1 %73, i1 false
%75 = icmp slt i32 %71, 1664
%76 = select i1 %74, i1 %75, i1 false
br i1 %76, label %6, label %77
77: ; preds = %45, %66
%78 = phi i32 [ %70, %66 ], [ %10, %45 ]
%79 = phi i32 [ %69, %66 ], [ 2, %45 ]
%80 = icmp eq i32* %3, null
br i1 %80, label %82, label %81
81: ; preds = %77
store i32 %78, i32* %3
br label %82
82: ; preds = %81, %77
%83 = icmp eq i32 %79, 2
br i1 %83, label %88, label %84
84: ; preds = %82
%85 = load i32, i32* @LOG_INFO
%86 = tail call i32 @syslog(i32 %85, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%87 = load i32, i32* @EBADMSG
br label %88
88: ; preds = %14, %33, %43, %59, %82, %84
%89 = phi i32 [ %87, %84 ], [ 0, %82 ], [ %17, %14 ], [ %34, %33 ], [ %44, %43 ], [ %60, %59 ]
ret i32 %89
}