#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 */
#define EALREADY 131
#define EINPROGRESS 130
#define EINTR 129
#define EWOULDBLOCK 128
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fd_name(int);
int /*<<< orphan*/ send_error(char *, char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ strerror(int);
int check_error(int ret, int io, int fd) {
  if (ret > 0)
    return ret;
  if (ret == 0) {
    send_error("%s failed on %s: EOF",
               ((io == 2) ? "select" : ((io == 1) ? "write" : "read")),
               (fd_name(fd)));
    exit(1);
  }
  switch (errno) {
  case EINPROGRESS:
  case EWOULDBLOCK:
  case EALREADY:
  case EINTR:
    return 0;
  }
  send_error("%s failed on %s: %s",
             ((io == 2) ? "select" : ((io == 1) ? "write" : "read")),
             (fd_name(fd)), strerror(errno));
  exit(1);
}


x86 assembly (clang, -O0):
<check_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1166 <check_error+0x26>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1272 <check_error+0x132>
cmpl   $0x0,-0x8(%rbp)
jne    11d4 <check_error+0x94>
cmpl   $0x2,-0xc(%rbp)
jne    118a <check_error+0x4a>
lea    0xe94(%rip),%rax
mov    %rax,-0x18(%rbp)
jmp    11a6 <check_error+0x66>
mov    -0xc(%rbp),%edx
lea    0xe8e(%rip),%rax
lea    0xe81(%rip),%rcx
cmp    $0x1,%edx
cmove  %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <fd_name@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <send_error@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x3,%eax
ja     11fa <check_error+0xba>
jmp    11ee <check_error+0xae>
movl   $0x0,-0x4(%rbp)
jmp    1272 <check_error+0x132>
cmpl   $0x2,-0xc(%rbp)
jne    1214 <check_error+0xd4>
lea    0xe0a(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    1230 <check_error+0xf0>
mov    -0xc(%rbp),%edx
lea    0xe04(%rip),%rax
lea    0xdf7(%rip),%rcx
cmp    $0x1,%edx
cmove  %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <fd_name@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1030 <send_error@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_error>:
push   %rbp
push   %r14
push   %rbx
test   %edi,%edi
jg     1160 <check_error+0x20>
je     1167 <check_error+0x27>
mov    0x2e8f(%rip),%rbp
mov    0x0(%rbp),%eax
and    $0xfffffffc,%eax
xor    %edi,%edi
cmp    $0x80,%eax
jne    11ae <check_error+0x6e>
mov    %edi,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0x1,%esi
lea    0xeab(%rip),%rax
lea    0xeaa(%rip),%rcx
cmove  %rax,%rcx
cmp    $0x2,%esi
lea    0xe8f(%rip),%rbx
cmovne %rcx,%rbx
mov    %edx,%edi
call   1040 <fd_name@plt>
lea    0xe68(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <send_error@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
cmp    $0x1,%esi
lea    0xe64(%rip),%rax
lea    0xe63(%rip),%rcx
cmove  %rax,%rcx
cmp    $0x2,%esi
lea    0xe48(%rip),%rbx
cmovne %rcx,%rbx
mov    %edx,%edi
call   1040 <fd_name@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%edi
call   1060 <strerror@plt>
lea    0xe3d(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <send_error@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"%s failed on %s: EOF\00"
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [5 x i8] c"read\00"
@errno = global i32 0
@.str.4 = [20 x i8] c"%s failed on %s: %s\00"
define i32 @check_error(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i32, i32* %5
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = load i32, i32* %5
store i32 %11, i32* %4
br label %50
12: ; preds = %3
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %30
15: ; preds = %12
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 2
br i1 %17, label %18, label %19
18: ; preds = %15
br label %24
19: ; preds = %15
%20 = load i32, i32* %6
%21 = icmp eq i32 %20, 1
%22 = zext i1 %21 to i64
%23 = select i1 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
br label %24
24: ; preds = %19, %18
%25 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %18 ], [ %23, %19 ]
%26 = load i32, i32* %7
%27 = call i32 @fd_name(i32 %26)
%28 = call i32 (i8*, i8*, i32, ...) @send_error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %12
%31 = load i32, i32* @errno
switch i32 %31, label %33 [
i32 130, label %32
i32 128, label %32
i32 131, label %32
i32 129, label %32
]
32: ; preds = %30, %30, %30, %30
store i32 0, i32* %4
br label %50
33: ; preds = %30
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %37
36: ; preds = %33
br label %42
37: ; preds = %33
%38 = load i32, i32* %6
%39 = icmp eq i32 %38, 1
%40 = zext i1 %39 to i64
%41 = select i1 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
br label %42
42: ; preds = %37, %36
%43 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %36 ], [ %41, %37 ]
%44 = load i32, i32* %7
%45 = call i32 @fd_name(i32 %44)
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = call i32 (i8*, i8*, i32, ...) @send_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %43, i32 %45, i32 %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %32, %10
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@.str = [21 x i8] c"%s failed on %s: EOF\00"
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [5 x i8] c"read\00"
@errno = global i32 0
@.str.4 = [20 x i8] c"%s failed on %s: %s\00"
define i32 @check_error(i32 %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %0, 0
br i1 %4, label %29, label %5
5: ; preds = %3
%6 = icmp eq i32 %0, 0
br i1 %6, label %7, label %15
7: ; preds = %5
%8 = icmp eq i32 %1, 2
%9 = icmp eq i32 %1, 1
%10 = select i1 %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
%11 = select i1 %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %10
%12 = tail call i32 @fd_name(i32 %2)
%13 = tail call i32 (i8*, i8*, i32, ...) @send_error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %5
%16 = load i32, i32* @errno
%17 = and i32 %16, -4
%18 = icmp eq i32 %17, 128
br i1 %18, label %29, label %19
19: ; preds = %15
%20 = icmp eq i32 %1, 2
%21 = icmp eq i32 %1, 1
%22 = select i1 %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0)
%23 = select i1 %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %22
%24 = tail call i32 @fd_name(i32 %2)
%25 = load i32, i32* @errno
%26 = tail call i32 @strerror(i32 %25)
%27 = tail call i32 (i8*, i8*, i32, ...) @send_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %23, i32 %24, i32 %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %15, %3
%30 = phi i32 [ %0, %3 ], [ 0, %15 ]
ret i32 %30
}