#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 /*<<< orphan*/ c_cflag;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ CLOCAL;
int /*<<< orphan*/ CREAD;
int /*<<< orphan*/ CS8;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCGETS;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TCSETS;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(void) {
  char buf[256];
  int fd;
  struct termios tty;
  struct termios old;
  int len;
  fd = open("/dev/ttyAMA0", O_RDWR);
  if (fd < 0) {
    perror("open error");
    return -1;
  }
  memset(&old, 0, sizeof(struct termios));
  ioctl(fd, TCGETS, &old);
  memset(&tty, 0, sizeof(struct termios));
  tty.c_cflag |= CREAD;
  tty.c_cflag |= CLOCAL;
  tty.c_cflag |= CS8;
  cfsetospeed(&tty, B115200);
  cfsetispeed(&tty, B115200);
  tcsetattr(fd, TCSANOW, &tty);
  ioctl(fd, TCSETS, &tty);
  while (1) {
    len = read(fd, buf, sizeof(buf));
    if (len > 0) {
      printf("buf = [%s]\n", buf);
      if (strcmp(buf, "q") == 0) {
        break;
      }
    }
  }
  ioctl(fd, TCSETS, &old);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0xe2e(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    1202 <main+0x52>
lea    0xe1c(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1378 <main+0x1c8>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x114(%rbp),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    -0x120(%rbp),%rdx
call   1060 <ioctl@plt>
lea    -0x118(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
or     -0x118(%rbp),%eax
mov    %eax,-0x118(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
or     -0x118(%rbp),%eax
mov    %eax,-0x118(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
or     -0x118(%rbp),%eax
mov    %eax,-0x118(%rbp)
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
lea    -0x118(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
lea    -0x118(%rbp),%rdi
call   10c0 <cfsetispeed@plt>
mov    -0x114(%rbp),%edi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
lea    -0x118(%rbp),%rdx
call   10a0 <tcsetattr@plt>
mov    -0x114(%rbp),%edi
mov    0x2ce4(%rip),%rax
mov    (%rax),%esi
lea    -0x118(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x114(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jle    1346 <main+0x196>
lea    -0x110(%rbp),%rsi
lea    0xcfe(%rip),%rdi
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
lea    0xcf7(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1341 <main+0x191>
jmp    134b <main+0x19b>
jmp    1346 <main+0x196>
jmp    12e2 <main+0x132>
mov    -0x114(%rbp),%edi
mov    0x2c60(%rip),%rax
mov    (%rax),%esi
lea    -0x120(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x114(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0xe31(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     12e7 <main+0x137>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1060 <ioctl@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    0x2db2(%rip),%rax
mov    (%rsp),%ecx
or     (%rax),%ecx
mov    0x2d76(%rip),%rax
or     (%rax),%ecx
mov    0x2d95(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x2d71(%rip),%rbx
mov    (%rbx),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   10c0 <cfsetispeed@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   10a0 <tcsetattr@plt>
mov    0x2d4b(%rip),%r12
mov    (%r12),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
lea    0x10(%rsp),%rbx
lea    0xd91(%rip),%r14
lea    0xd96(%rip),%r15
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1290 <main+0xe0>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1290 <main+0xe0>
mov    (%r12),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1f(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%eax
jmp    12d7 <main+0x127>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@TCGETS = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@CS8 = global i32 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@TCSETS = global i32 0
@.str.2 = [12 x i8] c"buf = [%s]\0A\00"
@.str.3 = [2 x i8] c"q\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %62
13: ; preds = %0
%14 = call i32 @memset(%struct.termios* %5, i32 0, i32 4)
%15 = load i32, i32* %3
%16 = load i32, i32* @TCGETS
%17 = call i32 @ioctl(i32 %15, i32 %16, %struct.termios* %5)
%18 = call i32 @memset(%struct.termios* %4, i32 0, i32 4)
%19 = load i32, i32* @CREAD
%20 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%21 = load i32, i32* %20
%22 = or i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @CLOCAL
%24 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @CS8
%28 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = or i32 %29, %27
store i32 %30, i32* %28
%31 = load i32, i32* @B115200
%32 = call i32 @cfsetospeed(%struct.termios* %4, i32 %31)
%33 = load i32, i32* @B115200
%34 = call i32 @cfsetispeed(%struct.termios* %4, i32 %33)
%35 = load i32, i32* %3
%36 = load i32, i32* @TCSANOW
%37 = call i32 @tcsetattr(i32 %35, i32 %36, %struct.termios* %4)
%38 = load i32, i32* %3
%39 = load i32, i32* @TCSETS
%40 = call i32 @ioctl(i32 %38, i32 %39, %struct.termios* %4)
br label %41
41: ; preds = %13, %55
%42 = load i32, i32* %3
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 256)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %55
47: ; preds = %41
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%49 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%51 = call i64 @strcmp(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %47
br label %56
54: ; preds = %47
br label %55
55: ; preds = %54, %41
br label %41
56: ; preds = %53
%57 = load i32, i32* %3
%58 = load i32, i32* @TCSETS
%59 = call i32 @ioctl(i32 %57, i32 %58, %struct.termios* %5)
%60 = load i32, i32* %3
%61 = call i32 @close(i32 %60)
store i32 0, i32* %1
br label %62
62: ; preds = %56, %11
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"open error\00"
@TCGETS = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@CS8 = global i32 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@TCSETS = global i32 0
@.str.2 = [12 x i8] c"buf = [%s]\0A\00"
@.str.3 = [2 x i8] c"q\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %3 to i8*
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %45
12: ; preds = %0
%13 = call i32 @memset(%struct.termios* %3, i32 0, i32 4)
%14 = load i32, i32* @TCGETS
%15 = call i32 @ioctl(i32 %8, i32 %14, %struct.termios* %3)
%16 = call i32 @memset(%struct.termios* %2, i32 0, i32 4)
%17 = load i32, i32* @CREAD
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = or i32 %19, %17
%21 = load i32, i32* @CLOCAL
%22 = or i32 %21, %20
%23 = load i32, i32* @CS8
%24 = or i32 %23, %22
store i32 %24, i32* %18
%25 = load i32, i32* @B115200
%26 = call i32 @cfsetospeed(%struct.termios* %2, i32 %25)
%27 = load i32, i32* @B115200
%28 = call i32 @cfsetispeed(%struct.termios* %2, i32 %27)
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %8, i32 %29, %struct.termios* %2)
%31 = load i32, i32* @TCSETS
%32 = call i32 @ioctl(i32 %8, i32 %31, %struct.termios* %2)
br label %33
33: ; preds = %40, %12
%34 = call i32 @read(i32 %8, i8* %4, i32 256)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%38 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %36, %33
br label %33
41: ; preds = %36
%42 = load i32, i32* @TCSETS
%43 = call i32 @ioctl(i32 %8, i32 %42, %struct.termios* %3)
%44 = call i32 @close(i32 %8)
br label %45
45: ; preds = %41, %10
%46 = phi i32 [ -1, %10 ], [ 0, %41 ]
ret i32 %46
}