#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;
  int /*<<< orphan*/ 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*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int strlen(char *);
size_t write(int, char *, size_t);
int main(void) {
  fd_set rfds;
  fd_set wfds;
  int ret;
  int fd[2];
  char string[] = "Hello, world!\n";
  struct timeval tv = {.tv_sec = 10, .tv_usec = 0};
  ret = pipe(fd);
  if (ret < 0) {
    perror("pipe creation failed");
    return 1;
  }
  FD_ZERO(&rfds);
  FD_ZERO(&wfds);
  FD_SET(fd[0], &rfds);
  FD_SET(fd[1], &wfds);
  ret = select(fd[1] + 1, NULL, &wfds, NULL, &tv);
  if (ret <= 0) {
    perror("select() on write event failed");
    return 1;
  }
  printf("select() on write event returned %d file descriptors\n", ret);
  size_t size = strlen(string) + 1;
  if (write(fd[1], string, size) != size) {
    perror("write error");
    return 1;
  }
  ret = select(fd[1] + 1, &rfds, NULL, NULL, &tv);
  if (ret <= 0) {
    perror("select() on read event failed");
    return 1;
  }
  printf("select() on read event returned %d file descriptors\n", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x27(%rbp)
mov    0xe68(%rip),%eax
mov    %eax,-0x1f(%rbp)
mov    0xe62(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xe5a(%rip),%al
mov    %al,-0x19(%rbp)
mov    0xf1a(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11f0 <main+0x70>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <main+0x181>
lea    -0x8(%rbp),%rdi
call   1070 <FD_ZERO@plt>
lea    -0xc(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x18(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0xc(%rbp),%rdx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
call   1080 <select@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     1259 <main+0xd9>
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <main+0x181>
mov    -0x10(%rbp),%esi
lea    0xde0(%rip),%rdi
call   1050 <printf@plt>
lea    -0x27(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x27(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x38(%rbp),%rax
je     12ac <main+0x12c>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <main+0x181>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     12eb <main+0x16b>
lea    0xdab(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1301 <main+0x181>
mov    -0x10(%rbp),%esi
lea    0xdae(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movabs $0xa21646c726f77,%rax
mov    %rax,0x2f(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,0x28(%rsp)
movq   $0xa,0x18(%rsp)
lea    0xc(%rsp),%rdi
call   1060 <pipe@plt>
test   %eax,%eax
js     1273 <main+0xf3>
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
lea    0x24(%rsp),%r14
mov    %r14,%rdi
call   1070 <FD_ZERO@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
mov    0x10(%rsp),%edi
mov    %r14,%rsi
call   10a0 <FD_SET@plt>
mov    0x10(%rsp),%edi
add    $0x1,%edi
lea    0x18(%rsp),%r8
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1080 <select@plt>
test   %eax,%eax
jle    127c <main+0xfc>
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0x28(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
cmp    %rbx,%rax
jne    1285 <main+0x105>
mov    0x10(%rsp),%edi
add    $0x1,%edi
xor    %ebx,%ebx
lea    0x14(%rsp),%rsi
lea    0x18(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
test   %eax,%eax
jle    128e <main+0x10e>
lea    0xe39(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
jmp    129f <main+0x11f>
lea    0xd95(%rip),%rdi
jmp    1295 <main+0x115>
lea    0xda1(%rip),%rdi
jmp    1295 <main+0x115>
lea    0xded(%rip),%rdi
jmp    1295 <main+0x115>
lea    0xdf0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.main.string = [15 x i8] c"Hello, world!\0A\00"
@__const.main.tv = %struct.timeval { i32 10, i32 0 }
@.str = [21 x i8] c"pipe creation failed\00"
@.str.1 = [31 x i8] c"select() on write event failed\00"
@.str.2 = [54 x i8] c"select() on write event returned %d file descriptors\0A\00"
@.str.3 = [12 x i8] c"write error\00"
@.str.4 = [30 x i8] c"select() on read event failed\00"
@.str.5 = [53 x i8] c"select() on read event returned %d file descriptors\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca [15 x i8]
%7 = alloca %struct.timeval
%8 = alloca i64
store i32 0, i32* %1
%9 = bitcast [15 x i8]* %6 to i8*
%10 = bitcast %struct.timeval* %7 to i8*
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %62
17: ; preds = %0
%18 = call i32 @FD_ZERO(i32* %2)
%19 = call i32 @FD_ZERO(i32* %3)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%21 = load i32, i32* %20
%22 = call i32 @FD_SET(i32 %21, i32* %2)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @FD_SET(i32 %24, i32* %3)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%27 = load i32, i32* %26
%28 = add nsw i32 %27, 1
%29 = call i32 @select(i32 %28, i32* null, i32* %3, i32* null, %struct.timeval* %7)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %62
34: ; preds = %17
%35 = load i32, i32* %4
%36 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds [15 x i8], [15 x i8]* %6, i64 0, i64 0
%38 = call i32 @strlen(i8* %37)
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
store i64 %40, i64* %8
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds [15 x i8], [15 x i8]* %6, i64 0, i64 0
%44 = load i64, i64* %8
%45 = call i64 @write(i32 %42, i8* %43, i64 %44)
%46 = load i64, i64* %8
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %34
%49 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %62
50: ; preds = %34
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%52 = load i32, i32* %51
%53 = add nsw i32 %52, 1
%54 = call i32 @select(i32 %53, i32* %2, i32* null, i32* null, %struct.timeval* %7)
store i32 %54, i32* %4
%55 = load i32, i32* %4
%56 = icmp sle i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %62
59: ; preds = %50
%60 = load i32, i32* %4
%61 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i32 %60)
store i32 0, i32* %1
br label %62
62: ; preds = %59, %57, %48, %32, %15
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@__const.main.string = [15 x i8] c"Hello, world!\0A\00"
@.str = [21 x i8] c"pipe creation failed\00"
@.str.1 = [31 x i8] c"select() on write event failed\00"
@.str.2 = [54 x i8] c"select() on write event returned %d file descriptors\0A\00"
@.str.3 = [12 x i8] c"write error\00"
@.str.4 = [30 x i8] c"select() on read event failed\00"
@.str.5 = [53 x i8] c"select() on read event returned %d file descriptors\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca [15 x i8]
%5 = alloca i64
%6 = bitcast i64* %5 to %struct.timeval*
%7 = bitcast i32* %1 to i8*
%8 = bitcast i32* %2 to i8*
%9 = bitcast [2 x i32]* %3 to i8*
%10 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%11 = bitcast i64* %5 to i8*
store i64 10, i64* %5
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %50
17: ; preds = %0
%18 = call i32 @FD_ZERO(i32* %1)
%19 = call i32 @FD_ZERO(i32* %2)
%20 = load i32, i32* %12
%21 = call i32 @FD_SET(i32 %20, i32* %1)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @FD_SET(i32 %23, i32* %2)
%25 = load i32, i32* %22
%26 = add nsw i32 %25, 1
%27 = call i32 @select(i32 %26, i32* null, i32* %2, i32* null, %struct.timeval* %6)
%28 = icmp slt i32 %27, 1
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %50
31: ; preds = %17
%32 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%33 = call i32 @strlen(i8* %10)
%34 = add nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = load i32, i32* %22
%37 = call i64 @write(i32 %36, i8* %10, i64 %35)
%38 = icmp eq i64 %37, %35
br i1 %38, label %41, label %39
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %50
41: ; preds = %31
%42 = load i32, i32* %22
%43 = add nsw i32 %42, 1
%44 = call i32 @select(i32 %43, i32* %1, i32* null, i32* null, %struct.timeval* %6)
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %50
48: ; preds = %41
%49 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0), i32 %44)
br label %50
50: ; preds = %39, %46, %48, %29, %15
%51 = phi i32 [ 1, %15 ], [ 1, %29 ], [ 1, %39 ], [ 1, %46 ], [ 0, %48 ]
ret i32 %51
}