#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 B9600;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int serialport_init(char *, int);
int serialport_read_until(int, char *, char);
int serialport_write(int, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  int fd = 0;
  char serialport[256];
  int baudrate = B9600;
  char buf[20], dat[20], use[1];
  int rc, n;
  fd = serialport_init("/dev/ttyUSB0", baudrate);
  if (fd == -1)
    return -1;
  while (1) {
    printf("reading");
    strcpy(dat, "00000000:\0");
    gets(use);
    if (use[0] == 'i') {
      dat[0] = 'f';
      dat[1] = 5;
    } else if (use[0] == 'k') {
      dat[0] = 'b';
      dat[1] = 5;
    } else if (use[0] == 'j') {
      dat[2] = 'f';
      dat[3] = 5;
    } else if (use[0] == 'l') {
      dat[2] = 'b';
      dat[3] = 5;
    }
    rc = serialport_write(fd, dat);
    if (rc == -1)
      return -1;
    n = serialport_read_until(fd, buf, '\r');
    printf("wrote %d bytes, read %d bytes: %s\n", rc, n, buf);
  }
  close(fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%esi
lea    0xe64(%rip),%rdi
call   1040 <serialport_init@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ba <main+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <main+0x190>
jmp    11bf <main+0x5f>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x160(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x161(%rbp),%rdi
call   1070 <gets@plt>
movsbl -0x161(%rbp),%eax
cmp    $0x69,%eax
jne    120f <main+0xaf>
movb   $0x66,-0x160(%rbp)
movb   $0x5,-0x15f(%rbp)
jmp    1282 <main+0x122>
movsbl -0x161(%rbp),%eax
cmp    $0x6b,%eax
jne    1232 <main+0xd2>
movb   $0x62,-0x160(%rbp)
movb   $0x5,-0x15f(%rbp)
jmp    127d <main+0x11d>
movsbl -0x161(%rbp),%eax
cmp    $0x6a,%eax
jne    1255 <main+0xf5>
movb   $0x66,-0x15e(%rbp)
movb   $0x5,-0x15d(%rbp)
jmp    1278 <main+0x118>
movsbl -0x161(%rbp),%eax
cmp    $0x6c,%eax
jne    1273 <main+0x113>
movb   $0x62,-0x15e(%rbp)
movb   $0x5,-0x15d(%rbp)
jmp    1278 <main+0x118>
jmp    127d <main+0x11d>
jmp    1282 <main+0x122>
mov    -0x14(%rbp),%edi
lea    -0x160(%rbp),%rsi
call   1080 <serialport_write@plt>
mov    %eax,-0x168(%rbp)
cmpl   $0xffffffff,-0x168(%rbp)
jne    12b0 <main+0x150>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f0 <main+0x190>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xd,%edx
call   1050 <serialport_read_until@plt>
mov    %eax,-0x16c(%rbp)
mov    -0x168(%rbp),%esi
mov    -0x16c(%rbp),%edx
lea    -0x140(%rbp),%rcx
lea    0xd31(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11bf <main+0x5f>
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1040 <serialport_init@plt>
cmp    $0xffffffff,%eax
je     1269 <main+0x109>
mov    %eax,%ebx
lea    0x10(%rsp),%r12
lea    0xf(%rsp),%r13
lea    0x30(%rsp),%rbp
lea    0xe5c(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xe71(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
lea    0xe48(%rip),%rsi
call   1030 <strcpy@plt>
mov    %r13,%rdi
call   1070 <gets@plt>
movzbl 0xf(%rsp),%eax
add    $0x97,%al
cmp    $0x3,%al
ja     122d <main+0xcd>
movzbl %al,%eax
movslq (%r14,%rax,4),%rax
add    %r14,%rax
jmp    *%rax
movb   $0x66,0x10(%rsp)
lea    0x11(%rsp),%rax
jmp    122a <main+0xca>
nopl   0x0(%rax,%rax,1)
movb   $0x66,0x12(%rsp)
jmp    1225 <main+0xc5>
nopw   0x0(%rax,%rax,1)
movb   $0x62,0x10(%rsp)
lea    0x11(%rsp),%rax
jmp    122a <main+0xca>
nopl   0x0(%rax)
movb   $0x62,0x12(%rsp)
lea    0x13(%rsp),%rax
movb   $0x5,(%rax)
mov    %ebx,%edi
mov    %r12,%rsi
call   1080 <serialport_write@plt>
cmp    $0xffffffff,%eax
je     1269 <main+0x109>
mov    %eax,%r15d
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xd,%edx
call   1050 <serialport_read_until@plt>
lea    0xddb(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
mov    %rbp,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11b0 <main+0x50>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@B9600 = global i32 0
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@.str.1 = [8 x i8] c"reading\00"
@.str.2 = [11 x i8] c"00000000:\00\00"
@.str.3 = [35 x i8] c"wrote %d bytes, read %d bytes: %s\0A\00"
@EXIT_SUCCESS = global i32 0
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 [20 x i8]
%10 = alloca [20 x i8]
%11 = alloca [1 x i8]
%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* @B9600
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = call i32 @serialport_init(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %76
20: ; preds = %2
br label %21
21: ; preds = %20, %68
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%25 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%26 = call i32 @gets(i8* %25)
%27 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 105
br i1 %30, label %31, label %34
31: ; preds = %21
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
store i8 102, i8* %32
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 1
store i8 5, i8* %33
br label %61
34: ; preds = %21
%35 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 107
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
store i8 98, i8* %40
%41 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 1
store i8 5, i8* %41
br label %60
42: ; preds = %34
%43 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 106
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 2
store i8 102, i8* %48
%49 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 3
store i8 5, i8* %49
br label %59
50: ; preds = %42
%51 = getelementptr inbounds [1 x i8], [1 x i8]* %11, i64 0, i64 0
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 108
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 2
store i8 98, i8* %56
%57 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 3
store i8 5, i8* %57
br label %58
58: ; preds = %55, %50
br label %59
59: ; preds = %58, %47
br label %60
60: ; preds = %59, %39
br label %61
61: ; preds = %60, %31
%62 = load i32, i32* %6
%63 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%64 = call i32 @serialport_write(i32 %62, i8* %63)
store i32 %64, i32* %12
%65 = load i32, i32* %12
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %68
67: ; preds = %61
store i32 -1, i32* %3
br label %76
68: ; preds = %61
%69 = load i32, i32* %6
%70 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%71 = call i32 @serialport_read_until(i32 %69, i8* %70, i8 signext 13)
store i32 %71, i32* %13
%72 = load i32, i32* %12
%73 = load i32, i32* %13
%74 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %72, i32 %73, i8* %74)
br label %21
76: ; preds = %67, %19
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@B9600 = global i32 0
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@.str.1 = [8 x i8] c"reading\00"
@.str.2 = [11 x i8] c"00000000:\00\00"
@.str.3 = [35 x i8] c"wrote %d bytes, read %d bytes: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [20 x i8]
%4 = alloca [20 x i8]
%5 = alloca [1 x i8]
%6 = load i32, i32* @B9600
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%10 = tail call i32 @serialport_init(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %33, label %12
12: ; preds = %2
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 2
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 3
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 1
br label %16
16: ; preds = %12, %30
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @gets(i8* %9)
%20 = load i8, i8* %9
switch i8 %20, label %27 [
i8 105, label %21
i8 107, label %22
i8 106, label %23
i8 108, label %24
]
21: ; preds = %16
store i8 102, i8* %8
br label %25
22: ; preds = %16
store i8 98, i8* %8
br label %25
23: ; preds = %16
store i8 102, i8* %13
br label %25
24: ; preds = %16
store i8 98, i8* %13
br label %25
25: ; preds = %21, %23, %24, %22
%26 = phi i8* [ %15, %22 ], [ %14, %24 ], [ %14, %23 ], [ %15, %21 ]
store i8 5, i8* %26
br label %27
27: ; preds = %25, %16
%28 = call i32 @serialport_write(i32 %10, i8* %8)
%29 = icmp eq i32 %28, -1
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i32 @serialport_read_until(i32 %10, i8* %7, i8 signext 13)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %31, i8* %7)
br label %16
33: ; preds = %27, %2
ret i32 -1
}