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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ stderr;
int tms_cal_chksum(int /*<<< orphan*/ *, int);
int tms_get_int(int /*<<< orphan*/ *, int *, int);
int tms_msg_size(int /*<<< orphan*/ *, int, int *);
int32_t tms_chk_msg(uint8_t *msg, int32_t n) {
  int32_t i;
  int32_t sync;
  int32_t size;
  int32_t calsum;
  i = 0;
  sync = tms_get_int(msg, &i, 2);
  if (sync != (0xAAAA)) {
    fprintf(stderr, "# Warning: found sync %04X != %04X\n", sync, (0xAAAA));
    return (-1);
  }
  size = tms_msg_size(msg, n, &i);
  if (n != (2 * size + i + 2)) {
    fprintf(stderr, "# Warning: found size %d != expected size %d\n", size,
            (n - i - 2) / 2);
  }
  calsum = tms_cal_chksum(msg, n);
  if (calsum != 0x0000) {
    fprintf(stderr, "# Warning: checksum 0x%04X != 0x0000\n", calsum);
    return (1);
  } else {
    return (0);
  }
}


x86 assembly (clang, -O0):
<tms_chk_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x2,%edx
call   1030 <tms_get_int@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xaaaa,-0x1c(%rbp)
je     11a3 <tms_chk_msg+0x63>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe75(%rip),%rsi
mov    $0xaaaa,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1246 <tms_chk_msg+0x106>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x18(%rbp),%rdx
call   1040 <tms_msg_size@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%ecx
shl    $0x1,%ecx
add    -0x18(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
je     1200 <tms_chk_msg+0xc0>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
sub    $0x2,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%edx
mov    %eax,%ecx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <tms_cal_chksum@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     123f <tms_chk_msg+0xff>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1246 <tms_chk_msg+0x106>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tms_chk_msg>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x2,%edx
call   1030 <tms_get_int@plt>
cmp    $0xaaaa,%eax
jne    11dd <tms_chk_msg+0x9d>
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <tms_msg_size@plt>
mov    0x4(%rsp),%ecx
lea    (%rcx,%rax,2),%edx
add    $0x2,%edx
cmp    %ebp,%edx
je     11af <tms_chk_msg+0x6f>
mov    0x2e56(%rip),%rdx
mov    (%rdx),%edi
mov    %ebp,%edx
sub    %ecx,%edx
mov    %edx,%ecx
add    $0xfffffffe,%ecx
shr    $0x1f,%ecx
add    %edx,%ecx
add    $0xfffffffe,%ecx
sar    %ecx
lea    0xe7e(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <tms_cal_chksum@plt>
test   %eax,%eax
je     1202 <tms_chk_msg+0xc2>
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%edi
lea    0xe85(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    1204 <tms_chk_msg+0xc4>
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%edi
lea    0xe13(%rip),%rsi
mov    %eax,%edx
mov    $0xaaaa,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1204 <tms_chk_msg+0xc4>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"# Warning: found sync %04X != %04X\0A\00"
@.str.1 = [46 x i8] c"# Warning: found size %d != expected size %d\0A\00"
@.str.2 = [38 x i8] c"# Warning: checksum 0x%04X != 0x0000\0A\00"
define i32 @tms_chk_msg(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%10 = load i32*, i32** %4
%11 = call i32 @tms_get_int(i32* %10, i32* %6, i32 2)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp ne i32 %12, 43690
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* %7
%17 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %16, i32 43690)
store i32 -1, i32* %3
br label %49
18: ; preds = %2
%19 = load i32*, i32** %4
%20 = load i32, i32* %5
%21 = call i32 @tms_msg_size(i32* %19, i32 %20, i32* %6)
store i32 %21, i32* %8
%22 = load i32, i32* %5
%23 = load i32, i32* %8
%24 = mul nsw i32 2, %23
%25 = load i32, i32* %6
%26 = add nsw i32 %24, %25
%27 = add nsw i32 %26, 2
%28 = icmp ne i32 %22, %27
br i1 %28, label %29, label %38
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = load i32, i32* %8
%32 = load i32, i32* %5
%33 = load i32, i32* %6
%34 = sub nsw i32 %32, %33
%35 = sub nsw i32 %34, 2
%36 = sdiv i32 %35, 2
%37 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %36)
br label %38
38: ; preds = %29, %18
%39 = load i32*, i32** %4
%40 = load i32, i32* %5
%41 = call i32 @tms_cal_chksum(i32* %39, i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i32, i32* @stderr
%46 = load i32, i32* %9
%47 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %46)
store i32 1, i32* %3
br label %49
48: ; preds = %38
store i32 0, i32* %3
br label %49
49: ; preds = %48, %44, %14
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"# Warning: found sync %04X != %04X\0A\00"
@.str.1 = [46 x i8] c"# Warning: found size %d != expected size %d\0A\00"
@.str.2 = [38 x i8] c"# Warning: checksum 0x%04X != 0x0000\0A\00"
define i32 @tms_chk_msg(i32* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = call i32 @tms_get_int(i32* %0, i32* %3, i32 2)
%6 = icmp eq i32 %5, 43690
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %5, i32 43690)
br label %29
10: ; preds = %2
%11 = call i32 @tms_msg_size(i32* %0, i32 %1, i32* %3)
%12 = shl nsw i32 %11, 1
%13 = load i32, i32* %3
%14 = add i32 %13, 2
%15 = add i32 %14, %12
%16 = icmp eq i32 %15, %1
br i1 %16, label %23, label %17
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = add i32 %1, -2
%20 = sub i32 %19, %13
%21 = sdiv i32 %20, 2
%22 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %21)
br label %23
23: ; preds = %17, %10
%24 = call i32 @tms_cal_chksum(i32* %0, i32 %1)
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %29
29: ; preds = %23, %26, %7
%30 = phi i32 [ -1, %7 ], [ 1, %26 ], [ 0, %23 ]
ret i32 %30
}