#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*/ uint8_t;
struct option {
  char *member_0;
  char member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ const member_1;
};

/* Variables and functions */
int B57600;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getopt_long(int, char **, char *, struct option *, int *);
#define no_argument 129
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, char *);
#define required_argument 128
int serialport_init(int /*<<< orphan*/, int);
int /*<<< orphan*/ serialport_read_until(int, char *, char);
int serialport_write(int, char *);
int serialport_writebyte(int, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strtol(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  int fd = 0;
  char serialport[256];
  int baudrate = B57600;
  char buf[256];
  int rc, n;
  if (argc == 1) {
    usage();
    exit(EXIT_SUCCESS);
  }
  int option_index = 0, opt;
  static struct option loptions[] = {
      {"help", no_argument, 0, 'h'},       {"port", required_argument, 0, 'p'},
      {"baud", required_argument, 0, 'b'}, {"send", required_argument, 0, 's'},
      {"receive", no_argument, 0, 'r'},    {"num", required_argument, 0, 'n'},
      {"delay", required_argument, 0, 'd'}};
  while (1) {
    opt = getopt_long(argc, argv, "hp:b:s:rn:d:", loptions, &option_index);
    if (opt == -1)
      break;
    switch (opt) {
    case '0':
      break;
    case 'd':
      n = strtol(optarg, NULL, 10);
      usleep(n * 1000);
      break;
    case 'h':
      usage();
      break;
    case 'b':
      baudrate = strtol(optarg, NULL, 10);
      break;
    case 'p':
      strcpy(serialport, optarg);
      fd = serialport_init(optarg, baudrate);
      if (fd == -1)
        return -1;
      break;
    case 'n':
      n = strtol(optarg, NULL, 10);
      rc = serialport_writebyte(fd, (uint8_t)n);
      if (rc == -1)
        return -1;
      break;
    case 's':
      strcpy(buf, optarg);
      rc = serialport_write(fd, buf);
      if (rc == -1)
        return -1;
      break;
    case 'r':
      serialport_read_until(fd, buf, '\n');
      printf("read: %s\n", buf);
      break;
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11fe <main+0x4e>
mov    $0x0,%al
call   10a0 <usage@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x0,-0x23c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xf20(%rip),%rdx
lea    0x2e63(%rip),%rcx
lea    -0x23c(%rbp),%r8
call   1060 <getopt_long@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0xffffffff,-0x240(%rbp)
jne    1241 <main+0x91>
jmp    13e1 <main+0x231>
mov    -0x240(%rbp),%eax
add    $0xffffffd0,%eax
mov    %eax,%ecx
mov    %rcx,-0x248(%rbp)
sub    $0x43,%eax
ja     13dc <main+0x22c>
mov    -0x248(%rbp),%rax
lea    0xd96(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    13dc <main+0x22c>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x238(%rbp)
imul   $0x3e8,-0x238(%rbp),%edi
call   10d0 <usleep@plt>
jmp    13dc <main+0x22c>
mov    $0x0,%al
call   10a0 <usage@plt>
jmp    13dc <main+0x22c>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x124(%rbp)
jmp    13dc <main+0x22c>
lea    -0x120(%rbp),%rdi
mov    0x2d13(%rip),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
mov    -0x124(%rbp),%esi
call   1040 <serialport_init@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1319 <main+0x169>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ef <main+0x23f>
jmp    13dc <main+0x22c>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x238(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x238(%rbp),%esi
call   10c0 <serialport_writebyte@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0xffffffff,-0x234(%rbp)
jne    1368 <main+0x1b8>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ef <main+0x23f>
jmp    13dc <main+0x22c>
lea    -0x230(%rbp),%rdi
mov    0x2c7d(%rip),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
call   1090 <serialport_write@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0xffffffff,-0x234(%rbp)
jne    13b0 <main+0x200>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ef <main+0x23f>
jmp    13dc <main+0x22c>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0xa,%edx
call   1050 <serialport_read_until@plt>
lea    -0x230(%rbp),%rsi
lea    0xd6c(%rip),%rdi
call   1070 <printf@plt>
jmp    1208 <main+0x58>
mov    0x2be8(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
cmp    $0x1,%edi
je     1350 <main+0x1a0>
mov    %rsi,%r15
mov    %edi,%r12d
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movl   $0x0,0xc(%rsp)
lea    0x2e94(%rip),%rbx
lea    0xc(%rsp),%rbp
lea    0xe08(%rip),%r14
xor    %r13d,%r13d
jmp    1225 <main+0x75>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %r13d,%edi
mov    %eax,%esi
call   10c0 <serialport_writebyte@plt>
cmp    $0xffffffff,%eax
je     1339 <main+0x189>
mov    %r12d,%edi
mov    %r15,%rsi
lea    0xe3c(%rip),%rdx
mov    %rbx,%rcx
mov    %rbp,%r8
call   1060 <getopt_long@plt>
lea    -0x62(%rax),%ecx
cmp    $0x11,%ecx
ja     1322 <main+0x172>
movslq (%r14,%rcx,4),%rax
add    %r14,%rax
jmp    *%rax
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,0x8(%rsp)
jmp    1225 <main+0x75>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
imul   $0x3e8,%eax,%edi
call   10d0 <usleep@plt>
jmp    1225 <main+0x75>
xor    %eax,%eax
call   10a0 <usage@plt>
jmp    1225 <main+0x75>
mov    0x2d59(%rip),%r13
mov    0x0(%r13),%esi
lea    0x110(%rsp),%rdi
call   1030 <strcpy@plt>
mov    0x0(%r13),%edi
mov    0x8(%rsp),%esi
call   1040 <serialport_init@plt>
mov    %eax,%r13d
jmp    121c <main+0x6c>
mov    %r13d,%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
mov    $0xa,%edx
call   1050 <serialport_read_until@plt>
lea    0xd9a(%rip),%rdi
mov    %r14,%rsi
lea    0xd15(%rip),%r14
call   1070 <printf@plt>
jmp    1225 <main+0x75>
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r13d,%edi
mov    %r14,%rsi
lea    0xce8(%rip),%r14
call   1090 <serialport_write@plt>
jmp    121c <main+0x6c>
cmp    $0xffffffff,%eax
jne    1225 <main+0x75>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0xffffffff,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   10a0 <usage@plt>
jmp    132b <main+0x17b>

LLVM IR (-O0):
%struct.option = type { i8*, i8, i32, i32 }
@B57600 = global i32 0
@EXIT_SUCCESS = global i32 0
@main.loptions = internal global [7 x %struct.option] [%struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8 -127, i32 0, i32 104 }, %struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i8 -128, i32 0, i32 112 }, %struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i8 -128, i32 0, i32 98 }, %struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0), i8 -128, i32 0, i32 115 }, %struct.option { i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i32 0, i32 0), i8 -127, i32 0, i32 114 }, %struct.option { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8 -128, i32 0, i32 110 }, %struct.option { i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i32 0, i32 0), i8 -128, i32 0, i32 100 }]
@.str = [5 x i8] c"help\00"
@.str.1 = [5 x i8] c"port\00"
@.str.2 = [5 x i8] c"baud\00"
@.str.3 = [5 x i8] c"send\00"
@.str.4 = [8 x i8] c"receive\00"
@.str.5 = [4 x i8] c"num\00"
@.str.6 = [6 x i8] c"delay\00"
@.str.7 = [13 x i8] c"hp:b:s:rn:d:\00"
@optarg = global i32 0
@.str.8 = [10 x i8] c"read: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%14 = load i32, i32* @B57600
store i32 %14, i32* %8
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = call i32 (...) @usage()
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
store i32 0, i32* %12
br label %22
22: ; preds = %21, %81
%23 = load i32, i32* %4
%24 = load i8**, i8*** %5
%25 = call i32 @getopt_long(i32 %23, i8** %24, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), %struct.option* getelementptr inbounds ([7 x %struct.option], [7 x %struct.option]* @main.loptions, i64 0, i64 0), i32* %12)
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %22
br label %82
29: ; preds = %22
%30 = load i32, i32* %13
switch i32 %30, label %81 [
i32 48, label %31
i32 100, label %32
i32 104, label %38
i32 98, label %40
i32 112, label %43
i32 110, label %54
i32 115, label %64
i32 114, label %75
]
31: ; preds = %29
br label %81
32: ; preds = %29
%33 = load i32, i32* @optarg
%34 = call i32 @strtol(i32 %33, i32* null, i32 10)
store i32 %34, i32* %11
%35 = load i32, i32* %11
%36 = mul nsw i32 %35, 1000
%37 = call i32 @usleep(i32 %36)
br label %81
38: ; preds = %29
%39 = call i32 (...) @usage()
br label %81
40: ; preds = %29
%41 = load i32, i32* @optarg
%42 = call i32 @strtol(i32 %41, i32* null, i32 10)
store i32 %42, i32* %8
br label %81
43: ; preds = %29
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%45 = load i32, i32* @optarg
%46 = call i32 @strcpy(i8* %44, i32 %45)
%47 = load i32, i32* @optarg
%48 = load i32, i32* %8
%49 = call i32 @serialport_init(i32 %47, i32 %48)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %43
store i32 -1, i32* %3
br label %85
53: ; preds = %43
br label %81
54: ; preds = %29
%55 = load i32, i32* @optarg
%56 = call i32 @strtol(i32 %55, i32* null, i32 10)
store i32 %56, i32* %11
%57 = load i32, i32* %6
%58 = load i32, i32* %11
%59 = call i32 @serialport_writebyte(i32 %57, i32 %58)
store i32 %59, i32* %10
%60 = load i32, i32* %10
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %63
62: ; preds = %54
store i32 -1, i32* %3
br label %85
63: ; preds = %54
br label %81
64: ; preds = %29
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%66 = load i32, i32* @optarg
%67 = call i32 @strcpy(i8* %65, i32 %66)
%68 = load i32, i32* %6
%69 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%70 = call i32 @serialport_write(i32 %68, i8* %69)
store i32 %70, i32* %10
%71 = load i32, i32* %10
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %74
73: ; preds = %64
store i32 -1, i32* %3
br label %85
74: ; preds = %64
br label %81
75: ; preds = %29
%76 = load i32, i32* %6
%77 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%78 = call i32 @serialport_read_until(i32 %76, i8* %77, i8 signext 10)
%79 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%80 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %29, %75, %74, %63, %53, %40, %38, %32, %31
br label %22
82: ; preds = %28
%83 = load i32, i32* @EXIT_SUCCESS
%84 = call i32 @exit(i32 %83)
unreachable
85: ; preds = %73, %62, %52
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
%struct.option = type { i8*, i8, i32, i32 }
@B57600 = global i32 0
@EXIT_SUCCESS = global i32 0
@main.loptions = internal global [7 x %struct.option] [%struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8 -127, i32 0, i32 104 }, %struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i8 -128, i32 0, i32 112 }, %struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0), i8 -128, i32 0, i32 98 }, %struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i32 0, i32 0), i8 -128, i32 0, i32 115 }, %struct.option { i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i32 0, i32 0), i8 -127, i32 0, i32 114 }, %struct.option { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8 -128, i32 0, i32 110 }, %struct.option { i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i32 0, i32 0), i8 -128, i32 0, i32 100 }]
@.str = [5 x i8] c"help\00"
@.str.1 = [5 x i8] c"port\00"
@.str.2 = [5 x i8] c"baud\00"
@.str.3 = [5 x i8] c"send\00"
@.str.4 = [8 x i8] c"receive\00"
@.str.5 = [4 x i8] c"num\00"
@.str.6 = [6 x i8] c"delay\00"
@.str.7 = [13 x i8] c"hp:b:s:rn:d:\00"
@optarg = global i32 0
@.str.8 = [10 x i8] c"read: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @B57600
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = icmp eq i32 %0, 1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = tail call i32 (...) @usage()
%12 = load i32, i32* @EXIT_SUCCESS
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = bitcast i32* %5 to i8*
store i32 0, i32* %5
br label %16
16: ; preds = %20, %14
%17 = phi i32 [ 0, %14 ], [ %21, %20 ]
%18 = phi i32 [ %7, %14 ], [ %22, %20 ]
%19 = call i32 @getopt_long(i32 %0, i8** %1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), %struct.option* getelementptr inbounds ([7 x %struct.option], [7 x %struct.option]* @main.loptions, i64 0, i64 0), i32* %5)
switch i32 %19, label %20 [
i32 -1, label %52
i32 114, label %49
i32 100, label %23
i32 104, label %28
i32 98, label %30
i32 112, label %33
i32 110, label %39
i32 115, label %44
]
20: ; preds = %16, %44, %39, %33, %49, %30, %28, %23
%21 = phi i32 [ %17, %44 ], [ %17, %39 ], [ %37, %33 ], [ %17, %30 ], [ %17, %28 ], [ %17, %23 ], [ %17, %49 ], [ %17, %16 ]
%22 = phi i32 [ %18, %44 ], [ %18, %39 ], [ %18, %33 ], [ %32, %30 ], [ %18, %28 ], [ %18, %23 ], [ %18, %49 ], [ %18, %16 ]
br label %16
23: ; preds = %16
%24 = load i32, i32* @optarg
%25 = call i32 @strtol(i32 %24, i32* null, i32 10)
%26 = mul nsw i32 %25, 1000
%27 = call i32 @usleep(i32 %26)
br label %20
28: ; preds = %16
%29 = call i32 (...) @usage()
br label %20
30: ; preds = %16
%31 = load i32, i32* @optarg
%32 = call i32 @strtol(i32 %31, i32* null, i32 10)
br label %20
33: ; preds = %16
%34 = load i32, i32* @optarg
%35 = call i32 @strcpy(i8* %6, i32 %34)
%36 = load i32, i32* @optarg
%37 = call i32 @serialport_init(i32 %36, i32 %18)
%38 = icmp eq i32 %37, -1
br i1 %38, label %55, label %20
39: ; preds = %16
%40 = load i32, i32* @optarg
%41 = call i32 @strtol(i32 %40, i32* null, i32 10)
%42 = call i32 @serialport_writebyte(i32 %17, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %55, label %20
44: ; preds = %16
%45 = load i32, i32* @optarg
%46 = call i32 @strcpy(i8* %8, i32 %45)
%47 = call i32 @serialport_write(i32 %17, i8* %8)
%48 = icmp eq i32 %47, -1
br i1 %48, label %55, label %20
49: ; preds = %16
%50 = call i32 @serialport_read_until(i32 %17, i8* %8, i8 signext 10)
%51 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* %8)
br label %20
52: ; preds = %16
%53 = load i32, i32* @EXIT_SUCCESS
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %44, %39, %33
ret i32 -1
}