#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 */
typedef int /*<<< orphan*/ c;

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ hwp_client_close();
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int get_hwp_rsp_char(int fd) {
  unsigned char c;
  if (-1 == fd) {
    fprintf(stderr,
            "Warning: Attempt to read from unopened HWP RSP client: Ignored\n");
    return -1;
  }
  while (1) {
    switch (read(fd, &c, sizeof(c))) {
    case -1:
      if ((EAGAIN != errno) && (EINTR != errno)) {
        fprintf(stderr,
                "Warning: Failed to read from HWP RSP client: Closing client "
                "connection: %s\n",
                strerror(errno));
        hwp_client_close();
        return -1;
      }
      break;
    case 0:
      hwp_client_close();
      return -1;
    default:
      return c & 0xff;
    }
  }
}


x86 assembly (clang, -O0):
<get_hwp_rsp_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    117c <get_hwp_rsp_char+0x3c>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <get_hwp_rsp_char+0x114>
jmp    1181 <get_hwp_rsp_char+0x41>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11b5 <get_hwp_rsp_char+0x75>
jmp    11a5 <get_hwp_rsp_char+0x65>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     122b <get_hwp_rsp_char+0xeb>
jmp    123e <get_hwp_rsp_char+0xfe>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%eax
je     1226 <get_hwp_rsp_char+0xe6>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%eax
je     1226 <get_hwp_rsp_char+0xe6>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <hwp_client_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <get_hwp_rsp_char+0x114>
jmp    124f <get_hwp_rsp_char+0x10f>
mov    $0x0,%al
call   1030 <hwp_client_close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <get_hwp_rsp_char+0x114>
movzbl -0x9(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x4(%rbp)
jmp    1254 <get_hwp_rsp_char+0x114>
jmp    1181 <get_hwp_rsp_char+0x41>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hwp_rsp_char>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0xffffffff,%edi
je     11c3 <get_hwp_rsp_char+0x83>
mov    %edi,%ebx
lea    0xf(%rsp),%r14
mov    0x2e60(%rip),%r15
mov    0x2e71(%rip),%r12
mov    0x2e62(%rip),%r13
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    11b6 <get_hwp_rsp_char+0x76>
mov    (%r12),%edi
cmp    %edi,(%r15)
je     1170 <get_hwp_rsp_char+0x30>
cmp    %edi,0x0(%r13)
je     1170 <get_hwp_rsp_char+0x30>
mov    0x2e46(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe98(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11ba <get_hwp_rsp_char+0x7a>
test   %eax,%eax
jne    11e1 <get_hwp_rsp_char+0xa1>
xor    %eax,%eax
call   1030 <hwp_client_close@plt>
jmp    11da <get_hwp_rsp_char+0x9a>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11e6 <get_hwp_rsp_char+0xa6>
movzbl 0xf(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [64 x i8] c"Warning: Attempt to read from unopened HWP RSP client: Ignored\0A\00"
@EAGAIN = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@.str.1 = [76 x i8] c"Warning: Failed to read from HWP RSP client: Closing client connection: %s\0A\00"
define i32 @get_hwp_rsp_char(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 -1, %5
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %36
10: ; preds = %1
br label %11
11: ; preds = %10, %35
%12 = load i32, i32* %3
%13 = call i32 @read(i32 %12, i8* %4, i32 1)
switch i32 %13, label %31 [
i32 -1, label %14
i32 0, label %29
]
14: ; preds = %11
%15 = load i32, i32* @EAGAIN
%16 = load i32, i32* @errno
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = load i32, i32* @EINTR
%20 = load i32, i32* @errno
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 (...) @hwp_client_close()
store i32 -1, i32* %2
br label %36
28: ; preds = %18, %14
br label %35
29: ; preds = %11
%30 = call i32 (...) @hwp_client_close()
store i32 -1, i32* %2
br label %36
31: ; preds = %11
%32 = load i8, i8* %4
%33 = zext i8 %32 to i32
%34 = and i32 %33, 255
store i32 %34, i32* %2
br label %36
35: ; preds = %28
br label %11
36: ; preds = %31, %29, %22, %7
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [64 x i8] c"Warning: Attempt to read from unopened HWP RSP client: Ignored\0A\00"
@EAGAIN = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@.str.1 = [76 x i8] c"Warning: Failed to read from HWP RSP client: Closing client connection: %s\0A\00"
define i32 @get_hwp_rsp_char(i32 %0) {
%2 = alloca i8
%3 = icmp eq i32 %0, -1
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0))
br label %26
7: ; preds = %1, %9
%8 = call i32 @read(i32 %0, i8* %2, i32 1)
switch i32 %8, label %23 [
i32 -1, label %9
i32 0, label %21
]
9: ; preds = %7
%10 = load i32, i32* @EAGAIN
%11 = load i32, i32* @errno
%12 = icmp eq i32 %10, %11
%13 = load i32, i32* @EINTR
%14 = icmp eq i32 %13, %11
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %7, label %16
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = tail call i8* @strerror(i32 %11)
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = tail call i32 (...) @hwp_client_close()
br label %26
21: ; preds = %7
%22 = tail call i32 (...) @hwp_client_close()
br label %26
23: ; preds = %7
%24 = load i8, i8* %2
%25 = zext i8 %24 to i32
br label %26
26: ; preds = %23, %21, %16, %4
%27 = phi i32 [ -1, %4 ], [ %25, %23 ], [ -1, %21 ], [ -1, %16 ]
ret i32 %27
}