#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 /*<<< orphan*/ memcpy(float *, char *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, void *, int);
int /*<<< orphan*/ sleep(int);
int write(int, unsigned char *, int);
float get_temp_uart() {
  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 -273.15;
  }
  fcntl(uart_descriptor, F_SETFL, 0);
  unsigned char buffer[3];
  unsigned char *p_buffer;
  p_buffer = &buffer[0];
  *p_buffer++ = 0x05;
  int n = write(uart_descriptor, &buffer[0], (p_buffer - &buffer[0]));
  if (n < 0) {
    close(uart_descriptor);
    perror("Erro ao escrever na uart");
    return -273.15;
  }
  sleep(1);
  char buffer_temp[4];
  int m = read(uart_descriptor, (void *)buffer_temp, 1);
  float temp;
  if (buffer_temp[0] != 0xE1 && m == 1) {
    m = read(uart_descriptor, (void *)(buffer_temp + 1), 3);
    if (m < 0) {
      perror("Erro ao ler a temperatura");
      temp = -273.15;
    } else if (m == 0) {
      printf("Sem dados na porta\n");
      temp = -273.15;
    } else {
      memcpy(&temp, buffer_temp, 4);
    }
  } else {
    temp = -273.15;
  }
  close(uart_descriptor);
  return temp;
}


