#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char **, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int /*<<< orphan*/ write_log(char *);
int request_port(char *message, int len_mes, char answer[256],
                 int file_descriptor) {
  int n, res;
  fd_set fs;
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 150000;
  int fd = file_descriptor;
  printf("len_mes = %d\n", len_mes);
  n = write(fd, message, len_mes - 1);
  if (n < 0) {
    if (1) {
      write_log("write() serial of failed!\n");
    }
  }
  FD_ZERO(&fs);
  FD_SET(fd, &fs);
  char buffer[55];
  if (select(fd + 1, &fs, NULL, NULL, &tv) < 0) {
    printf("Timeout for wait read fd");
    return (-1);
  };
  if (FD_ISSET(fd, &fs)) {
    usleep(150000);
    res = read(fd, &buffer, 56);
    printf("receiver: %d byte \n", res);
    if (res <= 0) {
      printf("read result < 0");
      return (-1);
    } else if (!res) {
      close(fd);
      FD_ZERO(&fs);
    } else {
      memset(answer, 0x00, res);
      memcpy(answer, buffer, res);
      return res;
    };
  };
}


x86 assembly (clang, -O0):
<request_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x249f0,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x44(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    122a <request_port+0x6a>
lea    0xde9(%rip),%rdi
call   10a0 <write_log@plt>
lea    -0x30(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x44(%rbp),%edi
add    $0x1,%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   10c0 <select@plt>
cmp    $0x0,%rax
jge    127d <request_port+0xbd>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <request_port+0x179>
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1339 <request_port+0x179>
mov    $0x249f0,%edi
call   10e0 <usleep@plt>
mov    -0x44(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x38,%edx
call   1080 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x2c(%rbp)
jg     12e6 <request_port+0x126>
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <request_port+0x179>
cmpl   $0x0,-0x2c(%rbp)
jne    1306 <request_port+0x146>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
lea    -0x30(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
jmp    132f <request_port+0x16f>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
lea    -0x80(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1090 <memcpy@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1339 <request_port+0x179>
jmp    1334 <request_port+0x174>
jmp    1339 <request_port+0x179>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<request_port>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
movq   $0x0,0x18(%rsp)
movl   $0x249f0,0x10(%rsp)
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1204 <request_port+0x54>
lea    0xe0f(%rip),%rdi
call   1090 <write_log@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
lea    0x1(%r15),%edi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <select@plt>
test   %rax,%rax
js     1297 <request_port+0xe7>
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12a0 <request_port+0xf0>
mov    $0x249f0,%edi
call   10d0 <usleep@plt>
lea    0x20(%rsp),%rsi
mov    %r15d,%edi
mov    $0x38,%edx
call   1070 <read@plt>
mov    %eax,%ebx
lea    0xdd5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jle    12a2 <request_port+0xf2>
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1060 <memset@plt>
lea    0x20(%rsp),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1080 <memcpy@plt>
jmp    12b5 <request_port+0x105>
lea    0xd8b(%rip),%rdi
jmp    12a9 <request_port+0xf9>
jmp    12b5 <request_port+0x105>
lea    0xdad(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [14 x i8] c"len_mes = %d\0A\00"
@.str.1 = [27 x i8] c"write() serial of failed!\0A\00"
@.str.2 = [25 x i8] c"Timeout for wait read fd\00"
@.str.3 = [20 x i8] c"receiver: %d byte \0A\00"
@.str.4 = [16 x i8] c"read result < 0\00"
define i32 @request_port(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
%15 = alloca [55 x i8]
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 150000, i32* %17
%18 = load i32, i32* %9
store i32 %18, i32* %14
%19 = load i32, i32* %7
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %14
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, 1
%25 = call i32 @write(i32 %21, i8* %22, i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %4
%29 = call i32 @write_log(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %4
%31 = call i32 @FD_ZERO(i32* %12)
%32 = load i32, i32* %14
%33 = call i32 @FD_SET(i32 %32, i32* %12)
%34 = load i32, i32* %14
%35 = add nsw i32 %34, 1
%36 = call i64 @select(i32 %35, i32* %12, i32* null, i32* null, %struct.timeval* %13)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %73
40: ; preds = %30
%41 = load i32, i32* %14
%42 = call i64 @FD_ISSET(i32 %41, i32* %12)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %73
44: ; preds = %40
%45 = call i32 @usleep(i32 150000)
%46 = load i32, i32* %14
%47 = bitcast [55 x i8]* %15 to i8**
%48 = call i32 @read(i32 %46, i8** %47, i32 56)
store i32 %48, i32* %11
%49 = load i32, i32* %11
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %11
%52 = icmp sle i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %5
br label %73
55: ; preds = %44
%56 = load i32, i32* %11
%57 = icmp ne i32 %56, 0
br i1 %57, label %62, label %58
58: ; preds = %55
%59 = load i32, i32* %14
%60 = call i32 @close(i32 %59)
%61 = call i32 @FD_ZERO(i32* %12)
br label %71
62: ; preds = %55
%63 = load i8*, i8** %8
%64 = load i32, i32* %11
%65 = call i32 @memset(i8* %63, i32 0, i32 %64)
%66 = load i8*, i8** %8
%67 = getelementptr inbounds [55 x i8], [55 x i8]* %15, i64 0, i64 0
%68 = load i32, i32* %11
%69 = call i32 @memcpy(i8* %66, i8* %67, i32 %68)
%70 = load i32, i32* %11
store i32 %70, i32* %5
br label %73
71: ; preds = %58
br label %72
72: ; preds = %71
br label %73
73: ; preds = %38, %53, %62, %72, %40
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [14 x i8] c"len_mes = %d\0A\00"
@.str.1 = [27 x i8] c"write() serial of failed!\0A\00"
@.str.2 = [25 x i8] c"Timeout for wait read fd\00"
@.str.3 = [20 x i8] c"receiver: %d byte \0A\00"
@.str.4 = [16 x i8] c"read result < 0\00"
define i32 @request_port(i8* nocapture %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca [55 x i8]
%8 = bitcast i32* %5 to i8*
%9 = bitcast %struct.timeval* %6 to i8*
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 150000, i32* %11
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %1)
%13 = add nsw i32 %1, -1
%14 = tail call i32 @write(i32 %3, i8* %0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = tail call i32 @write_log(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %4
%19 = call i32 @FD_ZERO(i32* %5)
%20 = call i32 @FD_SET(i32 %3, i32* %5)
%21 = getelementptr inbounds [55 x i8], [55 x i8]* %7, i64 0, i64 0
%22 = add nsw i32 %3, 1
%23 = call i64 @select(i32 %22, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %41
27: ; preds = %18
%28 = call i64 @FD_ISSET(i32 %3, i32* %5)
%29 = icmp eq i64 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %27
%31 = call i32 @usleep(i32 150000)
%32 = bitcast [55 x i8]* %7 to i8**
%33 = call i32 @read(i32 %3, i8** %32, i32 56)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = icmp slt i32 %33, 1
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %41
38: ; preds = %30
%39 = call i32 @memset(i8* %2, i32 0, i32 %33)
%40 = call i32 @memcpy(i8* %2, i8* %21, i32 %33)
br label %41
41: ; preds = %27, %38, %36, %25
%42 = phi i32 [ -1, %25 ], [ -1, %36 ], [ %33, %38 ], [ undef, %27 ]
ret i32 %42
}