#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 */

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int getpid();
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int write(int /*<<< orphan*/, char *, int);
void sigio(int signo) {
  int n = 0;
  char buf[4096 + 1];
  if ((n = read(STDIN_FILENO, buf, 4096)) < 0) {
    fprintf(stderr, "read error %d, %s\n", errno, strerror(errno));
    exit(-1);
  }
  fprintf(stderr, "[%08x] read len = %d\n", getpid(), n);
  if (n > 0) {
    if (buf[n - 1] != '\n')
      buf[n++] = '\n';
    if (write(STDERR_FILENO, buf, n) != n) {
      fprintf(stderr, "write error %d, %s\n", errno, strerror(errno));
      exit(-1);
    }
  } else
    exit(-1);
}


x86 assembly (clang, -O0):
<sigio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ee <sigio+0x8e>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1018(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1014(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1018(%rbp),%edi
mov    -0x1014(%rbp),%edx
mov    %rax,%rcx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x101c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x101c(%rbp),%edi
mov    %eax,%edx
mov    -0x8(%rbp),%ecx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
cmpl   $0x0,-0x8(%rbp)
jle    12ce <sigio+0x16e>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x1010(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     1255 <sigio+0xf5>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x8(%rbp)
cltq
movb   $0xa,-0x1010(%rbp,%rax,1)
0a
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
cmp    -0x8(%rbp),%eax
je     12c9 <sigio+0x169>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1020(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1024(%rbp),%edi
mov    -0x1020(%rbp),%edx
mov    %rax,%rcx
lea    0xd71(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    12d8 <sigio+0x178>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigio>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1010,%rsp
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    0x2e58(%rip),%r14
mov    (%r14),%ebp
test   %eax,%eax
js     11e5 <sigio+0x85>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe74(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %ebx,%ebx
je     1202 <sigio+0xa2>
lea    -0x1(%rbx),%eax
cmpb   $0xa,(%rsp,%rax,1)
je     11c2 <sigio+0x62>
mov    %ebx,%eax
add    $0x1,%ebx
movb   $0xa,(%rsp,%rax,1)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    120c <sigio+0xac>
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dec(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe04(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
jmp    122a <sigio+0xca>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    (%r14),%ebx
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe03(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"read error %d, %s\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"[%08x] read len = %d\0A\00"
@STDERR_FILENO = global i32 0
@.str.2 = [20 x i8] c"write error %d, %s\0A\00"
define void @sigio(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4097 x i8]
store i32 %0, i32* %2
store i32 0, i32* %3
%5 = load i32, i32* @STDIN_FILENO
%6 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 0
%7 = call i32 @read(i32 %5, i8* %6, i32 4096)
store i32 %7, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %13)
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 (...) @getpid()
%19 = load i32, i32* %3
%20 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = load i32, i32* %3
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %51
23: ; preds = %16
%24 = load i32, i32* %3
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 10
br i1 %30, label %31, label %36
31: ; preds = %23
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 1
store i32 %33, i32* %3
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 %34
store i8 10, i8* %35
br label %36
36: ; preds = %31, %23
%37 = load i32, i32* @STDERR_FILENO
%38 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 0
%39 = load i32, i32* %3
%40 = call i32 @write(i32 %37, i8* %38, i32 %39)
%41 = load i32, i32* %3
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %50
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %45, i8* %47)
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %36
br label %53
51: ; preds = %16
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %50
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"read error %d, %s\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"[%08x] read len = %d\0A\00"
@STDERR_FILENO = global i32 0
@.str.2 = [20 x i8] c"write error %d, %s\0A\00"
define void @sigio(i32 %0) {
%2 = alloca [4097 x i8]
%3 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @read(i32 %4, i8* %3, i32 4096)
%6 = icmp slt i32 %5, 0
%7 = load i32, i32* @stderr
br i1 %6, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %10)
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %1
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %5)
%16 = icmp eq i32 %5, 0
br i1 %16, label %38, label %17
17: ; preds = %13
%18 = add nsw i32 %5, -1
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 10
br i1 %22, label %27, label %23
23: ; preds = %17
%24 = add nuw nsw i32 %5, 1
%25 = zext i32 %5 to i64
%26 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 %25
store i8 10, i8* %26
br label %27
27: ; preds = %23, %17
%28 = phi i32 [ %24, %23 ], [ %5, %17 ]
%29 = load i32, i32* @STDERR_FILENO
%30 = call i32 @write(i32 %29, i8* %3, i32 %28)
%31 = icmp eq i32 %30, %28
br i1 %31, label %40, label %32
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = tail call i8* @strerror(i32 %34)
%36 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %34, i8* %35)
%37 = tail call i32 @exit(i32 -1)
unreachable
38: ; preds = %13
%39 = tail call i32 @exit(i32 -1)
unreachable
40: ; preds = %27
ret void
}