#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 timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, unsigned char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ setup_port(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int port;
  if (argc != 2) {
    printf("%s commport\n", argv[0]);
    return -1;
  }
  port = open(argv[1], O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (port < 0) {
    printf("Port could not be opened.\n");
    return -3;
  }
  setup_port(port);
  write(port, "hi", 2);
  while (1) {
    fd_set monitored_files;
    struct timeval timeout;
    FD_ZERO(&monitored_files);
    FD_SET(port, &monitored_files);
    timeout.tv_sec = 0;
    timeout.tv_usec = 290;
    if (select(port + 1, &monitored_files, NULL, NULL, &timeout) <= 0) {
      continue;
    }
    printf("Got something 1!\n");
    if (FD_ISSET(port, &monitored_files)) {
      unsigned char inch;
      printf("Performing read.\n");
      read(port, &inch, 1);
      printf("Got %02x\n", inch);
    }
  }
  close(port);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <main+0x153>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
or     (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1220 <main+0x90>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12e3 <main+0x153>
mov    -0x14(%rbp),%edi
call   1090 <setup_port@plt>
mov    -0x14(%rbp),%edi
lea    0xdf6(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
lea    -0x18(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <FD_SET@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x122,-0x28(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
cmp    $0x0,%rax
jg     1289 <main+0xf9>
jmp    123c <main+0xac>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12de <main+0x14e>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movzbl -0x29(%rbp),%esi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    123c <main+0xac>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    129f <main+0xff>
mov    0x2e2a(%rip),%rcx
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x8(%rsi),%rdi
mov    0x2e0c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   10b0 <open@plt>
test   %eax,%eax
js     12b7 <main+0x117>
mov    %eax,%ebx
mov    %eax,%edi
call   10a0 <setup_port@plt>
lea    0xe18(%rip),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %ebx,%r14d
add    $0x1,%r14d
lea    0x4(%rsp),%rbp
lea    0x8(%rsp),%r15
lea    0xe01(%rip),%r12
lea    0xdf0(%rip),%r13
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
movl   $0x122,0x8(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1090 <select@plt>
test   %rax,%rax
jle    1220 <main+0x80>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1220 <main+0x80>
lea    0xdb3(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movzbl 0x3(%rsp),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1220 <main+0x80>
mov    (%rsi),%rsi
lea    0xd57(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
jmp    12c8 <main+0x128>
lea    0xd7e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [13 x i8] c"%s commport\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [27 x i8] c"Port could not be opened.\0A\00"
@.str.2 = [3 x i8] c"hi\00"
@.str.3 = [18 x i8] c"Got something 1!\0A\00"
@.str.4 = [18 x i8] c"Performing read.\0A\00"
@.str.5 = [10 x i8] c"Got %02x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %60
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_NOCTTY
%23 = or i32 %21, %22
%24 = load i32, i32* @O_NONBLOCK
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %20, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -3, i32* %3
br label %60
31: ; preds = %17
%32 = load i32, i32* %6
%33 = call i32 @setup_port(i32 %32)
%34 = load i32, i32* %6
%35 = call i32 @write(i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %36
36: ; preds = %31, %46, %59
%37 = call i32 @FD_ZERO(i32* %7)
%38 = load i32, i32* %6
%39 = call i32 @FD_SET(i32 %38, i32* %7)
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %40
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 290, i32* %41
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
%44 = call i64 @select(i32 %43, i32* %7, i32* null, i32* null, %struct.timeval* %8)
%45 = icmp sle i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %36
br label %36
47: ; preds = %36
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = call i64 @FD_ISSET(i32 %49, i32* %7)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %47
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32, i32* %6
%55 = call i32 @read(i32 %54, i8* %9, i32 1)
%56 = load i8, i8* %9
%57 = zext i8 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %52, %47
br label %36
60: ; preds = %29, %12
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [13 x i8] c"%s commport\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [3 x i8] c"hi\00"
@.str.5 = [10 x i8] c"Got %02x\0A\00"
@str = [17 x i8] c"Got something 1!\00"
@str.6 = [17 x i8] c"Performing read.\00"
@str.7 = [26 x i8] c"Port could not be opened.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i8
%6 = icmp eq i32 %0, 2
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %46
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %14, %13
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %12, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
br label %46
22: ; preds = %10
%23 = tail call i32 @setup_port(i32 %18)
%24 = tail call i32 @write(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%25 = bitcast i32* %3 to i8*
%26 = bitcast %struct.timeval* %4 to i8*
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%29 = add nuw nsw i32 %18, 1
br label %30
30: ; preds = %45, %22
%31 = call i32 @FD_ZERO(i32* %3)
%32 = call i32 @FD_SET(i32 %18, i32* %3)
store i64 0, i64* %27
store i32 290, i32* %28
%33 = call i64 @select(i32 %29, i32* %3, i32* null, i32* null, %struct.timeval* %4)
%34 = icmp slt i64 %33, 1
br i1 %34, label %45, label %35
35: ; preds = %30
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%37 = call i64 @FD_ISSET(i32 %18, i32* %3)
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
%41 = call i32 @read(i32 %18, i8* %5, i32 1)
%42 = load i8, i8* %5
%43 = zext i8 %42 to i32
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %35, %39, %30
br label %30
46: ; preds = %20, %7
%47 = phi i32 [ -1, %7 ], [ -3, %20 ]
ret i32 %47
}