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

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open_port();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, char *, size_t);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ system(char *);
int main() {
  char buf[8], com[256];
  size_t nbytes;
  ssize_t bytes_read;
  int fd;
  nbytes = sizeof(buf);
  printf("\nSerial app.\nType script (ex:sh /dir/to/script/):");
  fgets(com, 256, stdin);
  fd = open_port();
  while (fd != -1) {
    bytes_read = read(fd, buf, nbytes);
    fprintf(stdout, "\nOutput:%s", buf);
    if (buf[0] == '1')
      system(com);
  }
  printf("*******************"
         "\n**Exiting program**"
         "\n*******************");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x8,-0x118(%rbp)
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1060 <fgets@plt>
mov    $0x0,%al
call   1080 <open_port@plt>
mov    %eax,-0x120(%rbp)
cmpl   $0xffffffff,-0x120(%rbp)
je     1210 <main+0xb0>
mov    -0x120(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    -0x118(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x11c(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rdx
lea    0xe45(%rip),%rsi
call   1070 <fprintf@plt>
movsbl -0xc(%rbp),%eax
cmp    $0x31,%eax
jne    120b <main+0xab>
lea    -0x110(%rbp),%rdi
call   1030 <system@plt>
jmp    11b0 <main+0x50>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1060 <fgets@plt>
xor    %eax,%eax
call   1080 <open_port@plt>
cmp    $0xffffffff,%eax
je     11ef <main+0x8f>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    0x2e2b(%rip),%r12
lea    0xe7e(%rip),%r14
lea    0x10(%rsp),%r15
nopl   0x0(%rax)
mov    $0x8,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <read@plt>
mov    (%r12),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
cmpb   $0x31,0x8(%rsp)
jne    11c0 <main+0x60>
mov    %r15,%rdi
call   1030 <system@plt>
jmp    11c0 <main+0x60>
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"\0ASerial app.\0AType script (ex:sh /dir/to/script/):\00"
@stdin = global i32 0
@stdout = global i32 0
@.str.1 = [11 x i8] c"\0AOutput:%s\00"
@.str.2 = [60 x i8] c"*******************\0A**Exiting program**\0A*******************\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [8 x i8]
%3 = alloca [256 x i8]
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i64 8, i64* %4
%7 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 256, i32 %9)
%11 = call i32 (...) @open_port()
store i32 %11, i32* %6
br label %12
12: ; preds = %30, %0
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %31
15: ; preds = %12
%16 = load i32, i32* %6
%17 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%18 = load i64, i64* %4
%19 = call i32 @read(i32 %16, i8* %17, i64 %18)
store i32 %19, i32* %5
%20 = load i32, i32* @stdout
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 49
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%29 = call i32 @system(i8* %28)
br label %30
30: ; preds = %27, %15
br label %12
31: ; preds = %12
%32 = call i32 @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [50 x i8] c"\0ASerial app.\0AType script (ex:sh /dir/to/script/):\00"
@stdin = global i32 0
@stdout = global i32 0
@.str.1 = [11 x i8] c"\0AOutput:%s\00"
@.str.2 = [60 x i8] c"*******************\0A**Exiting program**\0A*******************\00"
define i32 @main() {
%1 = alloca [8 x i8]
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [8 x i8], [8 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32 @fgets(i8* %4, i32 256, i32 %6)
%8 = call i32 (...) @open_port()
%9 = icmp eq i32 %8, -1
br i1 %9, label %19, label %10
10: ; preds = %0, %18
%11 = call i32 @read(i32 %8, i8* %3, i64 8)
%12 = load i32, i32* @stdout
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%14 = load i8, i8* %3
%15 = icmp eq i8 %14, 49
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @system(i8* %4)
br label %18
18: ; preds = %16, %10
br label %10
19: ; preds = %0
%20 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}