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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read_retry(int, char *, int);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
void tally(int sock) {
  setbuf(stdout, NULL);
  for (;;) {
    char act_msg[4];
    ssize_t read_size = read_retry(sock, act_msg, 4);
    if (read_size < 4) {
      if (read_size < 0)
        perror("ERROR: read");
      break;
    }
    if (act_msg[0])
      printf("TALLY: on\n");
    else
      printf("TALLY: off\n");
  }
}


x86 assembly (clang, -O0):
<tally>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read_retry@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    1196 <tally+0x56>
cmpl   $0x0,-0xc(%rbp)
jge    1191 <tally+0x51>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
jmp    11c2 <tally+0x82>
cmpb   $0x0,-0x8(%rbp)
je     11b1 <tally+0x71>
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
jmp    11bd <tally+0x7d>
lea    0xe5f(%rip),%rdi
call   1040 <printf@plt>
jmp    115d <tally+0x1d>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tally>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <read_retry@plt>
cmp    $0x4,%eax
jge    1180 <tally+0x40>
test   %eax,%eax
js     11c8 <tally+0x88>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe85(%rip),%r15
lea    0xe89(%rip),%r12
lea    0x4(%rsp),%r14
jmp    11b9 <tally+0x79>
cs nopw 0x0(%rax,%rax,1)
nop
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1060 <read_retry@plt>
cmp    $0x4,%eax
jl     1170 <tally+0x30>
cmpb   $0x0,0x4(%rsp)
mov    %r15,%rdi
je     11a0 <tally+0x60>
mov    %r12,%rdi
jmp    11a0 <tally+0x60>
lea    0xe31(%rip),%rdi
call   1050 <perror@plt>
jmp    1174 <tally+0x34>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [12 x i8] c"ERROR: read\00"
@.str.1 = [11 x i8] c"TALLY: on\0A\00"
@.str.2 = [12 x i8] c"TALLY: off\0A\00"
define void @tally(i32 %0) {
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* @stdout
%6 = call i32 @setbuf(i32 %5, i32* null)
br label %7
7: ; preds = %27, %1
%8 = load i32, i32* %2
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%10 = call i32 @read_retry(i32 %8, i8* %9, i32 4)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 4
br i1 %12, label %13, label %19
13: ; preds = %7
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
br label %28
19: ; preds = %7
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %27
25: ; preds = %19
%26 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %23
br label %7
28: ; preds = %18
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [12 x i8] c"ERROR: read\00"
@str = [11 x i8] c"TALLY: off\00"
@str.3 = [10 x i8] c"TALLY: on\00"
define void @tally(i32 %0) {
%2 = alloca [4 x i8]
%3 = load i32, i32* @stdout
%4 = tail call i32 @setbuf(i32 %3, i32* null)
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%6 = call i32 @read_retry(i32 %0, i8* %5, i32 4)
%7 = icmp slt i32 %6, 4
br i1 %7, label %8, label %13
8: ; preds = %13, %1
%9 = phi i32 [ %6, %1 ], [ %18, %13 ]
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %20
13: ; preds = %1, %13
%14 = load i8, i8* %5
%15 = icmp eq i8 %14, 0
%16 = select i1 %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @str.3, i64 0, i64 0)
%17 = call i32 @puts(i8* dereferenceable(1) %16)
%18 = call i32 @read_retry(i32 %0, i8* %5, i32 4)
%19 = icmp slt i32 %18, 4
br i1 %19, label %8, label %13
20: ; preds = %11, %8
ret void
}