#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_sec;
  scalar_t__ tv_usec;
};
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_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ write(int, char *, int);
void uartloopback_main(int argc, char *argv[]) {
  static char buf[16];
  int port;
  int ret = 0;
  for (port = 1; port < 5; port++) {
    snprintf(buf, 16, "/dev/ttyS%d", port);
    int fd = open(buf, O_RDWR | O_NOCTTY);
    if (fd < 0) {
      ret = 1;
    } else {
      char buf_tx[255] = "Hello";
      if (write(fd, buf_tx, 5) < 0) {
        ret = 1;
      }
      char buf_rx[255];
      struct timeval tv;
      fd_set readfds;
      FD_ZERO(&readfds);
      FD_SET(fd, &readfds);
      tv.tv_sec = 1;
      tv.tv_usec = 0;
      if (select(fd + 1, &readfds, NULL, NULL, &tv) < 0) {
        ret = 1;
      } else if (FD_ISSET(fd, &readfds)) {
        if (read(fd, buf_rx, 5) < 0) {
          ret = 1;
        }
      }
      int i;
      for (i = 0; i < 5; i++) {
        if (buf_tx[i] != buf_rx[i]) {
          ret = 1;
        }
      }
      if (ret) {
        printf("UART%d LOOPBACK FAIL!!!\n", port);
        if (fd > 0) {
          close(fd);
        }
        ret = 0;
      } else {
        printf("UART%d LOOPBACK PASS!!!\n", port);
        close(fd);
      }
    }
  }
}


