#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*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int open_port();
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  char buffer[1024];
  char *bufptr;
  char sendBuf[255];
  int bufLen;
  int totalChars = 0;
  long int count = 0;
  int nbytes;
  int ttyFd;
  int n;
  bufptr = buffer;
  if (argc == 2) {
    snprintf(sendBuf, sizeof sendBuf, "%s\r", *++argv);
  }
  bufLen = strlen(sendBuf);
  ttyFd = open_port();
  if (ttyFd == -1)
    return -1;
  nbytes = 1;
  while (nbytes != -1) {
    nbytes = read(ttyFd, bufptr, buffer + sizeof(buffer) - bufptr - 1);
  }
  n = write(ttyFd, sendBuf, bufLen);
  if (n < 0)
    fputs("write() of 4 bytes failed!\n", stderr);
  while (count < 1000) {
    nbytes = read(ttyFd, bufptr, buffer + sizeof(buffer) - bufptr - 1);
    if (nbytes != -1) {
      totalChars += nbytes;
      bufptr += nbytes;
      if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
        count = 10000000;
    } else {
      count += 1;
      usleep(1000);
    }
  }
  *bufptr = '\0';
  printf("%s", buffer);
  close(ttyFd);
  if (totalChars == 0)
    return -1;
  return totalChars;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x540,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x528(%rbp)
