#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, unsigned char);
int read(int, char *, int);
int serialport_init(char *);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  int fd = serialport_init("/dev/ttyACM0");
  if (fd < 0) {
    return -1;
  }
  char buffer[256];
  int num = 0, len = 0;
  for (;;) {
    if (len == -1) {
      perror("Could not execute write");
    }
    usleep(1E3 * 100);
    num = read(fd, buffer, 256);
    if (num == -1) {
      usleep(1E3 * 10);
      continue;
    }
    printf("read %d: %d\n", num, (unsigned char)buffer[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe90(%rip),%rdi
call   1030 <serialport_init@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1190 <main+0x40>
mov    $0xffffffff,%eax
add    $0x130,%rsp
pop    %rbp
ret
movl   $0x0,-0x124(%rbp)
movl   $0x0,-0x128(%rbp)
cmpl   $0xffffffff,-0x128(%rbp)
jne    11bd <main+0x6d>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0x186a0,%edi
call   1070 <usleep@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    11fd <main+0xad>
mov    $0x2710,%edi
call   1070 <usleep@plt>
jmp    11a4 <main+0x54>
mov    -0x124(%rbp),%esi
lea    0xe1b(%rip),%rdi
movzbl -0x120(%rbp),%edx
call   1040 <printf@plt>
jmp    11a4 <main+0x54>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
lea    0xead(%rip),%rdi
call   1030 <serialport_init@plt>
test   %eax,%eax
js     11a8 <main+0x68>
mov    %eax,%ebx
mov    %rsp,%r14
lea    0xea5(%rip),%r15
jmp    117a <main+0x3a>
nopw   0x0(%rax,%rax,1)
mov    $0x2710,%edi
call   1060 <usleep@plt>
mov    $0x186a0,%edi
call   1060 <usleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1170 <main+0x30>
movzbl (%rsp),%edx
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
jmp    117a <main+0x3a>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyACM0\00"
@.str.1 = [24 x i8] c"Could not execute write\00"
@.str.2 = [13 x i8] c"read %d: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @serialport_init(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
ret i32 -1
14: ; preds = %2
store i32 0, i32* %8
store i32 0, i32* %9
br label %15
15: ; preds = %29, %27, %14
%16 = load i32, i32* %9
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = call i32 @usleep(i32 100000)
%22 = load i32, i32* %6
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 256)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @usleep(i32 10000)
br label %15
29: ; preds = %20
%30 = load i32, i32* %8
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%32 = load i8, i8* %31
%33 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %30, i8 zeroext %32)
br label %15
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyACM0\00"
@.str.2 = [13 x i8] c"read %d: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = tail call i32 @serialport_init(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %2
ret i32 -1
7: ; preds = %2
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
br label %9
9: ; preds = %15, %7
%10 = tail call i32 @usleep(i32 100000)
%11 = call i32 @read(i32 %4, i8* %8, i32 256)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @usleep(i32 10000)
br label %15
15: ; preds = %13, %16
br label %9
16: ; preds = %9
%17 = load i8, i8* %8
%18 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %11, i8 zeroext %17)
br label %15
}