#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 */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
void read_and_print_reply(int sockfd) {
  int buff_size = 4096;
  char buffer[buff_size];
  int n = 1;
  while (n > 0) {
    bzero(buffer, buff_size);
    n = read(sockfd, buffer, buff_size);
    if (n < 0) {
      error("ERROR reading from socket");
    }
    if (n > 0) {
      buffer[n] = '\0';
      printf("%s", buffer);
    }
  }
}


x86 assembly (clang, -O0):
<read_and_print_reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1000,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11e4 <read_and_print_reply+0xa4>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1060 <bzero@plt>
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bd <read_and_print_reply+0x7d>
lea    0xe48(%rip),%rdi
call   1050 <error@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    11df <read_and_print_reply+0x9f>
mov    -0x28(%rbp),%rsi
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
jmp    117f <read_and_print_reply+0x3f>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_and_print_reply>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11b5 <read_and_print_reply+0x75>
lea    0xea3(%rip),%r14
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
test   %eax,%eax
je     11c1 <read_and_print_reply+0x81>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    1180 <read_and_print_reply+0x40>
lea    0xe44(%rip),%rdi
call   1050 <error@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"ERROR reading from socket\00"
@.str.1 = [3 x i8] c"%s\00"
define void @read_and_print_reply(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %2
store i32 4096, i32* %3
%7 = load i32, i32* %3
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
store i32 1, i32* %6
br label %11
11: ; preds = %32, %1
%12 = load i32, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %33
14: ; preds = %11
%15 = load i32, i32* %3
%16 = call i32 @bzero(i8* %10, i32 %15)
%17 = load i32, i32* %2
%18 = load i32, i32* %3
%19 = call i32 @read(i32 %17, i8* %10, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %14
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %10, i64 %29
store i8 0, i8* %30
%31 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %32
32: ; preds = %27, %24
br label %11
33: ; preds = %11
%34 = load i8*, i8** %4
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"ERROR reading from socket\00"
@.str.1 = [3 x i8] c"%s\00"
define void @read_and_print_reply(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i32 @bzero(i8* %3, i32 4096)
%5 = call i32 @read(i32 %0, i8* %3, i32 4096)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %12, %1
%8 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %19
9: ; preds = %1, %12
%10 = phi i32 [ %17, %12 ], [ %5, %1 ]
%11 = icmp eq i32 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %9
%13 = zext i32 %10 to i64
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%16 = call i32 @bzero(i8* %3, i32 4096)
%17 = call i32 @read(i32 %0, i8* %3, i32 4096)
%18 = icmp slt i32 %17, 0
br i1 %18, label %7, label %9
19: ; preds = %9, %7
ret void
}