#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 */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ warn(char *);
int read_data(int sockfd, unsigned char *readsig, int size) {
  fd_set rfds;
  struct timeval tv;
  int ret, n = 0;
  int i = 0;
  for (;;) {
    FD_ZERO(&rfds);
    FD_SET(sockfd, &rfds);
    tv.tv_sec = 10;
    tv.tv_usec = 0;
    ret = select(sockfd + 1, &rfds, NULL, NULL, &tv);
    if (ret == -1) {
      warn("Select()");
      return -1;
    }
    if (!ret) {
      return n;
    }
    ret = read(sockfd, readsig + n, size - n);
    if (ret == -1) {
      warn("Read()");
      return -1;
    }
    printf("read() %d\n", ret);
    for (i = 0; i < ret; i++) {
      printf("%02x ", readsig[n + i]);
      if (i && (i + 1) % 16 == 0) {
        printf("\n");
        if (i != n - 1)
          printf("     ");
      }
    }
    n += ret;
    if (n >= size)
      return n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <FD_SET@plt>
movl   $0xa,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e3 <read_data+0x83>
lea    0xe2e(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_data+0x1a4>
cmpl   $0x0,-0x2c(%rbp)
jne    11f8 <read_data+0x98>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1304 <read_data+0x1a4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x30(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1236 <read_data+0xd6>
lea    0xde4(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <read_data+0x1a4>
mov    -0x2c(%rbp),%esi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    12df <read_data+0x17f>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    -0x34(%rbp),%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
je     12cc <read_data+0x16c>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12cc <read_data+0x16c>
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     12c7 <read_data+0x167>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12cc <read_data+0x16c>
jmp    12d1 <read_data+0x171>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    124e <read_data+0xee>
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     12ff <read_data+0x19f>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1304 <read_data+0x1a4>
jmp    1180 <read_data+0x20>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edx,%r13d
mov    %rsi,0x28(%rsp)
mov    %edi,%r15d
lea    0x1(%r15),%eax
mov    %eax,0x14(%rsp)
xor    %ebx,%ebx
mov    %edx,0x10(%rsp)
mov    %r15,0x20(%rsp)
jmp    11bc <read_data+0x4c>
xchg   %ax,%ax
mov    0x18(%rsp),%rbx
add    0xc(%rsp),%ebx
mov    0x10(%rsp),%r13d
cmp    %r13d,%ebx
mov    0x20(%rsp),%r15
jge    1311 <read_data+0x1a1>
lea    0x34(%rsp),%r14
mov    %r14,%rdi
call   1070 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r14,%rsi
call   1090 <FD_SET@plt>
movl   $0xa,0x38(%rsp)
movq   $0x0,0x40(%rsp)
mov    0x14(%rsp),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x38(%rsp),%r8
call   1080 <select@plt>
cmp    $0xffffffff,%eax
je     12f7 <read_data+0x187>
test   %eax,%eax
je     1311 <read_data+0x1a1>
movslq %ebx,%rax
mov    0x28(%rsp),%rcx
lea    (%rcx,%rax,1),%r12
mov    %r13d,%edx
sub    %eax,%edx
mov    %r15d,%edi
mov    %r12,%rsi
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1300 <read_data+0x190>
mov    %eax,%ebp
mov    %rbx,0x18(%rsp)
lea    0xdd2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,0xc(%rsp)
test   %ebp,%ebp
lea    0xdc7(%rip),%rbx
jle    11a0 <read_data+0x30>
movzbl (%r12),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x1,0xc(%rsp)
je     11a0 <read_data+0x30>
mov    0x18(%rsp),%rax
add    $0xffffffff,%eax
mov    0xc(%rsp),%ecx
mov    $0x1,%ebp
sub    %rax,%rbp
mov    $0x1,%r13d
sub    %rcx,%r13
mov    $0x2,%r15d
jmp    12b6 <read_data+0x146>
nopl   0x0(%rax)
lea    (%r15,%r13,1),%rax
add    $0x1,%rax
add    $0x1,%r15
cmp    $0x2,%rax
je     11a0 <read_data+0x30>
movzbl -0x1(%r12,%r15,1),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   $0xf,%r15b
jne    12a0 <read_data+0x130>
lea    (%r15,%rbp,1),%r14
mov    $0xa,%edi
call   1030 <putchar@plt>
cmp    $0x2,%r14
lea    0xd36(%rip),%rbx
je     12a0 <read_data+0x130>
lea    0xd33(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a0 <read_data+0x130>
lea    0xd02(%rip),%rdi
jmp    1307 <read_data+0x197>
lea    0xd02(%rip),%rdi
call   1040 <warn@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [9 x i8] c"Select()\00"
@.str.1 = [7 x i8] c"Read()\00"
@.str.2 = [11 x i8] c"read() %d\0A\00"
@.str.3 = [6 x i8] c"%02x \00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [6 x i8] c" \00"
define i32 @read_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %11
store i32 0, i32* %12
br label %13
13: ; preds = %91, %3
%14 = call i32 @FD_ZERO(i32* %8)
%15 = load i32, i32* %5
%16 = call i32 @FD_SET(i32 %15, i32* %8)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 10, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %18
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
%21 = call i32 @select(i32 %20, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @warn(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %92
26: ; preds = %13
%27 = load i32, i32* %10
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = load i32, i32* %11
store i32 %30, i32* %4
br label %92
31: ; preds = %26
%32 = load i32, i32* %5
%33 = load i8*, i8** %6
%34 = load i32, i32* %11
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i32, i32* %7
%38 = load i32, i32* %11
%39 = sub nsw i32 %37, %38
%40 = call i32 @read(i32 %32, i8* %36, i32 %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = call i32 @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %92
45: ; preds = %31
%46 = load i32, i32* %10
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %46)
store i32 0, i32* %12
br label %48
48: ; preds = %79, %45
%49 = load i32, i32* %12
%50 = load i32, i32* %10
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %82
52: ; preds = %48
%53 = load i8*, i8** %6
%54 = load i32, i32* %11
%55 = load i32, i32* %12
%56 = add nsw i32 %54, %55
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %53, i64 %57
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %12
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %78
64: ; preds = %52
%65 = load i32, i32* %12
%66 = add nsw i32 %65, 1
%67 = srem i32 %66, 16
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %78
69: ; preds = %64
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%71 = load i32, i32* %12
%72 = load i32, i32* %11
%73 = sub nsw i32 %72, 1
%74 = icmp ne i32 %71, %73
br i1 %74, label %75, label %77
75: ; preds = %69
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %77
77: ; preds = %75, %69
br label %78
78: ; preds = %77, %64, %52
br label %79
79: ; preds = %78
%80 = load i32, i32* %12
%81 = add nsw i32 %80, 1
store i32 %81, i32* %12
br label %48
82: ; preds = %48
%83 = load i32, i32* %10
%84 = load i32, i32* %11
%85 = add nsw i32 %84, %83
store i32 %85, i32* %11
%86 = load i32, i32* %11
%87 = load i32, i32* %7
%88 = icmp sge i32 %86, %87
br i1 %88, label %89, label %91
89: ; preds = %82
%90 = load i32, i32* %11
store i32 %90, i32* %4
br label %92
91: ; preds = %82
br label %13
92: ; preds = %89, %43, %29, %24
%93 = load i32, i32* %4
ret i32 %93
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [9 x i8] c"Select()\00"
@.str.1 = [7 x i8] c"Read()\00"
@.str.2 = [11 x i8] c"read() %d\0A\00"
@.str.3 = [6 x i8] c"%02x \00"
@.str.5 = [6 x i8] c" \00"
define i32 @read_data(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%10 = add nsw i32 %0, 1
br label %11
11: ; preds = %50, %3
%12 = phi i32 [ 0, %3 ], [ %51, %50 ]
%13 = call i32 @FD_ZERO(i32* %4)
%14 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 10, i32* %8
store i64 0, i64* %9
%15 = call i32 @select(i32 %10, i32* %4, i32* null, i32* null, %struct.timeval* %5)
switch i32 %15, label %16 [
i32 -1, label %53
i32 0, label %56
]
16: ; preds = %11
%17 = sext i32 %12 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = sub nsw i32 %2, %12
%20 = call i32 @read(i32 %0, i8* %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %53, label %22
22: ; preds = %16
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%24 = icmp sgt i32 %20, 0
br i1 %24, label %25, label %50
25: ; preds = %22
%26 = add nsw i32 %12, -1
%27 = zext i32 %26 to i64
%28 = zext i32 %20 to i64
%29 = load i8, i8* %18
%30 = zext i8 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = icmp eq i32 %20, 1
br i1 %32, label %50, label %33
33: ; preds = %25, %48
%34 = phi i64 [ %40, %48 ], [ 1, %25 ]
%35 = add nsw i64 %34, %17
%36 = getelementptr inbounds i8, i8* %1, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = add nuw nsw i64 %34, 1
%41 = and i64 %40, 15
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %33
%44 = call i32 @putchar(i32 10)
%45 = icmp eq i64 %34, %27
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %33, %46, %43
%49 = icmp eq i64 %40, %28
br i1 %49, label %50, label %33
50: ; preds = %48, %25, %22
%51 = add nsw i32 %20, %12
%52 = icmp slt i32 %51, %2
br i1 %52, label %11, label %56
53: ; preds = %16, %11
%54 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %16 ]
%55 = call i32 @warn(i8* %54)
br label %56
56: ; preds = %50, %11, %53
%57 = phi i32 [ -1, %53 ], [ %51, %50 ], [ %12, %11 ]
ret i32 %57
}