x86 assembly (clang, -O0):
<get_temp_uart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
lea    0xe62(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ea <get_temp_uart+0x5a>
lea    0xe31(%rip),%rdi
call   10a0 <perror@plt>
movss  0xe20(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    133d <get_temp_uart+0x1ad>
mov    -0x8(%rbp),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
lea    -0xb(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x5,(%rax)
mov    -0x8(%rbp),%edi
lea    -0xb(%rbp),%rsi
mov    -0x18(%rbp),%rax
lea    -0xb(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1263 <get_temp_uart+0xd3>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xddd(%rip),%rdi
call   10a0 <perror@plt>
movss  0xda7(%rip),%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    133d <get_temp_uart+0x1ad>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
movsbl -0x20(%rbp),%eax
cmp    $0xe1,%eax
je     131e <get_temp_uart+0x18e>
cmpl   $0x1,-0x24(%rbp)
jne    131e <get_temp_uart+0x18e>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    $0x3,%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12da <get_temp_uart+0x14a>
lea    0xd7f(%rip),%rdi
call   10a0 <perror@plt>
movss  0xd30(%rip),%xmm0
movss  %xmm0,-0x28(%rbp)
jmp    1319 <get_temp_uart+0x189>
cmpl   $0x0,-0x24(%rbp)
jne    1302 <get_temp_uart+0x172>
lea    0xd71(%rip),%rdi
call   1050 <printf@plt>
movss  0xd08(%rip),%xmm0
movss  %xmm0,-0x28(%rbp)
jmp    1314 <get_temp_uart+0x184>
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdi
mov    $0x4,%edx
call   1080 <memcpy@plt>
jmp    1319 <get_temp_uart+0x189>
jmp    132b <get_temp_uart+0x19b>
movss  0xcda(%rip),%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movss  -0x28(%rbp),%xmm0
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_temp_uart>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e4c(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
lea    0xe65(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1267 <get_temp_uart+0xd7>
mov    %eax,%ebx
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
movb   $0x5,0xd(%rsp)
lea    0xd(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <write@plt>
test   %eax,%eax
js     1270 <get_temp_uart+0xe0>
mov    $0x1,%edi
call   10b0 <sleep@plt>
lea    0x9(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    124c <get_temp_uart+0xbc>
lea    0xa(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1070 <read@plt>
test   %eax,%eax
js     128d <get_temp_uart+0xfd>
je     1240 <get_temp_uart+0xb0>
lea    0x4(%rsp),%rdi
lea    0x9(%rsp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
jmp    1254 <get_temp_uart+0xc4>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc3889333,0x4(%rsp)
c3
mov    %ebx,%edi
call   1060 <close@plt>
movss  0x4(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd96(%rip),%rdi
jmp    127e <get_temp_uart+0xee>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xdab(%rip),%rdi
call   10a0 <perror@plt>
movss  0xd75(%rip),%xmm0
jmp    1261 <get_temp_uart+0xd1>
lea    0xdae(%rip),%rdi
call   10a0 <perror@plt>
jmp    124c <get_temp_uart+0xbc>

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 = [26 x i8] c"Erro ao ler a temperatura\00"
@.str.4 = [20 x i8] c"Sem dados na porta\0A\00"
define float @get_temp_uart() {
%1 = alloca float
%2 = alloca i32
%3 = alloca [3 x i8]
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [4 x i8]
%7 = alloca i32
%8 = alloca float
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_NDELAY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store float 0xC071126660000000, float* %1
br label %77
19: ; preds = %0
%20 = load i32, i32* %2
%21 = load i32, i32* @F_SETFL
%22 = call i32 @fcntl(i32 %20, i32 %21, i32 0)
%23 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
store i8 5, i8* %24
%26 = load i32, i32* %2
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%28 = load i8*, i8** %4
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = trunc i64 %32 to i32
%34 = call i32 @write(i32 %26, i8* %27, i32 %33)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %19
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
%40 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store float 0xC071126660000000, float* %1
br label %77
41: ; preds = %19
%42 = call i32 @sleep(i32 1)
%43 = load i32, i32* %2
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 1)
store i32 %45, i32* %7
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 225
br i1 %49, label %50, label %72
50: ; preds = %41
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %72
53: ; preds = %50
%54 = load i32, i32* %2
%55 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = call i32 @read(i32 %54, i8* %56, i32 3)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %53
%61 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store float 0xC071126660000000, float* %8
br label %71
62: ; preds = %53
%63 = load i32, i32* %7
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store float 0xC071126660000000, float* %8
br label %70
67: ; preds = %62
%68 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%69 = call i32 @memcpy(float* %8, i8* %68, i32 4)
br label %70
70: ; preds = %67, %65
br label %71
71: ; preds = %70, %60
br label %73
72: ; preds = %50, %41
store float 0xC071126660000000, float* %8
br label %73
73: ; preds = %72, %71
%74 = load i32, i32* %2
%75 = call i32 @close(i32 %74)
%76 = load float, float* %8
store float %76, float* %1
br label %77
77: ; preds = %73, %37, %17
%78 = load float, float* %1
ret float %78
}

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 = [26 x i8] c"Erro ao ler a temperatura\00"
@str = [19 x i8] c"Sem dados na porta\00"
define float @get_temp_uart() {
%1 = alloca [3 x i8]
%2 = alloca [4 x i8]
%3 = alloca float
%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 = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %46
13: ; preds = %0
%14 = load i32, i32* @F_SETFL
%15 = tail call i32 @fcntl(i32 %9, i32 %14, i32 0)
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 0, i64 0
store i8 5, i8* %16
%17 = call i32 @write(i32 %9, i8* %16, i32 1)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = tail call i32 @close(i32 %9)
%21 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %44
22: ; preds = %13
%23 = tail call i32 @sleep(i32 1)
%24 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%25 = call i32 @read(i32 %9, i8* %24, i32 1)
%26 = bitcast float* %3 to i8*
%27 = icmp eq i32 %25, 1
br i1 %27, label %28, label %40
28: ; preds = %22
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
%30 = call i32 @read(i32 %9, i8* %29, i32 3)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store float 0xC071126660000000, float* %3
br label %41
34: ; preds = %28
%35 = icmp eq i32 %30, 0
br i1 %35, label %36, label %38
36: ; preds = %34
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
store float 0xC071126660000000, float* %3
br label %41
38: ; preds = %34
%39 = call i32 @memcpy(float* %3, i8* %24, i32 4)
br label %41
40: ; preds = %22
store float 0xC071126660000000, float* %3
br label %41
41: ; preds = %32, %38, %36, %40
%42 = call i32 @close(i32 %9)
%43 = load float, float* %3
br label %44
44: ; preds = %41, %19
%45 = phi float [ 0xC071126660000000, %19 ], [ %43, %41 ]
br label %46
46: ; preds = %44, %11
%47 = phi float [ 0xC071126660000000, %11 ], [ %45, %44 ]
ret float %47
}