#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 */
char *formatMessage(int, int, int, int, int, int, int, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int serialport_init(char *, int);
int /*<<< orphan*/ serialport_read_until(int, char *, char, int, int);
int /*<<< orphan*/ serialport_write(int, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main() {
  int fd;
  int base, shoulder, shoulder1, elbow, elbow1, wrist, wrot, grip;
  base = 500;
  shoulder = 500;
  shoulder1 = 500;
  elbow = 400;
  elbow1 = 400;
  wrist = 700;
  wrot = 800;
  grip = 1000;
  char *portname = malloc(sizeof(char) * 40);
  strcpy(portname, "/dev/tty.usbserial-A602ZALM");
  printf("copy success\n");
  fd = serialport_init(portname, 9600);
  printf("open success!\n");
  char returnval[256];
  char *teststr = formatMessage(base, shoulder, shoulder1, elbow, elbow1, wrist,
                                wrot, grip);
  strcpy(teststr, "Start:35,55,55,42,42,66,12,100:End");
  printf("copy success\n");
  serialport_write(fd, teststr);
  printf("write success!\n");
  serialport_read_until(fd, returnval, ':', 256, 10000);
  printf("read success!\n");
  printf("%s\n", returnval);
  free(portname);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x1f4,-0x8(%rbp)
movl   $0x1f4,-0xc(%rbp)
movl   $0x1f4,-0x10(%rbp)
movl   $0x190,-0x14(%rbp)
movl   $0x190,-0x18(%rbp)
movl   $0x2bc,-0x1c(%rbp)
movl   $0x320,-0x20(%rbp)
movl   $0x3e8,-0x24(%rbp)
mov    $0x28,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    $0x2580,%esi
call   1050 <serialport_init@plt>
mov    %eax,-0x4(%rbp)
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
mov    -0x20(%rbp),%r10d
mov    -0x24(%rbp),%eax
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
call   10a0 <formatMessage@plt>
mov    %rax,-0x138(%rbp)
mov    -0x138(%rbp),%rdi
lea    0xdee(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x138(%rbp),%rsi
call   1090 <serialport_write@plt>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x3a,%edx
mov    $0x100,%ecx
mov    $0x2710,%r8d
call   1060 <serialport_read_until@plt>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x130(%rbp),%rsi
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    $0x28,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
lea    0xe5f(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
lea    0xe9d(%rip),%r14
mov    %r14,%rdi
call   1070 <puts@plt>
mov    %r15,%rdi
mov    $0x2580,%esi
call   1050 <serialport_init@plt>
mov    %eax,%ebp
lea    0xe71(%rip),%rdi
call   1070 <puts@plt>
mov    $0x1f4,%edi
mov    $0x1f4,%esi
mov    $0x1f4,%edx
mov    $0x190,%ecx
mov    $0x190,%r8d
mov    $0x2bc,%r9d
push   $0x3e8
push   $0x320
call   10a0 <formatMessage@plt>
add    $0x10,%rsp
mov    %rax,%rbx
lea    0xe0c(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1070 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <serialport_write@plt>
lea    0xe29(%rip),%rdi
call   1070 <puts@plt>
mov    %rsp,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3a,%edx
mov    $0x100,%ecx
mov    $0x2710,%r8d
call   1060 <serialport_read_until@plt>
lea    0xe0f(%rip),%rdi
call   1070 <puts@plt>
mov    %rbx,%rdi
call   1070 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"/dev/tty.usbserial-A602ZALM\00"
@.str.1 = [14 x i8] c"copy success\0A\00"
@.str.2 = [15 x i8] c"open success!\0A\00"
@.str.3 = [35 x i8] c"Start:35,55,55,42,42,66,12,100:End\00"
@.str.4 = [16 x i8] c"write success!\0A\00"
@.str.5 = [15 x i8] c"read success!\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [256 x i8]
%12 = alloca i8*
store i32 500, i32* %2
store i32 500, i32* %3
store i32 500, i32* %4
store i32 400, i32* %5
store i32 400, i32* %6
store i32 700, i32* %7
store i32 800, i32* %8
store i32 1000, i32* %9
%13 = call i8* @malloc(i32 40)
store i8* %13, i8** %10
%14 = load i8*, i8** %10
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8*, i8** %10
%18 = call i32 @serialport_init(i8* %17, i32 9600)
store i32 %18, i32* %1
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* %2
%21 = load i32, i32* %3
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = load i32, i32* %7
%26 = load i32, i32* %8
%27 = load i32, i32* %9
%28 = call i8* @formatMessage(i32 %20, i32 %21, i32 %22, i32 %23, i32 %24, i32 %25, i32 %26, i32 %27)
store i8* %28, i8** %12
%29 = load i8*, i8** %12
%30 = call i32 @strcpy(i8* %29, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %1
%33 = load i8*, i8** %12
%34 = call i32 @serialport_write(i32 %32, i8* %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* %1
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%38 = call i32 @serialport_read_until(i32 %36, i8* %37, i8 signext 58, i32 256, i32 10000)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %40)
%42 = load i8*, i8** %10
%43 = call i32 @free(i8* %42)
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"/dev/tty.usbserial-A602ZALM\00"
@.str.3 = [35 x i8] c"Start:35,55,55,42,42,66,12,100:End\00"
@str.7 = [14 x i8] c"open success!\00"
@str.8 = [13 x i8] c"copy success\00"
@str.9 = [15 x i8] c"write success!\00"
@str.10 = [14 x i8] c"read success!\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = tail call dereferenceable_or_null(40) i8* @malloc(i32 40)
%3 = tail call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%5 = tail call i32 @serialport_init(i8* %2, i32 9600)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%8 = tail call i8* @formatMessage(i32 500, i32 500, i32 500, i32 400, i32 400, i32 700, i32 800, i32 1000)
%9 = tail call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%11 = tail call i32 @serialport_write(i32 %5, i8* %8)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
%13 = call i32 @serialport_read_until(i32 %5, i8* %7, i8 signext 58, i32 256, i32 10000)
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.10, i64 0, i64 0))
%15 = call i32 @puts(i8* %7)
%16 = call i32 @free(i8* %2)
ret i32 0
}