#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 atoi(char *);
int /*<<< orphan*/ memcpy(void *, void *, int);
int /*<<< orphan*/ printf(char *, char, ...);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int write(int, char *, int);
int ssds_send_len(int fd, char *sbuf, int slen, char *rbuf, int rlen) {
  int len;
  int mlen;
  int res;
  int ret;
  char snum[8];
  sprintf(snum, "%04d", slen);
  memcpy((void *)&sbuf[4], (void *)&snum[0], 4);
  res = write(fd, sbuf, slen);
  if (res == slen) {
    res = read(fd, rbuf, 8);
    if (res == 8) {
      memcpy((void *)&snum[0], (void *)&rbuf[4], 4);
      snum[4] = 0;
      mlen = atoi(snum);
      if (0) {
        printf(" Client len (%d) \n", len);
        printf(" Client rbuf %c %c %c %c %x %x %x %x\n", rbuf[0], rbuf[1],
               rbuf[2], rbuf[3], rbuf[4], rbuf[5], rbuf[6], rbuf[7]);
      }
      len = mlen;
      if (len > rlen)
        len = rlen;
      res = read(fd, &rbuf[8], len - 8);
      res += 8;
      rbuf[res] = 0;
      while (mlen > rlen) {
        ret = read(fd, snum, 8);
        if (ret > 0) {
          mlen -= ret;
        }
      }
    }
  } else {
    res = -1;
  }
  return res;
}


