#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 read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
size_t sock_read(int socket, char *data) {
  char buf[4096] = {0};
  char re[32] = {0};
  int num_r = read(socket, buf, 4096);
  if (num_r <= 0) {
    return num_r;
  }
  while (num_r != 4096) {
    int r = read(socket, buf + num_r, 4096 - num_r);
    if (r <= 0) {
      return r;
    }
    num_r += r;
  }
  sprintf(re, "ACK");
  write(socket, re, 32);
  snprintf(data, 4096, "%s", buf);
  return num_r;
}


x86 assembly (clang, -O0):
<sock_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
lea    -0x1040(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x1044(%rbp)
cmpl   $0x0,-0x1044(%rbp)
jg     11bf <sock_read+0x6f>
movslq -0x1044(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1280 <sock_read+0x130>
jmp    11c4 <sock_read+0x74>
cmpl   $0x1000,-0x1044(%rbp)
je     1232 <sock_read+0xe2>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
movslq -0x1044(%rbp),%rax
add    %rax,%rsi
mov    $0x1000,%edx
sub    -0x1044(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x1048(%rbp)
cmpl   $0x0,-0x1048(%rbp)
jg     121b <sock_read+0xcb>
movslq -0x1048(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1280 <sock_read+0x130>
mov    -0x1048(%rbp),%eax
add    -0x1044(%rbp),%eax
mov    %eax,-0x1044(%rbp)
jmp    11c4 <sock_read+0x74>
lea    -0x1040(%rbp),%rdi
lea    0xdc0(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0x1040(%rbp),%rsi
mov    $0x20,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1020(%rbp),%rcx
mov    $0x1000,%esi
lea    0xd94(%rip),%rdx
call   1040 <snprintf@plt>
movslq -0x1044(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1020,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x20(%rsp),%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11bb <sock_read+0x7b>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x1000,%ebx
je     11c0 <sock_read+0x80>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x20,%rsi
mov    $0x1000,%edx
sub    %ebx,%edx
mov    %ebp,%edi
call   1060 <read@plt>
add    %eax,%ebx
test   %eax,%eax
jg     1190 <sock_read+0x50>
cltq
jmp    11f4 <sock_read+0xb4>
movslq %ebx,%rax
jmp    11f4 <sock_read+0xb4>
movl   $0x4b4341,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x20,%edx
call   1030 <write@plt>
lea    0xe23(%rip),%rdx
lea    0x20(%rsp),%rcx
mov    %r14,%rdi
mov    $0x1000,%esi
call   1040 <snprintf@plt>
mov    $0x1000,%eax
add    $0x1020,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"ACK\00"
@.str.1 = [3 x i8] c"%s\00"
define i64 @sock_read(i32 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [4096 x i8]
%7 = alloca [32 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = bitcast [4096 x i8]* %6 to i8*
%11 = bitcast [32 x i8]* %7 to i8*
%12 = load i32, i32* %4
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%14 = call i32 @read(i32 %12, i8* %13, i32 4096)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp sle i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
store i64 %19, i64* %3
br label %53
20: ; preds = %2
br label %21
21: ; preds = %38, %20
%22 = load i32, i32* %8
%23 = icmp ne i32 %22, 4096
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = load i32, i32* %4
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i32, i32* %8
%31 = sub nsw i32 4096, %30
%32 = call i32 @read(i32 %25, i8* %29, i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
store i64 %37, i64* %3
br label %53
38: ; preds = %24
%39 = load i32, i32* %9
%40 = load i32, i32* %8
%41 = add nsw i32 %40, %39
store i32 %41, i32* %8
br label %21
42: ; preds = %21
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%44 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%45 = load i32, i32* %4
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%47 = call i32 @write(i32 %45, i8* %46, i32 32)
%48 = load i8*, i8** %5
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%50 = call i32 @snprintf(i8* %48, i32 4096, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %49)
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
store i64 %52, i64* %3
br label %53
53: ; preds = %42, %35, %17
%54 = load i64, i64* %3
ret i64 %54
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%s\00"
define i64 @sock_read(i32 %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = call i32 @read(i32 %0, i8* %5, i32 4096)
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = sext i32 %7 to i64
br label %27
11: ; preds = %2, %14
%12 = phi i32 [ %20, %14 ], [ %7, %2 ]
%13 = icmp eq i32 %12, 4096
br i1 %13, label %21, label %14
14: ; preds = %11
%15 = sext i32 %12 to i64
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %15
%17 = sub nsw i32 4096, %12
%18 = call i32 @read(i32 %0, i8* %16, i32 %17)
%19 = icmp slt i32 %18, 1
%20 = add nsw i32 %18, %12
br i1 %19, label %25, label %11
21: ; preds = %11
%22 = bitcast [32 x i8]* %4 to i32*
store i32 4932417, i32* %22
%23 = call i32 @write(i32 %0, i8* %6, i32 32)
%24 = call i32 @snprintf(i8* %1, i32 4096, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %27
25: ; preds = %14
%26 = sext i32 %18 to i64
br label %27
27: ; preds = %25, %21, %9
%28 = phi i64 [ %10, %9 ], [ 4096, %21 ], [ %26, %25 ]
ret i64 %28
}