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

/* Variables and functions */
scalar_t__ FD_ISSET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ read(scalar_t__, int *, int);
scalar_t__ select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
scalar_t__ serial_setup(char *, int, int /*<<< orphan*/, int /*<<< orphan*/,
                        int);
scalar_t__ serial_write(scalar_t__, int *, int);
int /*<<< orphan*/ stdout;
void main_first_test(int argc, char *argv[]) {
  uint8_t buffer[1024];
  ((void)argc);
  ((void)argv);
  int32_t fd, nbRead, nbWrite;
  fd = serial_setup(argv[1], 9600, (0), (0), (1));
  if (fd >= 0) {
    fprintf(stdout, "serial correctly configurated fd = %d\n", fd);
    nbWrite = serial_write(fd, (uint8_t *)"+++", 3);
    fprintf(stdout, "nbWrite = %d\n", nbWrite);
    fd_set rfs;
    struct timeval waitTime;
    waitTime.tv_sec = 2;
    waitTime.tv_usec = 0;
    FD_ZERO(&rfs);
    FD_SET(fd, &rfs);
    if ((select(fd + 1, &rfs, NULL, NULL, &waitTime) > 0)) {
      if (FD_ISSET(fd, &rfs)) {
        nbRead = read(fd, buffer, 1024);
        while (nbRead > 0) {
          fprintf(stdout, "nbRead = %d\n", nbRead);
          for (int i = 0; i < nbRead; i++) {
            fprintf(stdout, "0x%x (%c) ", buffer[i], buffer[i]);
          }
          fprintf(stdout, "\n");
          nbRead = read(fd, buffer, 1024);
        }
      }
    }
  }
}


