#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
void read_ttys0_canon_mode(void) {
  char buf[32] = {0};
  int r, fd;
  fd = open("/dev/ttyS0", O_RDONLY);
  if (fd < 0) {
    perror("Open /dev/ttyS0 failed");
    return;
  }
  printf("Regular read from /dev/ttyS0 in canonical mode\n");
  r = read(fd, buf, 32);
  printf("read(%d): %s", r, buf);
  close(fd);
}


x86 assembly (clang, -O0):
<read_ttys0_canon_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11ab <read_ttys0_canon_mode+0x4b>
lea    0xe6a(%rip),%rdi
call   1080 <perror@plt>
jmp    11ea <read_ttys0_canon_mode+0x8a>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    -0x20(%rbp),%rdx
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ttys0_canon_mode>:
push   %r14
push   %rbx
sub    $0x28,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ce <read_ttys0_canon_mode+0x6e>
mov    %eax,%ebx
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x20,%edx
call   1060 <read@plt>
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe36(%rip),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r14
jmp    1080 <perror@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"Open /dev/ttyS0 failed\00"
@.str.2 = [48 x i8] c"Regular read from /dev/ttyS0 in canonical mode\0A\00"
@.str.3 = [13 x i8] c"read(%d): %s\00"
define void @read_ttys0_canon_mode() {
%1 = alloca [32 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = bitcast [32 x i8]* %1 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %21
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* %3
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%15 = call i32 @read(i32 %13, i8* %14, i32 32)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %16, i8* %17)
%19 = load i32, i32* %3
%20 = call i32 @close(i32 %19)
br label %21
21: ; preds = %11, %9
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ttyS0\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"Open /dev/ttyS0 failed\00"
@.str.3 = [13 x i8] c"read(%d): %s\00"
@str = [47 x i8] c"Regular read from /dev/ttyS0 in canonical mode\00"
define void @read_ttys0_canon_mode() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %13
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%10 = call i32 @read(i32 %4, i8* %2, i32 32)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %10, i8* %2)
%12 = call i32 @close(i32 %4)
br label %13
13: ; preds = %8, %6
ret void
}