x86 assembly (clang, -O0):
<ssds_send_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
lea    -0x3c(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe88(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rdi
add    $0x4,%rdi
lea    -0x3c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    127f <ssds_send_len+0x12f>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1040 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x8,-0x30(%rbp)
jne    127a <ssds_send_len+0x12a>
lea    -0x3c(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movb   $0x0,-0x38(%rbp)
lea    -0x3c(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    120d <ssds_send_len+0xbd>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    $0x8,%rsi
mov    -0x28(%rbp),%edx
sub    $0x8,%edx
call   1040 <read@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    1275 <ssds_send_len+0x125>
mov    -0x4(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    $0x8,%edx
call   1040 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    1270 <ssds_send_len+0x120>
mov    -0x34(%rbp),%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
jmp    123b <ssds_send_len+0xeb>
jmp    127a <ssds_send_len+0x12a>
jmp    1286 <ssds_send_len+0x136>
movl   $0xffffffff,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssds_send_len>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %rcx,%r14
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edi,%r13d
lea    0xe90(%rip),%rsi
mov    %rsp,%r12
mov    %r12,%rdi
call   1070 <sprintf@plt>
lea    0x4(%rbp),%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    $0xffffffff,%r12d
cmp    %ebx,%eax
jne    122c <ssds_send_len+0xdc>
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1040 <read@plt>
mov    %eax,%r12d
cmp    $0x8,%eax
jne    122c <ssds_send_len+0xdc>
lea    0x4(%r14),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movb   $0x0,0x4(%rsp)
mov    %rbx,%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
cmp    %r15d,%eax
mov    %eax,%edx
cmovg  %r15d,%edx
lea    0x8(%r14),%rsi
add    $0xfffffff8,%edx
mov    %r13d,%edi
call   1040 <read@plt>
cltq
lea    0x8(%rax),%r12
movb   $0x0,0x8(%r14,%rax,1)
cmp    %r15d,%ebx
jle    122c <ssds_send_len+0xdc>
mov    %rsp,%r14
xor    %ebp,%ebp
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1040 <read@plt>
test   %eax,%eax
cmovs  %ebp,%eax
sub    %eax,%ebx
cmp    %r15d,%ebx
jg     1210 <ssds_send_len+0xc0>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%04d\00"
define i32 @ssds_send_len(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [8 x i8]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%16 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 0
%17 = load i32, i32* %8
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 4
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 0
%22 = call i32 @memcpy(i8* %20, i8* %21, i32 4)
%23 = load i32, i32* %6
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = call i32 @write(i32 %23, i8* %24, i32 %25)
store i32 %26, i32* %13
%27 = load i32, i32* %13
%28 = load i32, i32* %8
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %80
30: ; preds = %5
%31 = load i32, i32* %6
%32 = load i8*, i8** %9
%33 = call i32 @read(i32 %31, i8* %32, i32 8)
store i32 %33, i32* %13
%34 = load i32, i32* %13
%35 = icmp eq i32 %34, 8
br i1 %35, label %36, label %79
36: ; preds = %30
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 0
%38 = load i8*, i8** %9
%39 = getelementptr inbounds i8, i8* %38, i64 4
%40 = call i32 @memcpy(i8* %37, i8* %39, i32 4)
%41 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 4
store i8 0, i8* %41
%42 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 0
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %12
%44 = load i32, i32* %12
store i32 %44, i32* %11
%45 = load i32, i32* %11
%46 = load i32, i32* %10
%47 = icmp sgt i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %36
%49 = load i32, i32* %10
store i32 %49, i32* %11
br label %50
50: ; preds = %48, %36
%51 = load i32, i32* %6
%52 = load i8*, i8** %9
%53 = getelementptr inbounds i8, i8* %52, i64 8
%54 = load i32, i32* %11
%55 = sub nsw i32 %54, 8
%56 = call i32 @read(i32 %51, i8* %53, i32 %55)
store i32 %56, i32* %13
%57 = load i32, i32* %13
%58 = add nsw i32 %57, 8
store i32 %58, i32* %13
%59 = load i8*, i8** %9
%60 = load i32, i32* %13
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
store i8 0, i8* %62
br label %63
63: ; preds = %77, %50
%64 = load i32, i32* %12
%65 = load i32, i32* %10
%66 = icmp sgt i32 %64, %65
br i1 %66, label %67, label %78
67: ; preds = %63
%68 = load i32, i32* %6
%69 = getelementptr inbounds [8 x i8], [8 x i8]* %15, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 8)
store i32 %70, i32* %14
%71 = load i32, i32* %14
%72 = icmp sgt i32 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %67
%74 = load i32, i32* %14
%75 = load i32, i32* %12
%76 = sub nsw i32 %75, %74
store i32 %76, i32* %12
br label %77
77: ; preds = %73, %67
br label %63
78: ; preds = %63
br label %79
79: ; preds = %78, %30
br label %81
80: ; preds = %5
store i32 -1, i32* %13
br label %81
81: ; preds = %80, %79
%82 = load i32, i32* %13
ret i32 %82
}

LLVM IR (-O2):
@.str = [5 x i8] c"%04d\00"
define i32 @ssds_send_len(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca [8 x i8]
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%8 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %2)
%9 = getelementptr inbounds i8, i8* %1, i64 4
%10 = call i32 @memcpy(i8* %9, i8* %7, i32 4)
%11 = call i32 @write(i32 %0, i8* %1, i32 %2)
%12 = icmp eq i32 %11, %2
br i1 %12, label %13, label %36
13: ; preds = %5
%14 = call i32 @read(i32 %0, i8* %3, i32 8)
%15 = icmp eq i32 %14, 8
br i1 %15, label %16, label %36
16: ; preds = %13
%17 = getelementptr inbounds i8, i8* %3, i64 4
%18 = call i32 @memcpy(i8* %7, i8* %17, i32 4)
%19 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 4
store i8 0, i8* %19
%20 = call i32 @atoi(i8* %7)
%21 = icmp sgt i32 %20, %4
%22 = select i1 %21, i32 %4, i32 %20
%23 = getelementptr inbounds i8, i8* %3, i64 8
%24 = add nsw i32 %22, -8
%25 = call i32 @read(i32 %0, i8* %23, i32 %24)
%26 = add nsw i32 %25, 8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %3, i64 %27
store i8 0, i8* %28
br i1 %21, label %29, label %36
29: ; preds = %16, %29
%30 = phi i32 [ %34, %29 ], [ %20, %16 ]
%31 = call i32 @read(i32 %0, i8* %7, i32 8)
%32 = icmp sgt i32 %31, 0
%33 = select i1 %32, i32 %31, i32 0
%34 = sub nsw i32 %30, %33
%35 = icmp sgt i32 %34, %4
br i1 %35, label %29, label %36
36: ; preds = %29, %16, %5, %13
%37 = phi i32 [ %14, %13 ], [ -1, %5 ], [ %26, %16 ], [ %26, %29 ]
ret i32 %37
}