x86 assembly (clang, -O0):
<main_first_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2580,%esi
xor    %ecx,%ecx
mov    $0x1,%r8d
mov    %ecx,%edx
call   1030 <serial_setup@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jl     1398 <main_first_test+0x218>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x1018(%rbp),%rdx
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1018(%rbp),%rdi
lea    0xe38(%rip),%rsi
mov    $0x3,%edx
call   1090 <serial_write@plt>
mov    %rax,-0x1028(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x1028(%rbp),%rdx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x2,-0x1040(%rbp)
movq   $0x0,-0x1038(%rbp)
lea    -0x102c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x1018(%rbp),%rdi
lea    -0x102c(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x1018(%rbp),%rdi
add    $0x1,%rdi
lea    -0x102c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x1040(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
cmp    $0x0,%rax
jle    1393 <main_first_test+0x213>
mov    -0x1018(%rbp),%rdi
lea    -0x102c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     138e <main_first_test+0x20e>
mov    -0x1018(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
jle    1389 <main_first_test+0x209>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
mov    -0x1020(%rbp),%rdx
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x1044(%rbp)
movslq -0x1044(%rbp),%rax
cmp    -0x1020(%rbp),%rax
jge    134e <main_first_test+0x1ce>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
movslq -0x1044(%rbp),%rax
mov    -0x1010(%rbp,%rax,4),%edx
movslq -0x1044(%rbp),%rax
mov    -0x1010(%rbp,%rax,4),%ecx
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1044(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1044(%rbp)
jmp    12f3 <main_first_test+0x173>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
lea    0xce6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1018(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x1020(%rbp)
jmp    12bd <main_first_test+0x13d>
jmp    138e <main_first_test+0x20e>
jmp    1393 <main_first_test+0x213>
jmp    1398 <main_first_test+0x218>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main_first_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    0x8(%rsi),%rdi
mov    $0x2580,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    $0x1,%r8d
call   1030 <serial_setup@plt>
test   %rax,%rax
js     12f6 <main_first_test+0x176>
mov    %rax,%r15
mov    0x2e24(%rip),%r14
mov    (%r14),%edi
lea    0xe3a(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xe50(%rip),%rsi
mov    %r15,%rdi
mov    $0x3,%edx
call   1090 <serial_write@plt>
mov    (%r14),%edi
lea    0xe3d(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
movl   $0x2,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
mov    %r15,%rdi
add    $0x1,%rdi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
test   %rax,%rax
jle    12f6 <main_first_test+0x176>
lea    0xc(%rsp),%rsi
mov    %r15,%rdi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12f6 <main_first_test+0x176>
lea    0x20(%rsp),%rsi
mov    %r15,%rdi
mov    $0x400,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    12f6 <main_first_test+0x176>
mov    %rax,%rbp
lea    0xdc9(%rip),%rbx
lea    0x20(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14),%edi
lea    0xd9f(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    0x20(%rsp,%r12,4),%ecx
mov    %rbx,%rsi
mov    %ecx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%rbp
jne    12b0 <main_first_test+0x130>
mov    (%r14),%edi
lea    0xd6d(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jg     1290 <main_first_test+0x110>
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@stdout = global i32 0
@.str = [39 x i8] c"serial correctly configurated fd = %d\0A\00"
@.str.1 = [4 x i8] c"+++\00"
@.str.2 = [14 x i8] c"nbWrite = %d\0A\00"
@.str.3 = [13 x i8] c"nbRead = %d\0A\00"
@.str.4 = [11 x i8] c"0x%x (%c) \00"
@.str.5 = [2 x i8] c"\0A\00"
define void @main_first_test(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [1024 x i32]
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%12 = load i32, i32* %3
%13 = load i8**, i8*** %4
%14 = load i8**, i8*** %4
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i64 @serial_setup(i8* %16, i32 9600, i32 0, i32 0, i32 1)
store i64 %17, i64* %6
%18 = load i64, i64* %6
%19 = icmp sge i64 %18, 0
br i1 %19, label %20, label %81
20: ; preds = %2
%21 = load i32, i32* @stdout
%22 = load i64, i64* %6
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i64 %22)
%24 = load i64, i64* %6
%25 = call i64 @serial_write(i64 %24, i32* bitcast ([4 x i8]* @.str.1 to i32*), i32 3)
store i64 %25, i64* %8
%26 = load i32, i32* @stdout
%27 = load i64, i64* %8
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i64 %27)
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 2, i32* %29
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %30
%31 = call i32 @FD_ZERO(i32* %9)
%32 = load i64, i64* %6
%33 = call i32 @FD_SET(i64 %32, i32* %9)
%34 = load i64, i64* %6
%35 = add nsw i64 %34, 1
%36 = call i64 @select(i64 %35, i32* %9, i32* null, i32* null, %struct.timeval* %10)
%37 = icmp sgt i64 %36, 0
br i1 %37, label %38, label %80
38: ; preds = %20
%39 = load i64, i64* %6
%40 = call i64 @FD_ISSET(i64 %39, i32* %9)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %79
42: ; preds = %38
%43 = load i64, i64* %6
%44 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
%45 = call i64 @read(i64 %43, i32* %44, i32 1024)
store i64 %45, i64* %7
br label %46
46: ; preds = %72, %42
%47 = load i64, i64* %7
%48 = icmp sgt i64 %47, 0
br i1 %48, label %49, label %78
49: ; preds = %46
%50 = load i32, i32* @stdout
%51 = load i64, i64* %7
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 %51)
store i32 0, i32* %11
br label %53
53: ; preds = %69, %49
%54 = load i32, i32* %11
%55 = sext i32 %54 to i64
%56 = load i64, i64* %7
%57 = icmp slt i64 %55, %56
br i1 %57, label %58, label %72
58: ; preds = %53
%59 = load i32, i32* @stdout
%60 = load i32, i32* %11
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 %61
%63 = load i32, i32* %62
%64 = load i32, i32* %11
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 %65
%67 = load i32, i32* %66
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %63, i32 %67)
br label %69
69: ; preds = %58
%70 = load i32, i32* %11
%71 = add nsw i32 %70, 1
store i32 %71, i32* %11
br label %53
72: ; preds = %53
%73 = load i32, i32* @stdout
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%75 = load i64, i64* %6
%76 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
%77 = call i64 @read(i64 %75, i32* %76, i32 1024)
store i64 %77, i64* %7
br label %46
78: ; preds = %46
br label %79
79: ; preds = %78, %38
br label %80
80: ; preds = %79, %20
br label %81
81: ; preds = %80, %2
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@stdout = global i32 0
@.str = [39 x i8] c"serial correctly configurated fd = %d\0A\00"
@.str.1 = [4 x i8] c"+++\00"
@.str.2 = [14 x i8] c"nbWrite = %d\0A\00"
@.str.3 = [13 x i8] c"nbRead = %d\0A\00"
@.str.4 = [11 x i8] c"0x%x (%c) \00"
@.str.5 = [2 x i8] c"\0A\00"
define void @main_first_test(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i32]
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast [1024 x i32]* %3 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @serial_setup(i8* %8, i32 9600, i32 0, i32 0, i32 1)
%10 = icmp sgt i64 %9, -1
br i1 %10, label %11, label %51
11: ; preds = %2
%12 = load i32, i32* @stdout
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i64 %9)
%14 = tail call i64 @serial_write(i64 %9, i32* bitcast ([4 x i8]* @.str.1 to i32*), i32 3)
%15 = load i32, i32* @stdout
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i64 %14)
%17 = bitcast i32* %4 to i8*
%18 = bitcast %struct.timeval* %5 to i8*
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 2, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %20
%21 = call i32 @FD_ZERO(i32* %4)
%22 = call i32 @FD_SET(i64 %9, i32* %4)
%23 = add nuw nsw i64 %9, 1
%24 = call i64 @select(i64 %23, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %50
26: ; preds = %11
%27 = call i64 @FD_ISSET(i64 %9, i32* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %50, label %29
29: ; preds = %26
%30 = getelementptr inbounds [1024 x i32], [1024 x i32]* %3, i64 0, i64 0
%31 = call i64 @read(i64 %9, i32* %30, i32 1024)
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %29, %37
%34 = phi i64 [ %40, %37 ], [ %31, %29 ]
%35 = load i32, i32* @stdout
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i64 %34)
br label %42
37: ; preds = %42
%38 = load i32, i32* @stdout
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%40 = call i64 @read(i64 %9, i32* %30, i32 1024)
%41 = icmp sgt i64 %40, 0
br i1 %41, label %33, label %50
42: ; preds = %33, %42
%43 = phi i64 [ 0, %33 ], [ %48, %42 ]
%44 = load i32, i32* @stdout
%45 = getelementptr inbounds [1024 x i32], [1024 x i32]* %3, i64 0, i64 %43
%46 = load i32, i32* %45
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %46, i32 %46)
%48 = add nuw nsw i64 %43, 1
%49 = icmp eq i64 %48, %34
br i1 %49, label %37, label %42
50: ; preds = %37, %29, %26, %11
br label %51
51: ; preds = %50, %2
ret void
}