#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 */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t packet_size(unsigned char *);
scalar_t__ read(int /*<<< orphan*/, void *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  unsigned int bShutdown = (0);
  fprintf(stderr, "Entering the loop.\n");
  while (!bShutdown) {
    unsigned char packet_size_buff[((size_t)2)];
    unsigned char packet_payload[((size_t)65536)];
    ssize_t bytes_read = 0;
    bytes_read = read(fileno(stdin), (void *)packet_size_buff, ((size_t)2));
    if (bytes_read > 0) {
      size_t bytes_to_read = packet_size(packet_size_buff);
      fprintf(stderr, "Packet size: %d\n", bytes_to_read);
      bytes_read = read(fileno(stdin), (void *)packet_payload, bytes_to_read);
      if (bytes_read != bytes_to_read) {
        bShutdown = (1);
      } else {
      }
    } else {
      fprintf(stderr, "Error: %s\n", strerror(errno));
      bShutdown = (1);
    }
  }
  fprintf(stderr, "Left the loop. Quitting.\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    119d <main+0x4d>
jmp    129d <main+0x14d>
movq   $0x0,-0x10028(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x16(%rbp),%rsi
mov    $0x2,%edx
call   1030 <read@plt>
mov    %rax,-0x10028(%rbp)
cmpq   $0x0,-0x10028(%rbp)
jle    125d <main+0x10d>
lea    -0x16(%rbp),%rdi
call   1050 <packet_size@plt>
mov    %rax,-0x10030(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0x10030(%rbp),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x10020(%rbp),%rsi
mov    -0x10030(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x10028(%rbp)
mov    -0x10028(%rbp),%rax
cmp    -0x10030(%rbp),%rax
je     1253 <main+0x103>
movl   $0x1,-0x14(%rbp)
jmp    1258 <main+0x108>
jmp    1258 <main+0x108>
jmp    1298 <main+0x148>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10034(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x10034(%rbp),%edi
mov    %rax,%rdx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
jmp    1187 <main+0x37>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    0x2e70(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe8d(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2e67(%rip),%rbp
lea    0xe(%rsp),%r12
lea    0xe87(%rip),%r14
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%rbp),%edi
call   1060 <fileno@plt>
mov    $0x2,%edx
mov    %eax,%edi
mov    %r12,%rsi
call   1030 <read@plt>
test   %rax,%rax
jle    11f4 <main+0xa4>
mov    %r12,%rdi
call   1050 <packet_size@plt>
mov    %rax,%rbx
mov    0x0(%r13),%edi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x0(%rbp),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    %rbx,%rax
je     11a0 <main+0x50>
jmp    1219 <main+0xc9>
mov    0x0(%r13),%ebx
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe18(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x0(%r13),%edi
lea    0xe0c(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Entering the loop.\0A\00"
@stdin = global i32 0
@.str.1 = [17 x i8] c"Packet size: %d\0A\00"
@.str.2 = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.3 = [26 x i8] c"Left the loop. Quitting.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [2 x i8]
%8 = alloca [65536 x i8]
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %46, %2
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
br i1 %16, label %17, label %47
17: ; preds = %13
store i64 0, i64* %9
%18 = load i32, i32* @stdin
%19 = call i32 @fileno(i32 %18)
%20 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%21 = call i64 @read(i32 %19, i8* %20, i64 2)
store i64 %21, i64* %9
%22 = load i64, i64* %9
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %17
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%26 = call i64 @packet_size(i8* %25)
store i64 %26, i64* %10
%27 = load i32, i32* @stderr
%28 = load i64, i64* %10
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %28)
%30 = load i32, i32* @stdin
%31 = call i32 @fileno(i32 %30)
%32 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
%33 = load i64, i64* %10
%34 = call i64 @read(i32 %31, i8* %32, i64 %33)
store i64 %34, i64* %9
%35 = load i64, i64* %9
%36 = load i64, i64* %10
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %24
store i32 1, i32* %6
br label %40
39: ; preds = %24
br label %40
40: ; preds = %39, %38
br label %46
41: ; preds = %17
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i32 1, i32* %6
br label %46
46: ; preds = %41, %40
br label %13
47: ; preds = %13
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Entering the loop.\0A\00"
@stdin = global i32 0
@.str.1 = [17 x i8] c"Packet size: %d\0A\00"
@.str.2 = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
@.str.3 = [26 x i8] c"Left the loop. Quitting.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i8]
%4 = alloca [65536 x i8]
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 0
br label %9
9: ; preds = %2, %19
%10 = load i32, i32* @stdin
%11 = call i32 @fileno(i32 %10)
%12 = call i64 @read(i32 %11, i8* %7, i64 2)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %27
19: ; preds = %9
%20 = call i64 @packet_size(i8* %7)
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %20)
%23 = load i32, i32* @stdin
%24 = call i32 @fileno(i32 %23)
%25 = call i64 @read(i32 %24, i8* %8, i64 %20)
%26 = icmp eq i64 %25, %20
br i1 %26, label %9, label %27
27: ; preds = %19, %14
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}