#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 uint8_t;
typedef int /*<<< orphan*/ rcv;
typedef int int32_t;
typedef int int16_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int tms_get_int(int *, int *, int);
int tms_get_type(int *, int);
int tms_msg_size(int *, int, int *);
int tms_put_chksum(int *, int);
int /*<<< orphan*/ tms_put_int(int, int *, int *, int);
int tms_rcv_msg(int, int *, int);
scalar_t__ tms_send_iddata_request(int, int, int);
int32_t tms_fetch_iddata(int32_t fd, uint8_t *msg, int32_t n) {
  int32_t i, j;
  int16_t adr = 0x0000;
  int16_t len = 0x80;
  int32_t br = 0;
  int32_t tbw = 0;
  uint8_t rcv[512];
  int32_t type;
  int32_t size;
  int32_t tsize = 0;
  int32_t start = 0;
  int32_t length = 0;
  int32_t rtc = 0;
  tbw = 0;
  tms_put_int((0xAAAA), msg, &tbw, 2);
  tms_put_int(0xFF, msg, &tbw, 1);
  tms_put_int((0x23), msg, &tbw, 1);
  tms_put_int(0, msg, &tbw, 4);
  adr = 0x0000;
  len = 0x80;
  rtc = 0;
  while ((rtc < 10) && (len > 0) && (tbw < n)) {
    rtc++;
    if (tms_send_iddata_request(fd, adr, len) < 0) {
      continue;
    }
    br = tms_rcv_msg(fd, rcv, sizeof(rcv));
    type = tms_get_type(rcv, br);
    if (type != (0x23)) {
      fprintf(stderr, "# Warning: tms_get_iddata: unexpected type 0x%02X\n",
              type);
      continue;
    } else {
      size = tms_msg_size(rcv, sizeof(rcv), &i);
      start = tms_get_int(rcv, &i, 2);
      length = tms_get_int(rcv, &i, 2);
      if (tbw + 2 * length > n) {
        fprintf(stderr, "# Error: tms_get_iddata: msg too small %d\n",
                tbw + 2 * length);
      } else {
        for (j = 0; j < 2 * length; j++) {
          msg[tbw + j] = rcv[i + j];
        }
        tbw += 2 * length;
        tsize += length;
      }
      adr += length;
      if ((rcv[2 * size - 2] == 0xFF) && (rcv[2 * size - 1] == 0xFF)) {
        len = 0;
      }
    }
  }
  i = 4;
  tms_put_int(tsize, msg, &i, 4);
  tbw = tms_put_chksum(msg, tbw);
  return (tbw);
}


