#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 dummy;
};
typedef int /*<<< orphan*/ com_write_buf;
typedef int /*<<< orphan*/ com_read_buf;

/* Variables and functions */
int STDIN_FILENO;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ copy_from_old(int, struct termios *, struct termios *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open_com(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ send_byte_com(int, char);
scalar_t__ set_com_config(int, struct termios *);
char strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char const *argv[]) {
  int pid;
  int len;
  char cmd_byte;
  char com_write_buf[64];
  char com_read_buf[64];
  struct termios new_config, old_config;
  int sfd = open_com("/dev/ttyS1");
  memset(com_write_buf, 0, sizeof(com_write_buf));
  memset(com_read_buf, 0, sizeof(com_read_buf));
  copy_from_old(sfd, &new_config, &old_config);
  if (0 != set_com_config(sfd, &new_config)) {
    perror("com config error!");
    exit(-1);
  }
  if ((pid = fork()) < 0) {
    perror("fork function error!");
    return 0;
  }
  if (pid == 0) {
    while (1) {
      bzero(com_read_buf, sizeof(com_read_buf));
      read(sfd, com_read_buf, sizeof(com_read_buf));
      printf("%s\n", com_read_buf);
    }
  } else {
    while (1) {
      read(STDIN_FILENO, com_write_buf, sizeof(com_write_buf));
      cmd_byte = strtol(com_write_buf, NULL, 16);
      send_byte_com(sfd, cmd_byte);
    }
  }
  if (0 != set_com_config(sfd, &old_config)) {
    perror("com restore	 error!");
    exit(-1);
  }
  close(sfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe20(%rip),%rdi
call   1090 <open_com@plt>
mov    %eax,-0xb4(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
mov    -0xb4(%rbp),%edi
lea    -0xa8(%rbp),%rsi
lea    -0xb0(%rbp),%rdx
call   1080 <copy_from_old@plt>
mov    -0xb4(%rbp),%edi
lea    -0xa8(%rbp),%rsi
call   10c0 <set_com_config@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     125d <main+0x9d>
lea    0xdbd(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1287 <main+0xc7>
lea    0xda6(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret
cmpl   $0x0,-0x14(%rbp)
jne    12d6 <main+0x116>
jmp    1296 <main+0xd6>
lea    -0xa0(%rbp),%rdi
mov    $0x40,%esi
call   10b0 <bzero@plt>
mov    -0xb4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
lea    -0xa0(%rbp),%rsi
lea    0xd66(%rip),%rdi
call   1040 <printf@plt>
jmp    1296 <main+0xd6>
jmp    12db <main+0x11b>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
lea    -0x60(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %al,-0x19(%rbp)
mov    -0xb4(%rbp),%edi
movsbl -0x19(%rbp),%esi
call   1030 <send_byte_com@plt>
jmp    12db <main+0x11b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
lea    0xe2e(%rip),%rdi
call   1090 <open_com@plt>
mov    %eax,%ebp
lea    0x50(%rsp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
lea    0x8(%rsp),%rbx
mov    %rsp,%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <copy_from_old@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10c0 <set_com_config@plt>
test   %rax,%rax
jne    12c3 <main+0x103>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     12a9 <main+0xe9>
je     126b <main+0xab>
mov    0x2da8(%rip),%r14
lea    0x50(%rsp),%rbx
nopl   (%rax)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
movsbl %al,%esi
mov    %ebp,%edi
call   1030 <send_byte_com@plt>
jmp    1240 <main+0x80>
lea    0x10(%rsp),%rbx
lea    0xdbb(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x40,%esi
call   10b0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
jmp    1280 <main+0xc0>
lea    0xd6d(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd41(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [11 x i8] c"/dev/ttyS1\00"
@.str.1 = [18 x i8] c"com config error!\00"
@.str.2 = [21 x i8] c"fork function error!\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@STDIN_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca [64 x i8]
%10 = alloca [64 x i8]
%11 = alloca %struct.termios
%12 = alloca %struct.termios
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call i32 @open_com(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %13
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%16 = call i32 @memset(i8* %15, i32 0, i32 64)
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 64)
%19 = load i32, i32* %13
%20 = call i32 @copy_from_old(i32 %19, %struct.termios* %11, %struct.termios* %12)
%21 = load i32, i32* %13
%22 = call i64 @set_com_config(i32 %21, %struct.termios* %11)
%23 = icmp ne i64 0, %22
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %2
%28 = call i32 (...) @fork()
store i32 %28, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
32: ; preds = %27
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %32
br label %36
36: ; preds = %35, %36
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%38 = call i32 @bzero(i8* %37, i32 64)
%39 = load i32, i32* %13
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 64)
%42 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%43 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %36
44: ; preds = %32
br label %45
45: ; preds = %44, %45
%46 = load i32, i32* @STDIN_FILENO
%47 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 64)
%49 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%50 = call signext i8 @strtol(i8* %49, i32* null, i32 16)
store i8 %50, i8* %8
%51 = load i32, i32* %13
%52 = load i8, i8* %8
%53 = call i32 @send_byte_com(i32 %51, i8 signext %52)
br label %45
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [11 x i8] c"/dev/ttyS1\00"
@.str.1 = [18 x i8] c"com config error!\00"
@.str.2 = [21 x i8] c"fork function error!\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@STDIN_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.termios* %5 to i8*
%10 = bitcast %struct.termios* %6 to i8*
%11 = tail call i32 @open_com(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @memset(i8* %7, i32 0, i32 64)
%13 = call i32 @memset(i8* %8, i32 0, i32 64)
%14 = call i32 @copy_from_old(i32 %11, %struct.termios* %5, %struct.termios* %6)
%15 = call i64 @set_com_config(i32 %11, %struct.termios* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = call i32 (...) @fork()
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
25: ; preds = %20
%26 = icmp eq i32 %21, 0
br i1 %26, label %27, label %31
27: ; preds = %25, %27
%28 = call i32 @bzero(i8* %8, i32 64)
%29 = call i32 @read(i32 %11, i8* %8, i32 64)
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %27
31: ; preds = %25, %31
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @read(i32 %32, i8* %7, i32 64)
%34 = call signext i8 @strtol(i8* nocapture %7, i32* null, i32 16)
%35 = call i32 @send_byte_com(i32 %11, i8 signext %34)
br label %31
}