#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 */
struct termios {
  int c_cflag;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int B9600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, void *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, char *argv[]) {
  struct termios options;
  char Rx_port[] = "/dev/ttyAMA0";
  int fd_device = open(Rx_port, O_RDONLY);
  if (fd_device == -1) {
    printf("Unable to read device");
    return 1;
  }
  tcgetattr(fd_device, &options);
  options.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  tcflush(fd_device, TCIFLUSH);
  tcsetattr(fd_device, TCSANOW, &options);
  if (fd_device != -1) {
    unsigned char Rx_buffer[256];
    int Rx_length = read(fd_device, (void *)Rx_buffer, 255);
    if (Rx_length < 0) {
      printf("No data");
    } else {
      Rx_buffer[Rx_length] = '\0';
      printf("%s\n", Rx_buffer);
    }
  }
  close(fd_device);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe70(%rip),%rax
mov    %rax,-0x3d(%rbp)
mov    0xe6e(%rip),%eax
mov    %eax,-0x35(%rbp)
mov    0xe69(%rip),%al
mov    %al,-0x31(%rbp)
lea    -0x3d(%rbp),%rdi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    11df <main+0x6f>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x160>
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    0x2dcd(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9c(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc3(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x44(%rbp),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x44(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmpl   $0xffffffff,-0x44(%rbp)
je     12c1 <main+0x151>
mov    -0x44(%rbp),%edi
lea    -0x150(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x154(%rbp)
cmpl   $0x0,-0x154(%rbp)
jge    1298 <main+0x128>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bc <main+0x14c>
movslq -0x154(%rbp),%rax
movb   $0x0,-0x150(%rbp,%rax,1)
lea    -0x150(%rbp),%rsi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c1 <main+0x151>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x138,%rsp
movabs $0x30414d41797474,%rax
mov    %rax,0x5(%rsp)
movabs $0x7974742f7665642f,%rax
74 74 79
mov    %rax,(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     125a <main+0xda>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1080 <tcgetattr@plt>
mov    0x2dff(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2db5(%rip),%rax
or     (%rax),%ecx
mov    0x2ddc(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1090 <tcsetattr@plt>
lea    0x30(%rsp),%rsi
mov    %ebx,%edi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
js     126f <main+0xef>
mov    %eax,%eax
movb   $0x0,0x30(%rsp,%rax,1)
lea    0x30(%rsp),%rdi
call   1030 <puts@plt>
jmp    127d <main+0xfd>
lea    0xdac(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    1286 <main+0x106>
lea    0xdad(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32 }
@__const.main.Rx_port = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDONLY = global i32 0
@.str = [22 x i8] c"Unable to read device\00"
@B9600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [8 x i8] c"No data\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.termios
%7 = alloca [13 x i8]
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [13 x i8]* %7 to i8*
%12 = getelementptr inbounds [13 x i8], [13 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %60
19: ; preds = %2
%20 = load i32, i32* %8
%21 = call i32 @tcgetattr(i32 %20, %struct.termios* %6)
%22 = load i32, i32* @B9600
%23 = load i32, i32* @CS8
%24 = or i32 %22, %23
%25 = load i32, i32* @CLOCAL
%26 = or i32 %24, %25
%27 = load i32, i32* @CREAD
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @IGNPAR
%31 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 0, i64* %32
%33 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
store i64 0, i64* %33
%34 = load i32, i32* %8
%35 = load i32, i32* @TCIFLUSH
%36 = call i32 @tcflush(i32 %34, i32 %35)
%37 = load i32, i32* %8
%38 = load i32, i32* @TCSANOW
%39 = call i32 @tcsetattr(i32 %37, i32 %38, %struct.termios* %6)
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, -1
br i1 %41, label %42, label %57
42: ; preds = %19
%43 = load i32, i32* %8
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 255)
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %56
50: ; preds = %42
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 %52
store i8 0, i8* %53
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %50, %48
br label %57
57: ; preds = %56, %19
%58 = load i32, i32* %8
%59 = call i32 @close(i32 %58)
store i32 0, i32* %3
br label %60
60: ; preds = %57, %17
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
@__const.main.Rx_port = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDONLY = global i32 0
@.str = [22 x i8] c"Unable to read device\00"
@B9600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [8 x i8] c"No data\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca [13 x i8]
%5 = alloca [256 x i8]
%6 = bitcast %struct.termios* %3 to i8*
%7 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %42
13: ; preds = %2
%14 = call i32 @tcgetattr(i32 %9, %struct.termios* %3)
%15 = load i32, i32* @B9600
%16 = load i32, i32* @CS8
%17 = or i32 %16, %15
%18 = load i32, i32* @CLOCAL
%19 = or i32 %17, %18
%20 = load i32, i32* @CREAD
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @IGNPAR
%24 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%26 = bitcast i64* %25 to i8*
%27 = load i32, i32* @TCIFLUSH
%28 = call i32 @tcflush(i32 %9, i32 %27)
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %9, i32 %29, %struct.termios* %3)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%32 = call i32 @read(i32 %9, i8* %31, i32 255)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %13
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %40
36: ; preds = %13
%37 = zext i32 %32 to i64
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %37
store i8 0, i8* %38
%39 = call i32 @puts(i8* %31)
br label %40
40: ; preds = %36, %34
%41 = call i32 @close(i32 %9)
br label %42
42: ; preds = %40, %11
%43 = phi i32 [ 1, %11 ], [ 0, %40 ]
ret i32 %43
}