movq   $0x0,-0x530(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1201 <main+0x71>
lea    -0x520(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rcx
mov    $0xff,%esi
lea    0xe04(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x520(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x524(%rbp)
mov    $0x0,%al
call   10a0 <open_port@plt>
mov    %eax,-0x538(%rbp)
cmpl   $0xffffffff,-0x538(%rbp)
jne    1239 <main+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f1 <main+0x261>
movl   $0x1,-0x534(%rbp)
cmpl   $0xffffffff,-0x534(%rbp)
je     128a <main+0xfa>
mov    -0x538(%rbp),%edi
mov    -0x418(%rbp),%rsi
lea    -0x410(%rbp),%rax
add    $0x400,%rax
mov    -0x418(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1090 <read@plt>
mov    %eax,-0x534(%rbp)
jmp    1243 <main+0xb3>
mov    -0x538(%rbp),%edi
lea    -0x520(%rbp),%rsi
mov    -0x524(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x53c(%rbp)
cmpl   $0x0,-0x53c(%rbp)
jge    12ca <main+0x13a>
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
lea    0xd3f(%rip),%rdi
call   1070 <fputs@plt>
jmp    12cf <main+0x13f>
cmpq   $0x3e8,-0x530(%rbp)
jge    13a7 <main+0x217>
mov    -0x538(%rbp),%edi
mov    -0x418(%rbp),%rsi
lea    -0x410(%rbp),%rax
add    $0x400,%rax
mov    -0x418(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1090 <read@plt>
mov    %eax,-0x534(%rbp)
cmpl   $0xffffffff,-0x534(%rbp)
je     1386 <main+0x1f6>
mov    -0x534(%rbp),%eax
add    -0x528(%rbp),%eax
mov    %eax,-0x528(%rbp)
mov    -0x534(%rbp),%ecx
mov    -0x418(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
je     1376 <main+0x1e6>
mov    -0x418(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xd,%eax
jne    1381 <main+0x1f1>
movq   $0x989680,-0x530(%rbp)
jmp    13a2 <main+0x212>
mov    -0x530(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x530(%rbp)
mov    $0x3e8,%edi
call   10b0 <usleep@plt>
jmp    12cf <main+0x13f>
mov    -0x418(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x410(%rbp),%rsi
lea    0xc61(%rip),%rdi
call   1050 <printf@plt>
mov    -0x538(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0x528(%rbp)
jne    13e8 <main+0x258>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f1 <main+0x261>
mov    -0x528(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x540,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x500,%rsp
cmp    $0x2,%edi
jne    11bc <main+0x2c>
mov    0x8(%rsi),%rcx
lea    0xe51(%rip),%rdx
mov    %rsp,%rdi
mov    $0xff,%esi
call   1060 <snprintf@plt>
mov    %rsp,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10a0 <open_port@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x124>
mov    %eax,%r15d
lea    0x100(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x60>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    %r14d,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    122c <main+0x9c>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
lea    0xddd(%rip),%rdi
call   1070 <fputs@plt>
xor    %r14d,%r14d
lea    0x500(%rsp),%r12
xor    %ebp,%ebp
jmp    125d <main+0xcd>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
mov    $0x3e8,%edi
call   10b0 <usleep@plt>
mov    %rbx,%rax
mov    %rax,%rbx
cmp    $0x3e8,%r14
jge    128c <main+0xfc>
mov    %ebx,%edx
not    %edx
add    %r12d,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1090 <read@plt>
cmp    $0xffffffff,%eax
je     1240 <main+0xb0>
add    %eax,%ebp
movslq %eax,%rcx
lea    (%rbx,%rcx,1),%rax
movzbl -0x1(%rbx,%rcx,1),%ecx
cmp    $0xa,%cl
je     128c <main+0xfc>
cmp    $0xd,%cl
jne    1251 <main+0xc1>
movb   $0x0,(%rax)
lea    0xd8a(%rip),%rdi
lea    0x100(%rsp),%rsi
call   1050 <printf@plt>
mov    %r15d,%edi
call   1080 <close@plt>
cmp    $0x1,%ebp
sbb    %eax,%eax
or     %ebp,%eax
jmp    12b9 <main+0x129>
mov    $0xffffffff,%eax
add    $0x500,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0D\00"
@.str.1 = [28 x i8] c"write() of 4 bytes failed!\0A\00"
@stderr = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca [255 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
store i64 0, i64* %11
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8* %15, i8** %7
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 2
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i32 1
store i8** %21, i8*** %5
%22 = load i8*, i8** %21
%23 = call i32 @snprintf(i8* %19, i32 255, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %18, %2
%25 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
store i32 %26, i32* %9
%27 = call i32 (...) @open_port()
store i32 %27, i32* %13
%28 = load i32, i32* %13
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %3
br label %112
31: ; preds = %24
store i32 1, i32* %12
br label %32
32: ; preds = %35, %31
%33 = load i32, i32* %12
%34 = icmp ne i32 %33, -1
br i1 %34, label %35, label %47
35: ; preds = %32
%36 = load i32, i32* %13
%37 = load i8*, i8** %7
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%39 = getelementptr inbounds i8, i8* %38, i64 1024
%40 = load i8*, i8** %7
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = sub nsw i64 %43, 1
%45 = trunc i64 %44 to i32
%46 = call i32 @read(i32 %36, i8* %37, i32 %45)
store i32 %46, i32* %12
br label %32
47: ; preds = %32
%48 = load i32, i32* %13
%49 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %14
%52 = load i32, i32* %14
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = load i32, i32* @stderr
%56 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %54, %47
br label %58
58: ; preds = %100, %57
%59 = load i64, i64* %11
%60 = icmp slt i64 %59, 1000
br i1 %60, label %61, label %101
61: ; preds = %58
%62 = load i32, i32* %13
%63 = load i8*, i8** %7
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%65 = getelementptr inbounds i8, i8* %64, i64 1024
%66 = load i8*, i8** %7
%67 = ptrtoint i8* %65 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
%70 = sub nsw i64 %69, 1
%71 = trunc i64 %70 to i32
%72 = call i32 @read(i32 %62, i8* %63, i32 %71)
store i32 %72, i32* %12
%73 = load i32, i32* %12
%74 = icmp ne i32 %73, -1
br i1 %74, label %75, label %96
75: ; preds = %61
%76 = load i32, i32* %12
%77 = load i32, i32* %10
%78 = add nsw i32 %77, %76
store i32 %78, i32* %10
%79 = load i32, i32* %12
%80 = load i8*, i8** %7
%81 = sext i32 %79 to i64
%82 = getelementptr inbounds i8, i8* %80, i64 %81
store i8* %82, i8** %7
%83 = load i8*, i8** %7
%84 = getelementptr inbounds i8, i8* %83, i64 -1
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 10
br i1 %87, label %94, label %88
88: ; preds = %75
%89 = load i8*, i8** %7
%90 = getelementptr inbounds i8, i8* %89, i64 -1
%91 = load i8, i8* %90
%92 = sext i8 %91 to i32
%93 = icmp eq i32 %92, 13
br i1 %93, label %94, label %95
94: ; preds = %88, %75
store i64 10000000, i64* %11
br label %95
95: ; preds = %94, %88
br label %100
96: ; preds = %61
%97 = load i64, i64* %11
%98 = add nsw i64 %97, 1
store i64 %98, i64* %11
%99 = call i32 @usleep(i32 1000)
br label %100
100: ; preds = %96, %95
br label %58
101: ; preds = %58
%102 = load i8*, i8** %7
store i8 0, i8* %102
%103 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%104 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %103)
%105 = load i32, i32* %13
%106 = call i32 @close(i32 %105)
%107 = load i32, i32* %10
%108 = icmp eq i32 %107, 0
br i1 %108, label %109, label %110
109: ; preds = %101
store i32 -1, i32* %3
br label %112
110: ; preds = %101
%111 = load i32, i32* %10
store i32 %111, i32* %3
br label %112
112: ; preds = %110, %109, %30
%113 = load i32, i32* %3
ret i32 %113
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0D\00"
@.str.1 = [28 x i8] c"write() of 4 bytes failed!\0A\00"
@stderr = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [255 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %8, %2
%13 = call i32 @strlen(i8* %6)
%14 = call i32 (...) @open_port()
%15 = icmp eq i32 %14, -1
br i1 %15, label %59, label %16
16: ; preds = %12, %16
%17 = call i32 @read(i32 %14, i8* %5, i32 1023)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %16
19: ; preds = %16
%20 = call i32 @write(i32 %14, i8* %6, i32 %13)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = call i32 @fputs(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %19
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1024
%27 = ptrtoint i8* %26 to i64
br label %28
28: ; preds = %25, %47
%29 = phi i64 [ 0, %25 ], [ %50, %47 ]
%30 = phi i32 [ 0, %25 ], [ %49, %47 ]
%31 = phi i8* [ %5, %25 ], [ %48, %47 ]
%32 = ptrtoint i8* %31 to i64
%33 = xor i64 %32, -1
%34 = add i64 %33, %27
%35 = trunc i64 %34 to i32
%36 = call i32 @read(i32 %14, i8* %31, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %44, label %38
38: ; preds = %28
%39 = add nsw i32 %36, %30
%40 = sext i32 %36 to i64
%41 = getelementptr inbounds i8, i8* %31, i64 %40
%42 = getelementptr inbounds i8, i8* %41, i64 -1
%43 = load i8, i8* %42
switch i8 %43, label %47 [
i8 10, label %52
i8 13, label %52
]
44: ; preds = %28
%45 = add nsw i64 %29, 1
%46 = call i32 @usleep(i32 1000)
br label %47
47: ; preds = %38, %44
%48 = phi i8* [ %31, %44 ], [ %41, %38 ]
%49 = phi i32 [ %30, %44 ], [ %39, %38 ]
%50 = phi i64 [ %45, %44 ], [ %29, %38 ]
%51 = icmp slt i64 %50, 1000
br i1 %51, label %28, label %52
52: ; preds = %38, %38, %47
%53 = phi i32 [ %49, %47 ], [ %39, %38 ], [ %39, %38 ]
%54 = phi i8* [ %48, %47 ], [ %41, %38 ], [ %41, %38 ]
store i8 0, i8* %54
%55 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%56 = call i32 @close(i32 %14)
%57 = icmp eq i32 %53, 0
%58 = select i1 %57, i32 -1, i32 %53
br label %59
59: ; preds = %52, %12
%60 = phi i32 [ -1, %12 ], [ %58, %52 ]
ret i32 %60
}