x86 assembly (clang, -O0):
<uartloopback_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    13f8 <uartloopback_main+0x248>
mov    -0x14(%rbp),%ecx
lea    0x2eac(%rip),%rdi
mov    $0x10,%esi
lea    0xe10(%rip),%rdx
call   1060 <snprintf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
or     (%rax),%esi
lea    0x2e82(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122c <uartloopback_main+0x7c>
movl   $0x1,-0x18(%rbp)
jmp    13e5 <uartloopback_main+0x235>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1070 <memset@plt>
movb   $0x48,-0x120(%rbp)
movb   $0x65,-0x11f(%rbp)
movb   $0x6c,-0x11e(%rbp)
movb   $0x6c,-0x11d(%rbp)
movb   $0x6f,-0x11c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1287 <uartloopback_main+0xd7>
movl   $0x1,-0x18(%rbp)
lea    -0x234(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x234(%rbp),%rsi
call   10d0 <FD_SET@plt>
movl   $0x1,-0x230(%rbp)
movq   $0x0,-0x228(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x234(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x230(%rbp),%r8
mov    %rcx,%rdx
call   10b0 <select@plt>
cmp    $0x0,%rax
jge    12ed <uartloopback_main+0x13d>
movl   $0x1,-0x18(%rbp)
jmp    1335 <uartloopback_main+0x185>
mov    -0x1c(%rbp),%edi
lea    -0x234(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1330 <uartloopback_main+0x180>
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x5,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jge    132b <uartloopback_main+0x17b>
movl   $0x1,-0x18(%rbp)
jmp    1330 <uartloopback_main+0x180>
jmp    1335 <uartloopback_main+0x185>
movl   $0x0,-0x238(%rbp)
cmpl   $0x5,-0x238(%rbp)
jge    1392 <uartloopback_main+0x1e2>
movslq -0x238(%rbp),%rax
movsbl -0x120(%rbp,%rax,1),%eax
ff
movslq -0x238(%rbp),%rcx
movsbl -0x220(%rbp,%rcx,1),%ecx
ff
cmp    %ecx,%eax
je     1379 <uartloopback_main+0x1c9>
movl   $0x1,-0x18(%rbp)
jmp    137e <uartloopback_main+0x1ce>
mov    -0x238(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x238(%rbp)
jmp    133f <uartloopback_main+0x18f>
cmpl   $0x0,-0x18(%rbp)
je     13c9 <uartloopback_main+0x219>
mov    -0x14(%rbp),%esi
lea    0xc66(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    13bd <uartloopback_main+0x20d>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x18(%rbp)
jmp    13e0 <uartloopback_main+0x230>
mov    -0x14(%rbp),%esi
lea    0xc52(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
jmp    13e5 <uartloopback_main+0x235>
jmp    13ea <uartloopback_main+0x23a>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d0 <uartloopback_main+0x20>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uartloopback_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
xor    %r13d,%r13d
lea    0x2ec5(%rip),%r12
lea    0xc(%rsp),%rbx
mov    $0x1,%ebp
jmp    11f7 <uartloopback_main+0x57>
nopw   0x0(%rax,%rax,1)
lea    0xe4e(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    %r15d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x1,%ebp
mov    %eax,%r13d
cmp    $0x5,%ebp
je     1384 <uartloopback_main+0x1e4>
mov    %r12,%rdi
mov    $0x10,%esi
lea    0xdfa(%rip),%rdx
mov    %ebp,%ecx
call   1060 <snprintf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
or     (%rax),%esi
mov    %r12,%rdi
call   10b0 <open@plt>
mov    %eax,%r15d
mov    $0x1,%eax
test   %r15d,%r15d
js     11e8 <uartloopback_main+0x48>
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movups %xmm0,0x10f(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movl   $0x6c6c6548,0x20(%rsp)
6c
movb   $0x6f,0x24(%rsp)
mov    %r15d,%edi
lea    0x20(%rsp),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
test   %rax,%rax
mov    $0x1,%r14d
cmovs  %r14d,%r13d
mov    %rbx,%rdi
call   1090 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
movl   $0x1,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x1(%r15),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   10a0 <select@plt>
test   %rax,%rax
js     134d <uartloopback_main+0x1ad>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     134a <uartloopback_main+0x1aa>
mov    %r15d,%edi
lea    0x120(%rsp),%rsi
mov    $0x5,%edx
call   1080 <read@plt>
test   %rax,%rax
mov    $0x1,%eax
cmovs  %eax,%r13d
movzbl 0x120(%rsp),%ecx
jmp    134a <uartloopback_main+0x1aa>
mov    %r13d,%r14d
cmpl   $0x6f6c6c65,0x121(%rsp)
65 6c 6c 6f
jne    1368 <uartloopback_main+0x1c8>
cmp    $0x48,%cl
jne    1368 <uartloopback_main+0x1c8>
test   %r14d,%r14d
je     11d0 <uartloopback_main+0x30>
lea    0xc9d(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
test   %r15d,%r15d
jne    11de <uartloopback_main+0x3e>
jmp    11e6 <uartloopback_main+0x46>
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@uartloopback_main.buf = internal global [16 x i8] zeroinitializer
@.str = [12 x i8] c"/dev/ttyS%d\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [25 x i8] c"UART%d LOOPBACK FAIL!!!\0A\00"
@.str.2 = [25 x i8] c"UART%d LOOPBACK PASS!!!\0A\00"
define void @uartloopback_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [255 x i8]
%9 = alloca [255 x i8]
%10 = alloca %struct.timeval
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %6
store i32 1, i32* %5
br label %13
13: ; preds = %102, %2
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 5
br i1 %15, label %16, label %105
16: ; preds = %13
%17 = load i32, i32* %5
%18 = call i32 @snprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @uartloopback_main.buf, i64 0, i64 0), i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_NOCTTY
%21 = or i32 %19, %20
%22 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @uartloopback_main.buf, i64 0, i64 0), i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %16
store i32 1, i32* %6
br label %101
26: ; preds = %16
%27 = bitcast [255 x i8]* %8 to i8*
%28 = bitcast i8* %27 to [255 x i8]*
%29 = getelementptr inbounds [255 x i8], [255 x i8]* %28, i32 0, i32 0
store i8 72, i8* %29
%30 = getelementptr inbounds [255 x i8], [255 x i8]* %28, i32 0, i32 1
store i8 101, i8* %30
%31 = getelementptr inbounds [255 x i8], [255 x i8]* %28, i32 0, i32 2
store i8 108, i8* %31
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %28, i32 0, i32 3
store i8 108, i8* %32
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %28, i32 0, i32 4
store i8 111, i8* %33
%34 = load i32, i32* %7
%35 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%36 = call i64 @write(i32 %34, i8* %35, i32 5)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %26
store i32 1, i32* %6
br label %39
39: ; preds = %38, %26
%40 = call i32 @FD_ZERO(i32* %11)
%41 = load i32, i32* %7
%42 = call i32 @FD_SET(i32 %41, i32* %11)
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 1, i32* %43
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i64 0, i64* %44
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
%47 = call i64 @select(i32 %46, i32* %11, i32* null, i32* null, %struct.timeval* %10)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %39
store i32 1, i32* %6
br label %62
50: ; preds = %39
%51 = load i32, i32* %7
%52 = call i64 @FD_ISSET(i32 %51, i32* %11)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %50
%55 = load i32, i32* %7
%56 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%57 = call i64 @read(i32 %55, i8* %56, i32 5)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %54
store i32 1, i32* %6
br label %60
60: ; preds = %59, %54
br label %61
61: ; preds = %60, %50
br label %62
62: ; preds = %61, %49
store i32 0, i32* %12
br label %63
63: ; preds = %80, %62
%64 = load i32, i32* %12
%65 = icmp slt i32 %64, 5
br i1 %65, label %66, label %83
66: ; preds = %63
%67 = load i32, i32* %12
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 %68
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = load i32, i32* %12
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 %73
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp ne i32 %71, %76
br i1 %77, label %78, label %79
78: ; preds = %66
store i32 1, i32* %6
br label %79
79: ; preds = %78, %66
br label %80
80: ; preds = %79
%81 = load i32, i32* %12
%82 = add nsw i32 %81, 1
store i32 %82, i32* %12
br label %63
83: ; preds = %63
%84 = load i32, i32* %6
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %95
86: ; preds = %83
%87 = load i32, i32* %5
%88 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %87)
%89 = load i32, i32* %7
%90 = icmp sgt i32 %89, 0
br i1 %90, label %91, label %94
91: ; preds = %86
%92 = load i32, i32* %7
%93 = call i32 @close(i32 %92)
br label %94
94: ; preds = %91, %86
store i32 0, i32* %6
br label %100
95: ; preds = %83
%96 = load i32, i32* %5
%97 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %96)
%98 = load i32, i32* %7
%99 = call i32 @close(i32 %98)
br label %100
100: ; preds = %95, %94
br label %101
101: ; preds = %100, %25
br label %102
102: ; preds = %101
%103 = load i32, i32* %5
%104 = add nsw i32 %103, 1
store i32 %104, i32* %5
br label %13
105: ; preds = %13
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@uartloopback_main.buf = internal global [16 x i8] zeroinitializer
@.str = [12 x i8] c"/dev/ttyS%d\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [25 x i8] c"UART%d LOOPBACK FAIL!!!\0A\00"
@.str.2 = [25 x i8] c"UART%d LOOPBACK PASS!!!\0A\00"
define void @uartloopback_main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [255 x i8]
%4 = alloca [255 x i8]
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 1
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 2
%10 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 3
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 4
%12 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%13 = bitcast %struct.timeval* %5 to i8*
%14 = bitcast i32* %6 to i8*
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 1
%18 = bitcast i8* %17 to i32*
br label %19
19: ; preds = %2, %62
%20 = phi i32 [ 1, %2 ], [ %64, %62 ]
%21 = phi i32 [ 0, %2 ], [ %63, %62 ]
%22 = call i32 @snprintf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @uartloopback_main.buf, i64 0, i64 0), i32 16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %20)
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_NOCTTY
%25 = or i32 %24, %23
%26 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @uartloopback_main.buf, i64 0, i64 0), i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %62, label %28
28: ; preds = %19
store i8 72, i8* %7
store i8 101, i8* %8
store i8 108, i8* %9
store i8 108, i8* %10
store i8 111, i8* %11
%29 = call i64 @write(i32 %26, i8* %7, i32 5)
%30 = icmp slt i64 %29, 0
%31 = select i1 %30, i32 1, i32 %21
%32 = call i32 @FD_ZERO(i32* %6)
%33 = call i32 @FD_SET(i32 %26, i32* %6)
store i32 1, i32* %15
store i64 0, i64* %16
%34 = add nuw nsw i32 %26, 1
%35 = call i64 @select(i32 %34, i32* %6, i32* null, i32* null, %struct.timeval* %5)
%36 = icmp slt i64 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %28
%38 = call i64 @FD_ISSET(i32 %26, i32* %6)
%39 = icmp eq i64 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %37
%41 = call i64 @read(i32 %26, i8* %12, i32 5)
%42 = icmp slt i64 %41, 0
%43 = select i1 %42, i32 1, i32 %31
%44 = load i8, i8* %12
br label %45
45: ; preds = %40, %28, %37
%46 = phi i8 [ undef, %37 ], [ undef, %28 ], [ %44, %40 ]
%47 = phi i32 [ %31, %37 ], [ 1, %28 ], [ %43, %40 ]
%48 = icmp eq i8 %46, 72
%49 = load i32, i32* %18
%50 = icmp eq i32 %47, 0
%51 = icmp eq i32 %49, 1869376613
%52 = select i1 %51, i1 %48, i1 false
%53 = and i1 %52, %50
br i1 %53, label %57, label %54
54: ; preds = %45
%55 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%56 = icmp eq i32 %26, 0
br i1 %56, label %61, label %59
57: ; preds = %45
%58 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %59
59: ; preds = %54, %57
%60 = call i32 @close(i32 %26)
br label %61
61: ; preds = %59, %54
br label %62
62: ; preds = %19, %61
%63 = phi i32 [ 0, %61 ], [ 1, %19 ]
%64 = add nuw nsw i32 %20, 1
%65 = icmp eq i32 %64, 5
br i1 %65, label %66, label %19
66: ; preds = %62
ret void
}