#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 sigaction {
  int dummy;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ memset(struct sigaction *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(void) {
  int ret;
  fd_set rdfd;
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  while (1) {
    FD_ZERO(&rdfd);
    FD_SET(0, &rdfd);
    ret = select(1, &rdfd, NULL, NULL, NULL);
    if (ret > 0) {
      printf("read on the socket\n");
      char buf[100];
      ret = read(0, buf, sizeof(buf));
      if (ret > 0)
        printf("read %d bytes \n", ret);
      else
        return -1;
    } else if (ret < 0) {
      if (errno == EINTR) {
        printf("interrupted!\n");
        return -1;
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    -0xc(%rbp),%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0xc(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    120e <main+0xae>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x80(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11fd <main+0x9d>
mov    -0x8(%rbp),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1209 <main+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <main+0xfb>
jmp    1256 <main+0xf6>
cmpl   $0x0,-0x8(%rbp)
jge    1251 <main+0xf1>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
jne    124c <main+0xec>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125b <main+0xfb>
jmp    1251 <main+0xf1>
jmp    1256 <main+0xf6>
jmp    1182 <main+0x22>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    0xc(%rsp),%rbx
lea    0xe82(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xe59(%rip),%r12
mov    0x2e32(%rip),%r13
mov    0x2e23(%rip),%rbp
jmp    11c2 <main+0x52>
nopw   0x0(%rax,%rax,1)
js     1215 <main+0xa5>
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %eax,%eax
jle    11c0 <main+0x50>
mov    %r14,%rdi
call   1030 <puts@plt>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    122b <main+0xbb>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c2 <main+0x52>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
jne    11c2 <main+0x52>
lea    0xdea(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { i32 }
@.str = [20 x i8] c"read on the socket\0A\00"
@.str.1 = [16 x i8] c"read %d bytes \0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [14 x i8] c"interrupted!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sigaction
%5 = alloca [100 x i8]
store i32 0, i32* %1
%6 = call i32 @memset(%struct.sigaction* %4, i32 0, i32 4)
br label %7
7: ; preds = %0, %35
%8 = call i32 @FD_ZERO(i32* %3)
%9 = call i32 @FD_SET(i32 0, i32* %3)
%10 = call i32 @select(i32 1, i32* %3, i32* null, i32* null, i32* null)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %7
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%16 = call i32 @read(i32 0, i8* %15, i32 100)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %23
22: ; preds = %13
store i32 -1, i32* %1
br label %36
23: ; preds = %19
br label %35
24: ; preds = %7
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %36
33: ; preds = %27
br label %34
34: ; preds = %33, %24
br label %35
35: ; preds = %34, %23
br label %7
36: ; preds = %31, %22
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
%struct.sigaction = type { i32 }
@.str.1 = [16 x i8] c"read %d bytes \0A\00"
@errno = global i64 0
@EINTR = global i64 0
@str = [13 x i8] c"interrupted!\00"
@str.3 = [19 x i8] c"read on the socket\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sigaction
%3 = alloca [100 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sigaction* %2 to i8*
%6 = call i32 @memset(%struct.sigaction* %2, i32 0, i32 4)
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
br label %8
8: ; preds = %19, %0
%9 = call i32 @FD_ZERO(i32* %1)
%10 = call i32 @FD_SET(i32 0, i32* %1)
%11 = call i32 @select(i32 1, i32* %1, i32* null, i32* null, i32* null)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.3, i64 0, i64 0))
%15 = call i32 @read(i32 0, i8* %7, i32 100)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %19
19: ; preds = %17, %21, %23
br label %8
20: ; preds = %13
br label %29
21: ; preds = %8
%22 = icmp slt i32 %11, 0
br i1 %22, label %23, label %19
23: ; preds = %21
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %19
27: ; preds = %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %20, %27
ret i32 -1
}