x86 assembly (clang, -O0):
<tms_fetch_iddata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x80,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x83c(%rbp)
movl   $0x0,-0x840(%rbp)
movl   $0x0,-0x844(%rbp)
movl   $0x0,-0x848(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rsi
mov    $0xaaaa,%edi
lea    -0x2c(%rbp),%rdx
mov    $0x2,%ecx
call   1090 <tms_put_int@plt>
mov    -0x10(%rbp),%rsi
mov    $0xff,%edi
lea    -0x2c(%rbp),%rdx
mov    $0x1,%ecx
call   1090 <tms_put_int@plt>
mov    -0x10(%rbp),%rsi
mov    $0x23,%edi
lea    -0x2c(%rbp),%rdx
mov    $0x1,%ecx
call   1090 <tms_put_int@plt>
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
lea    -0x2c(%rbp),%rdx
mov    $0x4,%ecx
call   1090 <tms_put_int@plt>
movl   $0x0,-0x20(%rbp)
movl   $0x80,-0x24(%rbp)
movl   $0x0,-0x848(%rbp)
xor    %eax,%eax
cmpl   $0xa,-0x848(%rbp)
mov    %al,-0x849(%rbp)
jge    1287 <tms_fetch_iddata+0x107>
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x849(%rbp)
jle    1287 <tms_fetch_iddata+0x107>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x849(%rbp)
mov    -0x849(%rbp),%al
test   $0x1,%al
jne    129a <tms_fetch_iddata+0x11a>
jmp    1473 <tms_fetch_iddata+0x2f3>
mov    -0x848(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x848(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
call   10a0 <tms_send_iddata_request@plt>
cmp    $0x0,%rax
jge    12c6 <tms_fetch_iddata+0x146>
jmp    1251 <tms_fetch_iddata+0xd1>
mov    -0x4(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    $0x800,%edx
call   1080 <tms_rcv_msg@plt>
mov    %eax,-0x28(%rbp)
lea    -0x830(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1050 <tms_get_type@plt>
mov    %eax,-0x834(%rbp)
cmpl   $0x23,-0x834(%rbp)
je     131f <tms_fetch_iddata+0x19f>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
mov    -0x834(%rbp),%edx
lea    0xceb(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1251 <tms_fetch_iddata+0xd1>
lea    -0x830(%rbp),%rdi
mov    $0x800,%esi
lea    -0x18(%rbp),%rdx
call   1060 <tms_msg_size@plt>
mov    %eax,-0x838(%rbp)
lea    -0x830(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x2,%edx
call   1040 <tms_get_int@plt>
mov    %eax,-0x840(%rbp)
lea    -0x830(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x2,%edx
call   1040 <tms_get_int@plt>
mov    %eax,-0x844(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x844(%rbp),%ecx
shl    $0x1,%ecx
add    %ecx,%eax
cmp    -0x14(%rbp),%eax
jle    13af <tms_fetch_iddata+0x22f>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
mov    -0x844(%rbp),%eax
shl    $0x1,%eax
add    %eax,%edx
lea    0xc8e(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1418 <tms_fetch_iddata+0x298>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x844(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    13f7 <tms_fetch_iddata+0x277>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
cltq
mov    -0x830(%rbp,%rax,4),%edx
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    13b6 <tms_fetch_iddata+0x236>
mov    -0x844(%rbp),%eax
shl    $0x1,%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x844(%rbp),%eax
add    -0x83c(%rbp),%eax
mov    %eax,-0x83c(%rbp)
mov    -0x844(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x838(%rbp),%eax
shl    $0x1,%eax
sub    $0x2,%eax
cltq
cmpl   $0xff,-0x830(%rbp,%rax,4)
jne    1469 <tms_fetch_iddata+0x2e9>
mov    -0x838(%rbp),%eax
shl    $0x1,%eax
sub    $0x1,%eax
cltq
cmpl   $0xff,-0x830(%rbp,%rax,4)
jne    1469 <tms_fetch_iddata+0x2e9>
movl   $0x0,-0x24(%rbp)
jmp    146e <tms_fetch_iddata+0x2ee>
jmp    1251 <tms_fetch_iddata+0xd1>
movl   $0x4,-0x18(%rbp)
mov    -0x83c(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x4,%ecx
call   1090 <tms_put_int@plt>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <tms_put_chksum@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tms_fetch_iddata>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x838,%rsp
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %edi,%ebx
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%r14
mov    $0xaaaa,%edi
mov    %r14,%rdx
mov    $0x2,%ecx
call   10a0 <tms_put_int@plt>
mov    $0xff,%edi
mov    %rbp,%rsi
mov    %r14,%rdx
mov    $0x1,%ecx
call   10a0 <tms_put_int@plt>
mov    $0x23,%edi
mov    %rbp,%rsi
mov    %r14,%rdx
mov    $0x1,%ecx
call   10a0 <tms_put_int@plt>
movl   $0x0,0x14(%rsp)
xor    %edi,%edi
mov    %rbp,0x28(%rsp)
mov    %rbp,%rsi
mov    %r14,%rdx
mov    $0x4,%ecx
call   10a0 <tms_put_int@plt>
cmp    %r12d,0xc(%rsp)
jge    1396 <tms_fetch_iddata+0x206>
mov    $0x80,%ebp
lea    0x30(%rsp),%r13
xor    %r15d,%r15d
movl   $0x0,0x14(%rsp)
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14d,%esi
mov    %ebp,%edx
call   10b0 <tms_send_iddata_request@plt>
test   %rax,%rax
js     1286 <tms_fetch_iddata+0xf6>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x800,%edx
call   1090 <tms_rcv_msg@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   1050 <tms_get_type@plt>
cmp    $0x23,%eax
je     12a0 <tms_fetch_iddata+0x110>
mov    0x2d6a(%rip),%rcx
mov    (%rcx),%edi
lea    0xd81(%rip),%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
cmp    $0x8,%r15d
jg     1396 <tms_fetch_iddata+0x206>
add    $0x1,%r15d
cmp    %r12d,0xc(%rsp)
jl     1240 <tms_fetch_iddata+0xb0>
jmp    1396 <tms_fetch_iddata+0x206>
mov    %r13,%rdi
mov    $0x800,%esi
lea    0x10(%rsp),%rdx
call   1060 <tms_msg_size@plt>
mov    %eax,0x24(%rsp)
mov    %r13,%rdi
lea    0x10(%rsp),%rsi
mov    $0x2,%edx
call   1040 <tms_get_int@plt>
mov    %r13,%rdi
lea    0x10(%rsp),%rsi
mov    $0x2,%edx
call   1040 <tms_get_int@plt>
mov    %eax,%ecx
movslq 0xc(%rsp),%rax
lea    (%rax,%rcx,2),%edx
cmp    %r12d,%edx
jle    130a <tms_fetch_iddata+0x17a>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
mov    %rcx,0x18(%rsp)
call   1070 <fprintf@plt>
mov    0x18(%rsp),%rcx
jmp    1355 <tms_fetch_iddata+0x1c5>
test   %ecx,%ecx
jle    134d <tms_fetch_iddata+0x1bd>
mov    %edx,0x20(%rsp)
lea    (%rcx,%rcx,1),%edx
mov    %rcx,0x18(%rsp)
movslq 0x10(%rsp),%rcx
mov    0x28(%rsp),%rsi
lea    (%rsi,%rax,4),%rdi
lea    (%rsp,%rcx,4),%rsi
add    $0x30,%rsi
cmp    $0x2,%edx
mov    $0x1,%eax
cmovl  %eax,%edx
shl    $0x2,%rdx
call   1080 <memcpy@plt>
mov    0x20(%rsp),%edx
mov    0x18(%rsp),%rcx
mov    %edx,0xc(%rsp)
add    %ecx,0x14(%rsp)
mov    0x24(%rsp),%eax
add    %eax,%eax
cltq
cmpl   $0xff,0x28(%rsp,%rax,4)
jne    1377 <tms_fetch_iddata+0x1e7>
cmpl   $0xff,0x2c(%rsp,%rax,4)
mov    $0x0,%eax
cmove  %eax,%ebp
cmp    $0x8,%r15d
jg     1396 <tms_fetch_iddata+0x206>
test   %ebp,%ebp
jle    1396 <tms_fetch_iddata+0x206>
add    $0x1,%r15d
add    %r14d,%ecx
mov    %ecx,%r14d
cmp    %r12d,0xc(%rsp)
jl     1240 <tms_fetch_iddata+0xb0>
movl   $0x4,0x10(%rsp)
lea    0x10(%rsp),%rdx
mov    0x14(%rsp),%edi
mov    0x28(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x4,%ecx
call   10a0 <tms_put_int@plt>
mov    0xc(%rsp),%esi
mov    %rbx,%rdi
call   1030 <tms_put_chksum@plt>
add    $0x838,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [51 x i8] c"# Warning: tms_get_iddata: unexpected type 0x%02X\0A\00"
@.str.1 = [43 x i8] c"# Error: tms_get_iddata: msg too small %d\0A\00"
define i32 @tms_fetch_iddata(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [512 x i32]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %9
store i32 128, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %16
store i32 0, i32* %17
store i32 0, i32* %18
store i32 0, i32* %19
store i32 0, i32* %12
%20 = load i32*, i32** %5
%21 = call i32 @tms_put_int(i32 43690, i32* %20, i32* %12, i32 2)
%22 = load i32*, i32** %5
%23 = call i32 @tms_put_int(i32 255, i32* %22, i32* %12, i32 1)
%24 = load i32*, i32** %5
%25 = call i32 @tms_put_int(i32 35, i32* %24, i32* %12, i32 1)
%26 = load i32*, i32** %5
%27 = call i32 @tms_put_int(i32 0, i32* %26, i32* %12, i32 4)
store i32 0, i32* %9
store i32 128, i32* %10
store i32 0, i32* %19
br label %28
28: ; preds = %133, %58, %48, %3
%29 = load i32, i32* %19
%30 = icmp slt i32 %29, 10
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load i32, i32* %10
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* %12
%36 = load i32, i32* %6
%37 = icmp slt i32 %35, %36
br label %38
38: ; preds = %34, %31, %28
%39 = phi i1 [ false, %31 ], [ false, %28 ], [ %37, %34 ]
br i1 %39, label %40, label %134
40: ; preds = %38
%41 = load i32, i32* %19
%42 = add nsw i32 %41, 1
store i32 %42, i32* %19
%43 = load i32, i32* %4
%44 = load i32, i32* %9
%45 = load i32, i32* %10
%46 = call i64 @tms_send_iddata_request(i32 %43, i32 %44, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %40
br label %28
49: ; preds = %40
%50 = load i32, i32* %4
%51 = getelementptr inbounds [512 x i32], [512 x i32]* %13, i64 0, i64 0
%52 = call i32 @tms_rcv_msg(i32 %50, i32* %51, i32 2048)
store i32 %52, i32* %11
%53 = getelementptr inbounds [512 x i32], [512 x i32]* %13, i64 0, i64 0
%54 = load i32, i32* %11
%55 = call i32 @tms_get_type(i32* %53, i32 %54)
store i32 %55, i32* %14
%56 = load i32, i32* %14
%57 = icmp ne i32 %56, 35
br i1 %57, label %58, label %62
58: ; preds = %49
%59 = load i32, i32* @stderr
%60 = load i32, i32* %14
%61 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %60)
br label %28
62: ; preds = %49
%63 = getelementptr inbounds [512 x i32], [512 x i32]* %13, i64 0, i64 0
%64 = call i32 @tms_msg_size(i32* %63, i32 2048, i32* %7)
store i32 %64, i32* %15
%65 = getelementptr inbounds [512 x i32], [512 x i32]* %13, i64 0, i64 0
%66 = call i32 @tms_get_int(i32* %65, i32* %7, i32 2)
store i32 %66, i32* %17
%67 = getelementptr inbounds [512 x i32], [512 x i32]* %13, i64 0, i64 0
%68 = call i32 @tms_get_int(i32* %67, i32* %7, i32 2)
store i32 %68, i32* %18
%69 = load i32, i32* %12
%70 = load i32, i32* %18
%71 = mul nsw i32 2, %70
%72 = add nsw i32 %69, %71
%73 = load i32, i32* %6
%74 = icmp sgt i32 %72, %73
br i1 %74, label %75, label %82
75: ; preds = %62
%76 = load i32, i32* @stderr
%77 = load i32, i32* %12
%78 = load i32, i32* %18
%79 = mul nsw i32 2, %78
%80 = add nsw i32 %77, %79
%81 = call i32 @fprintf(i32 %76, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %80)
br label %112
82: ; preds = %62
store i32 0, i32* %8
br label %83
83: ; preds = %101, %82
%84 = load i32, i32* %8
%85 = load i32, i32* %18
%86 = mul nsw i32 2, %85
%87 = icmp slt i32 %84, %86
br i1 %87, label %88, label %104
88: ; preds = %83
%89 = load i32, i32* %7
%90 = load i32, i32* %8
%91 = add nsw i32 %89, %90
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds [512 x i32], [512 x i32]* %13, i64 0, i64 %92
%94 = load i32, i32* %93
%95 = load i32*, i32** %5
%96 = load i32, i32* %12
%97 = load i32, i32* %8
%98 = add nsw i32 %96, %97
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds i32, i32* %95, i64 %99
store i32 %94, i32* %100
br label %101
101: ; preds = %88
%102 = load i32, i32* %8
%103 = add nsw i32 %102, 1
store i32 %103, i32* %8
br label %83
104: ; preds = %83
%105 = load i32, i32* %18
%106 = mul nsw i32 2, %105
%107 = load i32, i32* %12
%108 = add nsw i32 %107, %106
store i32 %108, i32* %12
%109 = load i32, i32* %18
%110 = load i32, i32* %16
%111 = add nsw i32 %110, %109
store i32 %111, i32* %16
br label %112
112: ; preds = %104, %75
%113 = load i32, i32* %18
%114 = load i32, i32* %9
%115 = add nsw i32 %114, %113
store i32 %115, i32* %9
%116 = load i32, i32* %15
%117 = mul nsw i32 2, %116
%118 = sub nsw i32 %117, 2
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds [512 x i32], [512 x i32]* %13, i64 0, i64 %119
%121 = load i32, i32* %120
%122 = icmp eq i32 %121, 255
br i1 %122, label %123, label %132
123: ; preds = %112
%124 = load i32, i32* %15
%125 = mul nsw i32 2, %124
%126 = sub nsw i32 %125, 1
%127 = sext i32 %126 to i64
%128 = getelementptr inbounds [512 x i32], [512 x i32]* %13, i64 0, i64 %127
%129 = load i32, i32* %128
%130 = icmp eq i32 %129, 255
br i1 %130, label %131, label %132
131: ; preds = %123
store i32 0, i32* %10
br label %132
132: ; preds = %131, %123, %112
br label %133
133: ; preds = %132
br label %28
134: ; preds = %38
store i32 4, i32* %7
%135 = load i32, i32* %16
%136 = load i32*, i32** %5
%137 = call i32 @tms_put_int(i32 %135, i32* %136, i32* %7, i32 4)
%138 = load i32*, i32** %5
%139 = load i32, i32* %12
%140 = call i32 @tms_put_chksum(i32* %138, i32 %139)
store i32 %140, i32* %12
%141 = load i32, i32* %12
ret i32 %141
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [51 x i8] c"# Warning: tms_get_iddata: unexpected type 0x%02X\0A\00"
@.str.1 = [43 x i8] c"# Error: tms_get_iddata: msg too small %d\0A\00"
define i32 @tms_fetch_iddata(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i32]
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast [512 x i32]* %6 to i8*
store i32 0, i32* %5
%10 = call i32 @tms_put_int(i32 43690, i32* %1, i32* %5, i32 2)
%11 = call i32 @tms_put_int(i32 255, i32* %1, i32* %5, i32 1)
%12 = call i32 @tms_put_int(i32 35, i32* %1, i32* %5, i32 1)
%13 = call i32 @tms_put_int(i32 0, i32* %1, i32* %5, i32 4)
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, %2
br i1 %15, label %16, label %90
16: ; preds = %3
%17 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 0
br label %18
18: ; preds = %16, %82
%19 = phi i32 [ 0, %16 ], [ %25, %82 ]
%20 = phi i32 [ 0, %16 ], [ %67, %82 ]
%21 = phi i32 [ 0, %16 ], [ %68, %82 ]
%22 = phi i32 [ 128, %16 ], [ %83, %82 ]
br label %23
23: ; preds = %18, %35
%24 = phi i32 [ %19, %18 ], [ %25, %35 ]
%25 = add nsw i32 %24, 1
%26 = call i64 @tms_send_iddata_request(i32 %0, i32 %21, i32 %22)
%27 = icmp slt i64 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %23
%29 = call i32 @tms_rcv_msg(i32 %0, i32* %17, i32 2048)
%30 = call i32 @tms_get_type(i32* %17, i32 %29)
%31 = icmp eq i32 %30, 35
br i1 %31, label %40, label %32
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %30)
br label %35
35: ; preds = %32, %23
%36 = icmp slt i32 %24, 9
%37 = load i32, i32* %5
%38 = icmp slt i32 %37, %2
%39 = select i1 %36, i1 %38, i1 false
br i1 %39, label %23, label %90
40: ; preds = %28
%41 = call i32 @tms_msg_size(i32* %17, i32 2048, i32* %4)
%42 = call i32 @tms_get_int(i32* %17, i32* %4, i32 2)
%43 = call i32 @tms_get_int(i32* %17, i32* %4, i32 2)
%44 = load i32, i32* %5
%45 = shl i32 %43, 1
%46 = add nsw i32 %44, %45
%47 = icmp sgt i32 %46, %2
br i1 %47, label %61, label %48
48: ; preds = %40
%49 = icmp sgt i32 %43, 0
br i1 %49, label %50, label %64
50: ; preds = %48
%51 = load i32, i32* %4
%52 = sext i32 %44 to i64
%53 = getelementptr i32, i32* %1, i64 %52
%54 = bitcast i32* %53 to i8*
%55 = sext i32 %51 to i64
%56 = getelementptr [512 x i32], [512 x i32]* %6, i64 0, i64 %55
%57 = bitcast i32* %56 to i8*
%58 = call i32 @llvm.smax.i32(i32 %45, i32 1)
%59 = zext i32 %58 to i64
%60 = shl nuw nsw i64 %59, 2
br label %64
61: ; preds = %40
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %46)
br label %66
64: ; preds = %50, %48
store i32 %46, i32* %5
%65 = add nsw i32 %43, %20
br label %66
66: ; preds = %64, %61
%67 = phi i32 [ %20, %61 ], [ %65, %64 ]
%68 = add nsw i32 %43, %21
%69 = shl nsw i32 %41, 1
%70 = add nsw i32 %69, -2
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 %71
%73 = load i32, i32* %72
%74 = icmp eq i32 %73, 255
br i1 %74, label %75, label %82
75: ; preds = %66
%76 = add nsw i32 %69, -1
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 %77
%79 = load i32, i32* %78
%80 = icmp eq i32 %79, 255
%81 = select i1 %80, i32 0, i32 %22
br label %82
82: ; preds = %75, %66
%83 = phi i32 [ %22, %66 ], [ %81, %75 ]
%84 = icmp sgt i32 %83, 0
%85 = icmp slt i32 %24, 9
%86 = select i1 %85, i1 %84, i1 false
%87 = load i32, i32* %5
%88 = icmp slt i32 %87, %2
%89 = select i1 %86, i1 %88, i1 false
br i1 %89, label %18, label %90
90: ; preds = %82, %35, %3
%91 = phi i32 [ 0, %3 ], [ %20, %35 ], [ %67, %82 ]
store i32 4, i32* %4
%92 = call i32 @tms_put_int(i32 %91, i32* %1, i32* %4, i32 4)
%93 = load i32, i32* %5
%94 = call i32 @tms_put_chksum(i32* %1, i32 %93)
ret i32 %94
}