#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 scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ sock_read(int, char *, int);
ssize_t sock_readbyte(int sock, char *byte) {
  static ssize_t bytes_read;
  static char buffer[512];
  static char *buf_ptr;
  if (bytes_read <= 0) {
    bytes_read = sock_read(sock, buffer, 512);
    if (bytes_read <= 0)
      return bytes_read;
    buf_ptr = buffer;
  }
  bytes_read--;
  *byte = *buf_ptr++;
  return 1;
}


x86 assembly (clang, -O0):
<sock_readbyte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,0x2f19(%rip)
jg     1174 <sock_readbyte+0x64>
mov    -0xc(%rbp),%edi
lea    0x2f19(%rip),%rsi
mov    $0x200,%edx
call   1030 <sock_read@plt>
mov    %rax,0x2ef8(%rip)
cmpq   $0x0,0x2ef0(%rip)
jg     1166 <sock_readbyte+0x56>
mov    0x2ee3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11ab <sock_readbyte+0x9b>
lea    0x2ee3(%rip),%rax
mov    %rax,0x30dc(%rip)
mov    0x2ec5(%rip),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,0x2eba(%rip)
mov    0x30c3(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x30b5(%rip)
mov    (%rax),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
movq   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_readbyte>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2f22(%rip),%rax
test   %rax,%rax
jle    112c <sock_readbyte+0x1c>
mov    0x3126(%rip),%rbx
jmp    114c <sock_readbyte+0x3c>
lea    0x2f1d(%rip),%rbx
mov    %rbx,%rsi
mov    $0x200,%edx
call   1030 <sock_read@plt>
mov    %rax,0x2ef9(%rip)
test   %rax,%rax
jle    116c <sock_readbyte+0x5c>
add    $0xffffffffffffffff,%rax
mov    %rax,0x2ee9(%rip)
lea    0x1(%rbx),%rax
mov    %rax,0x30ee(%rip)
mov    (%rbx),%al
mov    %al,(%r14)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@sock_readbyte.bytes_read = internal global i64 0
@sock_readbyte.buffer = internal global [512 x i8] zeroinitializer
@sock_readbyte.buf_ptr = internal global i8* null
define i64 @sock_readbyte(i32 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%6 = load i64, i64* @sock_readbyte.bytes_read
%7 = icmp sle i64 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %2
%9 = load i32, i32* %4
%10 = call i64 @sock_read(i32 %9, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sock_readbyte.buffer, i64 0, i64 0), i32 512)
store i64 %10, i64* @sock_readbyte.bytes_read
%11 = load i64, i64* @sock_readbyte.bytes_read
%12 = icmp sle i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = load i64, i64* @sock_readbyte.bytes_read
store i64 %14, i64* %3
br label %23
15: ; preds = %8
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sock_readbyte.buffer, i64 0, i64 0), i8** @sock_readbyte.buf_ptr
br label %16
16: ; preds = %15, %2
%17 = load i64, i64* @sock_readbyte.bytes_read
%18 = add nsw i64 %17, -1
store i64 %18, i64* @sock_readbyte.bytes_read
%19 = load i8*, i8** @sock_readbyte.buf_ptr
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** @sock_readbyte.buf_ptr
%21 = load i8, i8* %19
%22 = load i8*, i8** %5
store i8 %21, i8* %22
store i64 1, i64* %3
br label %23
23: ; preds = %16, %13
%24 = load i64, i64* %3
ret i64 %24
}

LLVM IR (-O2):
@sock_readbyte.bytes_read = internal unnamed_addr global i64 0
@sock_readbyte.buffer = internal global [512 x i8] zeroinitializer
@sock_readbyte.buf_ptr = internal unnamed_addr global i8* null
define i64 @sock_readbyte(i32 %0, i8* nocapture writeonly %1) {
%3 = load i64, i64* @sock_readbyte.bytes_read
%4 = icmp slt i64 %3, 1
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = load i8*, i8** @sock_readbyte.buf_ptr
br label %10
7: ; preds = %2
%8 = tail call i64 @sock_read(i32 %0, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sock_readbyte.buffer, i64 0, i64 0), i32 512)
store i64 %8, i64* @sock_readbyte.bytes_read
%9 = icmp slt i64 %8, 1
br i1 %9, label %16, label %10
10: ; preds = %7, %5
%11 = phi i8* [ %6, %5 ], [ getelementptr inbounds ([512 x i8], [512 x i8]* @sock_readbyte.buffer, i64 0, i64 0), %7 ]
%12 = phi i64 [ %3, %5 ], [ %8, %7 ]
%13 = add nsw i64 %12, -1
store i64 %13, i64* @sock_readbyte.bytes_read
%14 = getelementptr inbounds i8, i8* %11, i64 1
store i8* %14, i8** @sock_readbyte.buf_ptr
%15 = load i8, i8* %11
store i8 %15, i8* %1
br label %16
16: ; preds = %7, %10
%17 = phi i64 [ 1, %10 ], [ %8, %7 ]
ret i64 %17
}