#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*/ delay(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ serialDataAvail(int);
char serialGetchar(int);
int serialOpen(char *, int);
int /*<<< orphan*/ serialPuts(int, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int main() {
  int fd;
  int b = 0;
  int a = 0;
  int i = 0;
  char mac[] = "78A5048C47AF";
  char check[20] = "\0";
  if ((fd = serialOpen("/dev/ttyAMA0", 9600)) < 0) {
    fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno));
    return 1;
  }
  serialPuts(fd, "AT");
  while (serialDataAvail(fd) == 0)
    ;
  i = 0;
  while ((serialDataAvail(fd)) != 0) {
    delay(10);
    check[i] = serialGetchar(fd);
    i++;
  }
  check[i] = 0;
  printf("%s \n", check);
  if (strcmp(check, "OK+LOST") == 0) {
    check[0] = 0;
    printf("desconectado\n");
  } else {
    check[0] = 0;
    printf("ja estava desconectado\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0xe35(%rip),%rax
mov    %rax,-0x21(%rbp)
mov    0xe33(%rip),%eax
mov    %eax,-0x19(%rbp)
mov    0xe2e(%rip),%al
mov    %al,-0x15(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
lea    0xe15(%rip),%rdi
mov    $0x2580,%esi
call   1030 <serialOpen@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1246 <main+0xa6>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xde5(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130d <main+0x16d>
mov    -0x8(%rbp),%edi
lea    0xdec(%rip),%rsi
call   10b0 <serialPuts@plt>
mov    -0x8(%rbp),%edi
call   10a0 <serialDataAvail@plt>
cmp    $0x0,%rax
jne    126c <main+0xcc>
jmp    1255 <main+0xb5>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   10a0 <serialDataAvail@plt>
cmp    $0x0,%rax
je     12af <main+0x10f>
mov    $0xa,%edi
call   1070 <delay@plt>
mov    -0x8(%rbp),%edi
call   1050 <serialGetchar@plt>
mov    %al,%cl
movslq -0x14(%rbp),%rax
mov    %cl,-0x40(%rbp,%rax,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1273 <main+0xd3>
movslq -0x14(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x40(%rbp),%rdi
lea    0xd6f(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12fb <main+0x15b>
movb   $0x0,-0x40(%rbp)
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    130d <main+0x16d>
movb   $0x0,-0x40(%rbp)
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movl   $0x0,0x10(%rsp)
lea    0xe42(%rip),%rdi
mov    $0x2580,%esi
call   1030 <serialOpen@plt>
test   %eax,%eax
js     1228 <main+0x88>
mov    %eax,%ebp
lea    0xe5a(%rip),%rsi
mov    %eax,%edi
call   10b0 <serialPuts@plt>
nopl   0x0(%rax)
mov    %ebp,%edi
call   10a0 <serialDataAvail@plt>
test   %rax,%rax
je     11e0 <main+0x40>
mov    %ebp,%edi
call   10a0 <serialDataAvail@plt>
test   %rax,%rax
je     1257 <main+0xb7>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1070 <delay@plt>
mov    %ebp,%edi
call   1060 <serialGetchar@plt>
mov    %al,(%rsp,%rbx,1)
add    $0x1,%rbx
mov    %ebp,%edi
call   10a0 <serialDataAvail@plt>
test   %rax,%rax
jne    1200 <main+0x60>
mov    %ebx,%eax
jmp    1259 <main+0xb9>
mov    0x2db1(%rip),%rax
mov    (%rax),%ebx
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdc7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    $0x1,%ebx
jmp    12a0 <main+0x100>
xor    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xdce(%rip),%rdi
xor    %ebx,%ebx
mov    %rsp,%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdbd(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcmp@plt>
movb   $0x0,(%rsp)
test   %rax,%rax
je     1294 <main+0xf4>
lea    0xdad(%rip),%rdi
jmp    129b <main+0xfb>
lea    0xdae(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.mac = [13 x i8] c"78A5048C47AF\00"
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"Unable to open serial device: %s\0A\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"AT\00"
@.str.3 = [5 x i8] c"%s \0A\00"
@.str.4 = [8 x i8] c"OK+LOST\00"
@.str.5 = [14 x i8] c"desconectado\0A\00"
@.str.6 = [24 x i8] c"ja estava desconectado\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [13 x i8]
%7 = alloca [20 x i8]
store i32 0, i32* %1
store i32 0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
%8 = bitcast [13 x i8]* %6 to i8*
%9 = bitcast [20 x i8]* %7 to i8*
%10 = call i32 @serialOpen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 9600)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 1, i32* %1
br label %54
17: ; preds = %0
%18 = load i32, i32* %2
%19 = call i32 @serialPuts(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %24, %17
%21 = load i32, i32* %2
%22 = call i64 @serialDataAvail(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
br label %20
25: ; preds = %20
store i32 0, i32* %5
br label %26
26: ; preds = %30, %25
%27 = load i32, i32* %2
%28 = call i64 @serialDataAvail(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = call i32 @delay(i32 10)
%32 = load i32, i32* %2
%33 = call signext i8 @serialGetchar(i32 %32)
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 %35
store i8 %33, i8* %36
%37 = load i32, i32* %5
%38 = add nsw i32 %37, 1
store i32 %38, i32* %5
br label %26
39: ; preds = %26
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 %41
store i8 0, i8* %42
%43 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%46 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %39
%49 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
store i8 0, i8* %49
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %54
51: ; preds = %39
%52 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
store i8 0, i8* %52
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %54
54: ; preds = %12, %51, %48
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"Unable to open serial device: %s\0A\00"
@errno = global i32 0
@.str.2 = [3 x i8] c"AT\00"
@.str.3 = [5 x i8] c"%s \0A\00"
@.str.4 = [8 x i8] c"OK+LOST\00"
@str = [23 x i8] c"ja estava desconectado\00"
@str.7 = [13 x i8] c"desconectado\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @serialOpen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 9600)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %38
10: ; preds = %0
%11 = tail call i32 @serialPuts(i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %12, %10
%13 = tail call i64 @serialDataAvail(i32 %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %12, label %15
15: ; preds = %12
%16 = tail call i64 @serialDataAvail(i32 %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %28, label %18
18: ; preds = %15, %18
%19 = phi i64 [ %23, %18 ], [ 0, %15 ]
%20 = tail call i32 @delay(i32 10)
%21 = tail call signext i8 @serialGetchar(i32 %3)
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 %19
store i8 %21, i8* %22
%23 = add nuw i64 %19, 1
%24 = tail call i64 @serialDataAvail(i32 %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %18
26: ; preds = %18
%27 = and i64 %23, 4294967295
br label %28
28: ; preds = %26, %15
%29 = phi i64 [ 0, %15 ], [ %27, %26 ]
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%32 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
store i8 0, i8* %2
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0))
br label %38
36: ; preds = %28
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %34, %36, %5
%39 = phi i32 [ 1, %5 ], [ 0, %36 ], [ 0, %34 ]
ret i32 %39
}