#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*/ ByteToHexStr(char *, char *, int);
int InitSerial();
int /*<<< orphan*/ UART_Send(int, char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, int *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int main() {
  int u;
  u = InitSerial();
  if (u == -1) {
    fprintf(stderr, "InitSerial Error!\n");
    return -1;
  }
  int input;
  scanf("%d", &input);
  if (input < 253) {
    input &= 0xff;
    unsigned char *inputID = (char *)&input;
    char *buffer = (char *)malloc(8 * sizeof(char));
    *buffer = 0xff;
    *(buffer + 1) = 0xff;
    *(buffer + 2) = 0xfe;
    *(buffer + 3) = 0x04;
    *(buffer + 4) = 0x03;
    *(buffer + 5) = 0x03;
    *(buffer + 6) = *inputID;
    int sum;
    sum = 0xfe;
    sum += 0x04;
    sum += 0x03;
    sum += 0x03 + *inputID;
    sum = sum & 0xff;
    sum = ~sum;
    char *chsum = (char *)malloc(sizeof(char));
    memcpy(chsum, &sum, sizeof(char));
    *(buffer + 7) = *chsum;
    char todisplay[50];
    char *p_display = todisplay;
    memset(p_display, 0, 50 * sizeof(char));
    ByteToHexStr(buffer, p_display, 8 * sizeof(char));
    printf("Sending:\n %s \n", p_display);
    UART_Send(u, buffer, 8 * sizeof(char));
    free(buffer);
  } else {
    fflush(stdin);
    fprintf(stderr, "Error:inputID cannot large than 253.\n");
  }
  close(u);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10e0 <InitSerial@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1207 <main+0x47>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136f <main+0x1af>
lea    0xe05(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   10c0 <scanf@plt>
cmpl   $0xfd,-0xc(%rbp)
jge    1344 <main+0x184>
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0xc(%rbp)
lea    -0xc(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x8,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0xff,(%rax)
mov    -0x20(%rbp),%rax
movb   $0xff,0x1(%rax)
mov    -0x20(%rbp),%rax
movb   $0xfe,0x2(%rax)
mov    -0x20(%rbp),%rax
movb   $0x4,0x3(%rax)
mov    -0x20(%rbp),%rax
movb   $0x3,0x4(%rax)
mov    -0x20(%rbp),%rax
movb   $0x3,0x5(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x6(%rax)
movl   $0xfe,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
add    $0x3,%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    $0x1,%edx
call   1090 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x7(%rax)
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x78(%rbp),%rsi
mov    $0x8,%edx
call   1040 <ByteToHexStr@plt>
mov    -0x78(%rbp),%rsi
lea    0xcf6(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   10d0 <UART_Send@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1367 <main+0x1a7>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
lea    0xcc3(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
xor    %eax,%eax
call   10e0 <InitSerial@plt>
cmp    $0xffffffff,%eax
je     129f <main+0xdf>
mov    %eax,%r14d
lea    0xe31(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10c0 <scanf@plt>
mov    0x8(%rsp),%eax
cmp    $0xfc,%eax
jg     12bb <main+0xfb>
movzbl %al,%eax
mov    %eax,0x8(%rsp)
mov    $0x8,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
movl   $0x4feffff,(%rax)
movw   $0x303,0x4(%rax)
mov    0x8(%rsp),%al
mov    %al,0x6(%rbx)
add    $0x8,%al
movzbl %al,%eax
not    %eax
mov    %eax,0xc(%rsp)
mov    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbp
lea    0xc(%rsp),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
call   1090 <memcpy@plt>
mov    0x0(%rbp),%al
mov    %al,0x7(%rbx)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1040 <ByteToHexStr@plt>
lea    0xd99(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   10d0 <UART_Send@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    12de <main+0x11e>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    12e8 <main+0x128>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xd4c(%rip),%rsi
call   1080 <fprintf@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"InitSerial Error!\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [15 x i8] c"Sending:\0A %s \0A\00"
@stdin = global i32 0
@.str.3 = [38 x i8] c"Error:inputID cannot large than 253.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [50 x i8]
%9 = alloca i8*
store i32 0, i32* %1
%10 = call i32 (...) @InitSerial()
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %84
16: ; preds = %0
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 253
br i1 %19, label %20, label %76
20: ; preds = %16
%21 = load i32, i32* %3
%22 = and i32 %21, 255
store i32 %22, i32* %3
%23 = bitcast i32* %3 to i8*
store i8* %23, i8** %4
%24 = call i64 @malloc(i32 8)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %5
%26 = load i8*, i8** %5
store i8 -1, i8* %26
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i64 1
store i8 -1, i8* %28
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 2
store i8 -2, i8* %30
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i64 3
store i8 4, i8* %32
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 4
store i8 3, i8* %34
%35 = load i8*, i8** %5
%36 = getelementptr inbounds i8, i8* %35, i64 5
store i8 3, i8* %36
%37 = load i8*, i8** %4
%38 = load i8, i8* %37
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 6
store i8 %38, i8* %40
store i32 254, i32* %6
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 4
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 3
store i32 %44, i32* %6
%45 = load i8*, i8** %4
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = add nsw i32 3, %47
%49 = load i32, i32* %6
%50 = add nsw i32 %49, %48
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = and i32 %51, 255
store i32 %52, i32* %6
%53 = load i32, i32* %6
%54 = xor i32 %53, -1
store i32 %54, i32* %6
%55 = call i64 @malloc(i32 1)
%56 = inttoptr i64 %55 to i8*
store i8* %56, i8** %7
%57 = load i8*, i8** %7
%58 = call i32 @memcpy(i8* %57, i32* %6, i32 1)
%59 = load i8*, i8** %7
%60 = load i8, i8* %59
%61 = load i8*, i8** %5
%62 = getelementptr inbounds i8, i8* %61, i64 7
store i8 %60, i8* %62
%63 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
store i8* %63, i8** %9
%64 = load i8*, i8** %9
%65 = call i32 @memset(i8* %64, i32 0, i32 50)
%66 = load i8*, i8** %5
%67 = load i8*, i8** %9
%68 = call i32 @ByteToHexStr(i8* %66, i8* %67, i32 8)
%69 = load i8*, i8** %9
%70 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %69)
%71 = load i32, i32* %2
%72 = load i8*, i8** %5
%73 = call i32 @UART_Send(i32 %71, i8* %72, i32 8)
%74 = load i8*, i8** %5
%75 = call i32 @free(i8* %74)
br label %81
76: ; preds = %16
%77 = load i32, i32* @stdin
%78 = call i32 @fflush(i32 %77)
%79 = load i32, i32* @stderr
%80 = call i32 @fprintf(i32 %79, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %81
81: ; preds = %76, %20
%82 = load i32, i32* %2
%83 = call i32 @close(i32 %82)
br label %84
84: ; preds = %81, %13
%85 = load i32, i32* %1
ret i32 %85
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"InitSerial Error!\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [15 x i8] c"Sending:\0A %s \0A\00"
@stdin = global i32 0
@.str.3 = [38 x i8] c"Error:inputID cannot large than 253.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [50 x i8]
%4 = tail call i32 (...) @InitSerial()
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %47
9: ; preds = %0
%10 = bitcast i32* %1 to i8*
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%12 = load i32, i32* %1
%13 = icmp slt i32 %12, 253
br i1 %13, label %14, label %40
14: ; preds = %9
%15 = and i32 %12, 255
store i32 %15, i32* %1
%16 = call i64 @malloc(i32 8)
%17 = inttoptr i64 %16 to i8*
store i8 -1, i8* %17
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8 -1, i8* %18
%19 = getelementptr inbounds i8, i8* %17, i64 2
store i8 -2, i8* %19
%20 = getelementptr inbounds i8, i8* %17, i64 3
store i8 4, i8* %20
%21 = getelementptr inbounds i8, i8* %17, i64 4
store i8 3, i8* %21
%22 = getelementptr inbounds i8, i8* %17, i64 5
store i8 3, i8* %22
%23 = load i8, i8* %10
%24 = getelementptr inbounds i8, i8* %17, i64 6
store i8 %23, i8* %24
%25 = bitcast i32* %2 to i8*
%26 = add i8 %23, 8
%27 = zext i8 %26 to i32
%28 = xor i32 %27, -1
store i32 %28, i32* %2
%29 = call i64 @malloc(i32 1)
%30 = inttoptr i64 %29 to i8*
%31 = call i32 @memcpy(i8* %30, i32* %2, i32 1)
%32 = load i8, i8* %30
%33 = getelementptr inbounds i8, i8* %17, i64 7
store i8 %32, i8* %33
%34 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%35 = call i32 @memset(i8* %34, i32 0, i32 50)
%36 = call i32 @ByteToHexStr(i8* %17, i8* %34, i32 8)
%37 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%38 = call i32 @UART_Send(i32 %4, i8* %17, i32 8)
%39 = call i32 @free(i8* %17)
br label %45
40: ; preds = %9
%41 = load i32, i32* @stdin
%42 = call i32 @fflush(i32 %41)
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %40, %14
%46 = call i32 @close(i32 %4)
br label %47
47: ; preds = %45, %6
%48 = phi i32 [ -1, %6 ], [ 0, %45 ]
ret i32 %48
}