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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EOF;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void *receive_socket(void *sockfd_arg) {
  char buffer2[4096] = {0};
  int sockfd = (int)*((int *)sockfd_arg);
  ssize_t n = 0;
again:
  while ((n = read(sockfd, buffer2, 4096)) > 0) {
    buffer2[n] = '\0';
    if (fputs(buffer2, stdout) == EOF) {
      fprintf(stderr, "fputs() error.\n");
      exit(1);
    }
  }
  if (n < 0 && errno == EINTR)
    goto again;
  else if (n < 0)
    perror("failed to read data from server.server terminated prematurely.");
  return NULL;
}


x86 assembly (clang, -O0):
<receive_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1014(%rbp)
movq   $0x0,-0x1020(%rbp)
jmp    119e <receive_socket+0x3e>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x1020(%rbp)
cmp    $0x0,%rax
jbe    121e <receive_socket+0xbe>
mov    -0x1020(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
call   1030 <fputs@plt>
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
jne    1219 <receive_socket+0xb9>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    119e <receive_socket+0x3e>
cmpq   $0x0,-0x1020(%rbp)
jae    124b <receive_socket+0xeb>
mov    0x2d9d(%rip),%rax
mov    (%rax),%rax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
jne    124b <receive_socket+0xeb>
jmp    1199 <receive_socket+0x39>
cmpq   $0x0,-0x1020(%rbp)
jae    1265 <receive_socket+0x105>
lea    0xdb0(%rip),%rdi
call   1070 <perror@plt>
jmp    126a <receive_socket+0x10a>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbp
mov    %rsp,%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    0x0(%rbp),%ebp
mov    0x2e54(%rip),%r14
mov    0x2e65(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11d7 <receive_socket+0x87>
movb   $0x0,(%rsp,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1030 <fputs@plt>
cmp    (%r15),%rax
jne    1190 <receive_socket+0x40>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@EOF = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"fputs() error.\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [63 x i8] c"failed to read data from server.server terminated prematurely.\00"
define i8* @receive_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %2
%6 = bitcast [4096 x i8]* %3 to i8*
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
store i32 %9, i32* %4
store i64 0, i64* %5
br label %10
10: ; preds = %36, %1
br label %11
11: ; preds = %28, %10
%12 = load i32, i32* %4
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%14 = call i64 @read(i32 %12, i8* %13, i32 4096)
store i64 %14, i64* %5
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %11
%17 = load i64, i64* %5
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %17
store i8 0, i8* %18
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%20 = load i32, i32* @stdout
%21 = call i64 @fputs(i8* %19, i32 %20)
%22 = load i64, i64* @EOF
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
br label %11
29: ; preds = %11
%30 = load i64, i64* %5
%31 = icmp ult i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
br label %10
37: ; preds = %32, %29
%38 = load i64, i64* %5
%39 = icmp ult i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
br label %43
43: ; preds = %42
ret i8* null
}

LLVM IR (-O2):
@stdout = global i32 0
@EOF = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"fputs() error.\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define noalias i8* @receive_socket(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
br label %6
6: ; preds = %9, %1
%7 = call i64 @read(i32 %5, i8* %3, i32 4096)
%8 = icmp eq i64 %7, 0
br i1 %8, label %19, label %9
9: ; preds = %6
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %7
store i8 0, i8* %10
%11 = load i32, i32* @stdout
%12 = call i64 @fputs(i8* %3, i32 %11)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %6
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %6
ret i8* null
}