#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*/ B115200;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int atoi(char *);
int encode(unsigned char *, int, unsigned char *);
int /*<<< orphan*/ exit(int);
int fileheight(char *);
int filewidth(char *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
unsigned char *read256BMPdata(char *);
unsigned char *read256BMPpallet(char *);
int /*<<< orphan*/ set_interface_attribs(int, int /*<<< orphan*/,
                                         int /*<<< orphan*/);
int /*<<< orphan*/ write(int, ...);
int main(int argc, char *argv[]) {
  if (argc != 4) {
    perror("program filename x_coor, y_coor");
    exit(1);
  }
  char *portname = "/dev/rfcomm0";
  int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    perror("Error open USB port");
    exit(1);
  }
  set_interface_attribs(fd, B115200, 0);
  unsigned char *pal_ptr = read256BMPpallet(argv[1]);
  unsigned char *pic_ptr2 = read256BMPdata(argv[1]);
  int width = filewidth(argv[1]);
  int height = fileheight(argv[1]);
  int startx = atoi(argv[2]);
  int starty = atoi(argv[3]);
  char k[8];
  k[0] = startx & 0xFF;
  k[1] = (startx >> 8) & 0xFF;
  k[2] = starty & 0xFF;
  k[3] = (starty >> 8) & 0xFF;
  k[4] = width & 0xFF;
  k[5] = (width >> 8) & 0xFF;
  k[6] = height & 0xFF;
  k[7] = (height >> 8) & 0xFF;
  write(fd, k, 8);
  unsigned char *shortpic = malloc(sizeof(char) * (width * height * 2));
  memset(shortpic, 0, (width * height * 2) * sizeof(unsigned char));
  int len = encode(pic_ptr2, width * height, shortpic);
  printf("Encode finish, begin send\n");
  write(fd, pal_ptr, 768);
  write(fd, shortpic, len);
  printf("Send finished, free memory\n");
  free(shortpic);
  free(pal_ptr);
  free(pic_ptr2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1226 <main+0x36>
lea    0xde9(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xdf3(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2d8b(%rip),%rax
or     (%rax),%esi
mov    0x2d92(%rip),%rax
or     (%rax),%esi
call   10e0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1278 <main+0x88>
lea    0xdc4(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <set_interface_attribs@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <read256BMPpallet@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <read256BMPdata@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <filewidth@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <fileheight@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x48(%rbp)
mov    -0x3c(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x47(%rbp)
mov    -0x40(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x46(%rbp)
mov    -0x40(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x45(%rbp)
mov    -0x34(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x44(%rbp)
mov    -0x34(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x43(%rbp)
mov    -0x38(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x42(%rbp)
mov    -0x38(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x41(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1060 <write@plt>
mov    -0x34(%rbp),%eax
imul   -0x38(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x34(%rbp),%eax
imul   -0x38(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
imul   -0x38(%rbp),%esi
mov    -0x50(%rbp),%rdx
call   1040 <encode@plt>
mov    %eax,-0x54(%rbp)
lea    0xc87(%rip),%rdi
call   1080 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x300,%edx
mov    $0x0,%al
call   1060 <write@plt>
mov    -0x1c(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    -0x54(%rbp),%edx
mov    $0x0,%al
call   1060 <write@plt>
lea    0xc72(%rip),%rdi
call   1080 <printf@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x4,%edi
jne    1362 <main+0x172>
mov    %rsi,%rbp
mov    0x2dd7(%rip),%rax
mov    0x2db8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dbd(%rip),%rax
or     (%rax),%esi
lea    0xdf4(%rip),%rdi
call   10e0 <open@plt>
test   %eax,%eax
js     136b <main+0x17b>
mov    %eax,%r12d
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
xor    %edx,%edx
call   1080 <set_interface_attribs@plt>
mov    0x8(%rbp),%rdi
call   10b0 <read256BMPpallet@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rbp),%rdi
call   1060 <read256BMPdata@plt>
mov    %rax,%r15
mov    0x8(%rbp),%rdi
call   10d0 <filewidth@plt>
mov    %eax,%r13d
mov    0x8(%rbp),%rdi
call   10a0 <fileheight@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   1100 <atoi@plt>
mov    %eax,%r14d
mov    0x18(%rbp),%rdi
call   1100 <atoi@plt>
mov    %r14d,%ecx
mov    %cl,0x8(%rsp)
mov    %ch,0x9(%rsp)
mov    %al,0xa(%rsp)
mov    %ah,0xb(%rsp)
mov    %r13d,%eax
mov    %al,0xc(%rsp)
mov    %ah,0xd(%rsp)
mov    %bl,0xe(%rsp)
mov    %bh,0xf(%rsp)
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
mov    $0x8,%edx
xor    %eax,%eax
call   1070 <write@plt>
imul   %r13d,%ebx
lea    (%rbx,%rbx,1),%ebp
mov    %ebp,%edi
call   10c0 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1090 <memset@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r13,%rdx
call   1040 <encode@plt>
mov    %eax,%ebx
lea    0xd3f(%rip),%rdi
call   1050 <puts@plt>
mov    %r12d,%edi
mov    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x300,%edx
xor    %eax,%eax
call   1070 <write@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <write@plt>
lea    0xd27(%rip),%rdi
call   1050 <puts@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc97(%rip),%rdi
jmp    1372 <main+0x182>
lea    0xcbb(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"program filename x_coor, y_coor\00"
@.str.1 = [13 x i8] c"/dev/rfcomm0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.2 = [20 x i8] c"Error open USB port\00"
@B115200 = global i32 0
@.str.3 = [27 x i8] c"Encode finish, begin send\0A\00"
@.str.4 = [28 x i8] c"Send finished, free memory\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [8 x i8]
%15 = alloca i8*
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 4
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %6
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NOCTTY
%26 = or i32 %24, %25
%27 = load i32, i32* @O_SYNC
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %23, i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %22
%33 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %22
%36 = load i32, i32* %7
%37 = load i32, i32* @B115200
%38 = call i32 @set_interface_attribs(i32 %36, i32 %37, i32 0)
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i8* @read256BMPpallet(i8* %41)
store i8* %42, i8** %8
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i8* @read256BMPdata(i8* %45)
store i8* %46, i8** %9
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @filewidth(i8* %49)
store i32 %50, i32* %10
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = call i32 @fileheight(i8* %53)
store i32 %54, i32* %11
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32 @atoi(i8* %57)
store i32 %58, i32* %12
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 3
%61 = load i8*, i8** %60
%62 = call i32 @atoi(i8* %61)
store i32 %62, i32* %13
%63 = load i32, i32* %12
%64 = and i32 %63, 255
%65 = trunc i32 %64 to i8
%66 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
store i8 %65, i8* %66
%67 = load i32, i32* %12
%68 = ashr i32 %67, 8
%69 = and i32 %68, 255
%70 = trunc i32 %69 to i8
%71 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 1
store i8 %70, i8* %71
%72 = load i32, i32* %13
%73 = and i32 %72, 255
%74 = trunc i32 %73 to i8
%75 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 2
store i8 %74, i8* %75
%76 = load i32, i32* %13
%77 = ashr i32 %76, 8
%78 = and i32 %77, 255
%79 = trunc i32 %78 to i8
%80 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 3
store i8 %79, i8* %80
%81 = load i32, i32* %10
%82 = and i32 %81, 255
%83 = trunc i32 %82 to i8
%84 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 4
store i8 %83, i8* %84
%85 = load i32, i32* %10
%86 = ashr i32 %85, 8
%87 = and i32 %86, 255
%88 = trunc i32 %87 to i8
%89 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 5
store i8 %88, i8* %89
%90 = load i32, i32* %11
%91 = and i32 %90, 255
%92 = trunc i32 %91 to i8
%93 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 6
store i8 %92, i8* %93
%94 = load i32, i32* %11
%95 = ashr i32 %94, 8
%96 = and i32 %95, 255
%97 = trunc i32 %96 to i8
%98 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 7
store i8 %97, i8* %98
%99 = load i32, i32* %7
%100 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%101 = call i32 (i32, ...) @write(i32 %99, i8* %100, i32 8)
%102 = load i32, i32* %10
%103 = load i32, i32* %11
%104 = mul nsw i32 %102, %103
%105 = mul nsw i32 %104, 2
%106 = sext i32 %105 to i64
%107 = mul i64 1, %106
%108 = trunc i64 %107 to i32
%109 = call i8* @malloc(i32 %108)
store i8* %109, i8** %15
%110 = load i8*, i8** %15
%111 = load i32, i32* %10
%112 = load i32, i32* %11
%113 = mul nsw i32 %111, %112
%114 = mul nsw i32 %113, 2
%115 = sext i32 %114 to i64
%116 = mul i64 %115, 1
%117 = trunc i64 %116 to i32
%118 = call i32 @memset(i8* %110, i32 0, i32 %117)
%119 = load i8*, i8** %9
%120 = load i32, i32* %10
%121 = load i32, i32* %11
%122 = mul nsw i32 %120, %121
%123 = load i8*, i8** %15
%124 = call i32 @encode(i8* %119, i32 %122, i8* %123)
store i32 %124, i32* %16
%125 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%126 = load i32, i32* %7
%127 = load i8*, i8** %8
%128 = call i32 (i32, ...) @write(i32 %126, i8* %127, i32 768)
%129 = load i32, i32* %7
%130 = load i8*, i8** %15
%131 = load i32, i32* %16
%132 = call i32 (i32, ...) @write(i32 %129, i8* %130, i32 %131)
%133 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%134 = load i8*, i8** %15
%135 = call i32 @free(i8* %134)
%136 = load i8*, i8** %8
%137 = call i32 @free(i8* %136)
%138 = load i8*, i8** %9
%139 = call i32 @free(i8* %138)
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"program filename x_coor, y_coor\00"
@.str.1 = [13 x i8] c"/dev/rfcomm0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.2 = [20 x i8] c"Error open USB port\00"
@B115200 = global i32 0
@str = [26 x i8] c"Encode finish, begin send\00"
@str.5 = [27 x i8] c"Send finished, free memory\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8 x i8]
%4 = icmp eq i32 %0, 4
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %10, %9
%12 = load i32, i32* @O_SYNC
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %8
%20 = load i32, i32* @B115200
%21 = tail call i32 @set_interface_attribs(i32 %14, i32 %20, i32 0)
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call i8* @read256BMPpallet(i8* %23)
%25 = load i8*, i8** %22
%26 = tail call i8* @read256BMPdata(i8* %25)
%27 = load i8*, i8** %22
%28 = tail call i32 @filewidth(i8* %27)
%29 = load i8*, i8** %22
%30 = tail call i32 @fileheight(i8* %29)
%31 = getelementptr inbounds i8*, i8** %1, i64 2
%32 = load i8*, i8** %31
%33 = tail call i32 @atoi(i8* %32)
%34 = getelementptr inbounds i8*, i8** %1, i64 3
%35 = load i8*, i8** %34
%36 = tail call i32 @atoi(i8* %35)
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%38 = trunc i32 %33 to i8
store i8 %38, i8* %37
%39 = lshr i32 %33, 8
%40 = trunc i32 %39 to i8
%41 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
store i8 %40, i8* %41
%42 = trunc i32 %36 to i8
%43 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 2
store i8 %42, i8* %43
%44 = lshr i32 %36, 8
%45 = trunc i32 %44 to i8
%46 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 3
store i8 %45, i8* %46
%47 = trunc i32 %28 to i8
%48 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 4
store i8 %47, i8* %48
%49 = lshr i32 %28, 8
%50 = trunc i32 %49 to i8
%51 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 5
store i8 %50, i8* %51
%52 = trunc i32 %30 to i8
%53 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 6
store i8 %52, i8* %53
%54 = lshr i32 %30, 8
%55 = trunc i32 %54 to i8
%56 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 7
store i8 %55, i8* %56
%57 = call i32 (i32, ...) @write(i32 %14, i8* %37, i32 8)
%58 = mul nsw i32 %30, %28
%59 = shl nsw i32 %58, 1
%60 = call i8* @malloc(i32 %59)
%61 = call i32 @memset(i8* %60, i32 0, i32 %59)
%62 = call i32 @encode(i8* %26, i32 %58, i8* %60)
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%64 = call i32 (i32, ...) @write(i32 %14, i8* %24, i32 768)
%65 = call i32 (i32, ...) @write(i32 %14, i8* %60, i32 %62)
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%67 = call i32 @free(i8* %60)
%68 = call i32 @free(i8* %24)
%69 = call i32 @free(i8* %26)
ret i32 0
}