#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*/ F_SETFL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, void *, int);
int /*<<< orphan*/ sleep(int);
int write(int, unsigned char *, int);
bool change_air_state(bool state) {
  int uart_descriptor;
  uart_descriptor = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (uart_descriptor == -1) {
    perror("Erro ao abrir o arquivo /dev/ttyAMA0");
    return false;
  }
  fcntl(uart_descriptor, F_SETFL, 0);
  unsigned char buffer[2];
  unsigned char *p_buffer;
  p_buffer = &buffer[0];
  *p_buffer++ = 0xA0;
  if (state) {
    *p_buffer++ = 0x01;
  } else {
    *p_buffer++ = 0x00;
  }
  int n = write(uart_descriptor, &buffer[0], (p_buffer - &buffer[0]));
  if (n < 0) {
    close(uart_descriptor);
    perror("Erro ao escrever na uart");
    return false;
  }
  sleep(2);
  char buffer_result;
  int m = read(uart_descriptor, (void *)&buffer_result, 1);
  bool result;
  if (m <= 0) {
    perror("Erro ao ler a resposta do ar condicionado");
    result = false;
  }
  if (buffer_result != 0xA1) {
    perror("O arduino recebeu um comando desconhecido");
    result = false;
  } else {
    result = true;
  }
  close(uart_descriptor);
  return result;
}


x86 assembly (clang, -O0):
<change_air_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
mov    0x2e34(%rip),%rax
or     (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c7 <change_air_state+0x57>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e4 <change_air_state+0x174>
mov    -0xc(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
lea    -0xe(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0xa0,(%rax)
cmpl   $0x0,-0x8(%rbp)
je     1215 <change_air_state+0xa5>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x1,(%rax)
jmp    1227 <change_air_state+0xb7>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,(%rax)
mov    -0xc(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    -0x18(%rbp),%rax
lea    -0xe(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    126d <change_air_state+0xfd>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    0xdc9(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e4 <change_air_state+0x174>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    -0xc(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     12a8 <change_air_state+0x138>
lea    0xda2(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x28(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0xa1,%eax
je     12cf <change_air_state+0x15f>
lea    0xdaa(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x28(%rbp)
jmp    12d6 <change_air_state+0x166>
movl   $0x1,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_air_state>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %edi,%ebp
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11b2 <change_air_state+0x42>
lea    0xe58(%rip),%rdi
call   1080 <perror@plt>
jmp    123a <change_air_state+0xca>
mov    %eax,%ebx
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
movb   $0xa0,0x5(%rsp)
test   %ebp,%ebp
setne  0x6(%rsp)
lea    0x5(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    11fc <change_air_state+0x8c>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe30(%rip),%rdi
call   1080 <perror@plt>
jmp    123a <change_air_state+0xca>
mov    $0x2,%edi
call   1090 <sleep@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1227 <change_air_state+0xb7>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [37 x i8] c"Erro ao abrir o arquivo /dev/ttyAMA0\00"
@F_SETFL = global i32 0
@.str.2 = [25 x i8] c"Erro ao escrever na uart\00"
@.str.3 = [42 x i8] c"Erro ao ler a resposta do ar condicionado\00"
@.str.4 = [42 x i8] c"O arduino recebeu um comando desconhecido\00"
define i32 @change_air_state(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = load i32, i32* @O_NDELAY
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %71
21: ; preds = %1
%22 = load i32, i32* %4
%23 = load i32, i32* @F_SETFL
%24 = call i32 @fcntl(i32 %22, i32 %23, i32 0)
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %6
store i8 -96, i8* %26
%28 = load i32, i32* %3
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %6
store i8 1, i8* %31
br label %36
33: ; preds = %21
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
store i8 0, i8* %34
br label %36
36: ; preds = %33, %30
%37 = load i32, i32* %4
%38 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%39 = load i8*, i8** %6
%40 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = call i32 @write(i32 %37, i8* %38, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %36
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
%51 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %71
52: ; preds = %36
%53 = call i32 @sleep(i32 2)
%54 = load i32, i32* %4
%55 = call i32 @read(i32 %54, i8* %8, i32 1)
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = icmp sle i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %10
br label %60
60: ; preds = %58, %52
%61 = load i8, i8* %8
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 161
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %10
br label %67
66: ; preds = %60
store i32 1, i32* %10
br label %67
67: ; preds = %66, %64
%68 = load i32, i32* %4
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %10
store i32 %70, i32* %2
br label %71
71: ; preds = %67, %48, %19
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [37 x i8] c"Erro ao abrir o arquivo /dev/ttyAMA0\00"
@F_SETFL = global i32 0
@.str.2 = [25 x i8] c"Erro ao escrever na uart\00"
@.str.3 = [42 x i8] c"Erro ao ler a resposta do ar condicionado\00"
@.str.4 = [42 x i8] c"O arduino recebeu um comando desconhecido\00"
define i32 @change_air_state(i32 %0) {
%2 = alloca [2 x i8]
%3 = alloca i8
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_NDELAY
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %35
13: ; preds = %1
%14 = load i32, i32* @F_SETFL
%15 = tail call i32 @fcntl(i32 %9, i32 %14, i32 0)
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
store i8 -96, i8* %16
%18 = icmp ne i32 %0, 0
%19 = zext i1 %18 to i8
store i8 %19, i8* %17
%20 = call i32 @write(i32 %9, i8* %16, i32 2)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = tail call i32 @close(i32 %9)
%24 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %34
25: ; preds = %13
%26 = tail call i32 @sleep(i32 2)
%27 = call i32 @read(i32 %9, i8* %3, i32 1)
%28 = icmp slt i32 %27, 1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %25, %29
%32 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%33 = tail call i32 @close(i32 %9)
br label %34
34: ; preds = %31, %22
br label %35
35: ; preds = %34, %11
ret i32 0
}