#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, char *argv[]) {
  struct termios portset;
  int fd;
  FILE *f;
  char search_cmd[3] = {0xDB, 0x00, 0x7B};
  char search_buf[256];
  int count;
  fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("ERROR: open failed");
    exit(1);
  }
  tcgetattr(fd, &portset);
  cfmakeraw(&portset);
  cfsetospeed(&portset, B115200);
  cfsetispeed(&portset, B115200);
  tcsetattr(fd, TCSANOW, &portset);
  close(fd);
  f = fopen("/dev/ttyUSB0", "r+");
  count = fwrite(search_cmd, 1, 3, f);
  if (count < 1) {
    perror("ERROR: search sensor write failed");
    exit(1);
  }
  count = fread(search_buf, 1, 3 + 8 * 16 + 1, f);
  if (count < 1) {
    perror("ERROR: search sensor read failed");
    exit(1);
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe20(%rip),%ax
mov    %ax,-0x2b(%rbp)
mov    0xe18(%rip),%al
mov    %al,-0x29(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
or     (%rax),%esi
mov    0x2dc2(%rip),%rax
or     (%rax),%esi
lea    0xdf4(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1237 <main+0x77>
lea    0xde8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
lea    -0x18(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1040 <cfsetospeed@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   10b0 <cfsetispeed@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
lea    0xd6f(%rip),%rdi
lea    0xd88(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
lea    -0x2b(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   10e0 <fwrite@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x1,-0x134(%rbp)
jge    12e4 <main+0x124>
lea    0xd51(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x84,%edx
call   1030 <fread@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x1,-0x134(%rbp)
jge    1327 <main+0x167>
lea    0xd30(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
movb   $0x7b,0x6(%rsp)
mov    0x2e11(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movw   $0xdb,0x4(%rsp)
mov    0x2dd8(%rip),%rax
or     (%rax),%esi
lea    0xe0a(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12b8 <main+0xf8>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <tcgetattr@plt>
mov    %rbx,%rdi
call   1060 <cfmakeraw@plt>
mov    0x2d99(%rip),%r14
mov    (%r14),%esi
mov    %rbx,%rdi
call   1040 <cfsetospeed@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   10b0 <cfsetispeed@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1080 <tcsetattr@plt>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xda5(%rip),%rdi
lea    0xdbe(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
test   %eax,%eax
jle    12c1 <main+0x101>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x84,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    12ca <main+0x10a>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd51(%rip),%rdi
jmp    12d1 <main+0x111>
lea    0xd5e(%rip),%rdi
jmp    12d1 <main+0x111>
lea    0xd77(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@__const.main.search_cmd = [3 x i8] c"\DB\00{"
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [19 x i8] c"ERROR: open failed\00"
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [34 x i8] c"ERROR: search sensor write failed\00"
@.str.4 = [33 x i8] c"ERROR: search sensor read failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [3 x i8]
%10 = alloca [256 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [3 x i8]* %9 to i8*
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_NDELAY
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i32, i32* %7
%26 = call i32 @tcgetattr(i32 %25, %struct.termios* %6)
%27 = call i32 @cfmakeraw(%struct.termios* %6)
%28 = load i32, i32* @B115200
%29 = call i32 @cfsetospeed(%struct.termios* %6, i32 %28)
%30 = load i32, i32* @B115200
%31 = call i32 @cfsetispeed(%struct.termios* %6, i32 %30)
%32 = load i32, i32* %7
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %6)
%35 = load i32, i32* %7
%36 = call i32 @close(i32 %35)
%37 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %8
%38 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%39 = load i32*, i32** %8
%40 = call i32 @fwrite(i8* %38, i32 1, i32 3, i32* %39)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = icmp slt i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %24
%44 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %24
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%48 = load i32*, i32** %8
%49 = call i32 @fread(i8* %47, i32 1, i32 132, i32* %48)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp slt i32 %50, 1
br i1 %51, label %52, label %55
52: ; preds = %46
%53 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %46
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@__const.main.search_cmd = [3 x i8] c"\DB\00{"
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str.1 = [19 x i8] c"ERROR: open failed\00"
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [34 x i8] c"ERROR: search sensor write failed\00"
@.str.4 = [33 x i8] c"ERROR: search sensor read failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca [3 x i8]
%5 = alloca [256 x i8]
%6 = bitcast %struct.termios* %3 to i8*
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %10, %9
%12 = load i32, i32* @O_NDELAY
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 @tcgetattr(i32 %14, %struct.termios* %3)
%21 = call i32 @cfmakeraw(%struct.termios* %3)
%22 = load i32, i32* @B115200
%23 = call i32 @cfsetospeed(%struct.termios* %3, i32 %22)
%24 = load i32, i32* @B115200
%25 = call i32 @cfsetispeed(%struct.termios* %3, i32 %24)
%26 = load i32, i32* @TCSANOW
%27 = call i32 @tcsetattr(i32 %14, i32 %26, %struct.termios* %3)
%28 = call i32 @close(i32 %14)
%29 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @fwrite(i8* %7, i32 1, i32 3, i32* %29)
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %19
%36 = call i32 @fread(i8* %8, i32 1, i32 132, i32* %29)
%37 = icmp slt i32 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %35
%42 = call i32 @close(i32 %14)
ret i32 0
}