#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int sbser_send(void *, unsigned char const *, size_t);
int sbser_sendall(void *_data, const unsigned char *msg, size_t size,
                  unsigned int timeout_ms) {
  static int nuser = 0;
  unsigned int sentbytes;
  double t1ms, t2ms, dtms, maxms;
  struct timeval tv1, tv2;
  nuser += 1;
  gettimeofday(&tv1, NULL);
  maxms = timeout_ms * 1e-3;
  t1ms = tv1.tv_sec + (tv1.tv_usec * 1e-6);
  sentbytes = 0;
  while (sentbytes < size) {
    assert(nuser == 1);
    int r = sbser_send(_data, msg + sentbytes, size - sentbytes);
    if (r > 0)
      sentbytes += r;
    gettimeofday(&tv2, NULL);
    t2ms = tv2.tv_sec + (tv2.tv_usec * 1e-6);
    dtms = t2ms - t1ms;
    if ((timeout_ms > 0) && (dtms >= maxms))
      break;
  }
  nuser -= 1;
  return (sentbytes == size) ? 0 : -1;
}


x86 assembly (clang, -O0):
<sbser_sendall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2eef(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ee6(%rip)
xor    %eax,%eax
mov    %eax,%esi
lea    -0x48(%rbp),%rdi
call   1030 <gettimeofday@plt>
mov    -0x1c(%rbp),%eax
cvtsi2sd %rax,%xmm0
movsd  0xe95(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
cvtsi2sdl -0x48(%rbp),%xmm1
cvtsi2sdl -0x44(%rbp),%xmm0
movsd  0xe72(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    1255 <sbser_sendall+0x125>
cmpl   $0x1,0x2e86(%rip)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%eax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%eax
sub    %rax,%rdx
call   1040 <sbser_send@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jle    11f6 <sbser_sendall+0xc6>
mov    -0x54(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
cvtsi2sdl -0x50(%rbp),%xmm1
cvtsi2sdl -0x4c(%rbp),%xmm0
movsd  0xdeb(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jbe    1250 <sbser_sendall+0x120>
movsd  -0x38(%rbp),%xmm0
ucomisd -0x40(%rbp),%xmm0
jb     1250 <sbser_sendall+0x120>
jmp    1255 <sbser_sendall+0x125>
jmp    11a2 <sbser_sendall+0x72>
mov    0x2de1(%rip),%eax
sub    $0x1,%eax
mov    %eax,0x2dd8(%rip)
mov    -0x20(%rbp),%eax
mov    %eax,%edx
mov    -0x18(%rbp),%rsi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sbser_sendall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r13d
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
addl   $0x1,0x2eeb(%rip)
xor    %ebp,%ebp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    $0x0,%r12d
test   %rbx,%rbx
je     1221 <sbser_sendall+0xf1>
mov    %r13d,%eax
cvtsi2sd %rax,%xmm0
mulsd  0xe82(%rip),%xmm0
movsd  %xmm0,0x20(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sdl 0x10(%rsp),%xmm0
cvtsi2sdl 0x14(%rsp),%xmm1
mulsd  0xe6d(%rip),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x18(%rsp)
xor    %r12d,%r12d
jmp    11b5 <sbser_sendall+0x85>
nopw   0x0(%rax,%rax,1)
cmp    %rbx,%r12
jae    1221 <sbser_sendall+0xf1>
xor    %edi,%edi
cmpl   $0x1,0x2e7e(%rip)
sete   %dil
call   1050 <assert@plt>
lea    (%r14,%r12,1),%rsi
mov    %rbx,%rdx
sub    %r12,%rdx
mov    %r15,%rdi
call   1040 <sbser_send@plt>
test   %eax,%eax
jns    11df <sbser_sendall+0xaf>
xor    %eax,%eax
add    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    %ebp,%r12d
test   %r13d,%r13d
je     11b0 <sbser_sendall+0x80>
xorps  %xmm0,%xmm0
cvtsi2sdl 0xc(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sdl 0x8(%rsp),%xmm1
mulsd  0xdf9(%rip),%xmm0
addsd  %xmm1,%xmm0
subsd  0x18(%rsp),%xmm0
ucomisd 0x20(%rsp),%xmm0
jb     11b0 <sbser_sendall+0x80>
addl   $0xffffffff,0x2e14(%rip)
xor    %eax,%eax
cmp    %rbx,%r12
setne  %al
neg    %eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@sbser_sendall.nuser = internal global i32 0
define i32 @sbser_sendall(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca %struct.timeval
%15 = alloca %struct.timeval
%16 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%17 = load i32, i32* @sbser_sendall.nuser
%18 = add nsw i32 %17, 1
store i32 %18, i32* @sbser_sendall.nuser
%19 = call i32 @gettimeofday(%struct.timeval* %14, i32* null)
%20 = load i32, i32* %8
%21 = uitofp i32 %20 to double
%22 = fmul double %21, 1.000000e-03
store double %22, double* %13
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
%24 = load i32, i32* %23
%25 = sitofp i32 %24 to double
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
%27 = load i32, i32* %26
%28 = sitofp i32 %27 to double
%29 = call double @llvm.fmuladd.f64(double %28, double 0x3EB0C6F7A0B5ED8D, double %25)
store double %29, double* %10
store i32 0, i32* %9
br label %30
30: ; preds = %75, %4
%31 = load i32, i32* %9
%32 = zext i32 %31 to i64
%33 = load i64, i64* %7
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %76
35: ; preds = %30
%36 = load i32, i32* @sbser_sendall.nuser
%37 = icmp eq i32 %36, 1
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i8*, i8** %5
%41 = load i8*, i8** %6
%42 = load i32, i32* %9
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i64, i64* %7
%46 = load i32, i32* %9
%47 = zext i32 %46 to i64
%48 = sub i64 %45, %47
%49 = call i32 @sbser_send(i8* %40, i8* %44, i64 %48)
store i32 %49, i32* %16
%50 = load i32, i32* %16
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %35
%53 = load i32, i32* %16
%54 = load i32, i32* %9
%55 = add i32 %54, %53
store i32 %55, i32* %9
br label %56
56: ; preds = %52, %35
%57 = call i32 @gettimeofday(%struct.timeval* %15, i32* null)
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%59 = load i32, i32* %58
%60 = sitofp i32 %59 to double
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%62 = load i32, i32* %61
%63 = sitofp i32 %62 to double
%64 = call double @llvm.fmuladd.f64(double %63, double 0x3EB0C6F7A0B5ED8D, double %60)
store double %64, double* %11
%65 = load double, double* %11
%66 = load double, double* %10
%67 = fsub double %65, %66
store double %67, double* %12
%68 = load i32, i32* %8
%69 = icmp ugt i32 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %56
%71 = load double, double* %12
%72 = load double, double* %13
%73 = fcmp oge double %71, %72
br i1 %73, label %74, label %75
74: ; preds = %70
br label %76
75: ; preds = %70, %56
br label %30
76: ; preds = %74, %30
%77 = load i32, i32* @sbser_sendall.nuser
%78 = sub nsw i32 %77, 1
store i32 %78, i32* @sbser_sendall.nuser
%79 = load i32, i32* %9
%80 = zext i32 %79 to i64
%81 = load i64, i64* %7
%82 = icmp eq i64 %80, %81
%83 = zext i1 %82 to i64
%84 = select i1 %82, i32 0, i32 -1
ret i32 %84
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@sbser_sendall.nuser = internal unnamed_addr global i32 0
define i32 @sbser_sendall(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = bitcast %struct.timeval* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = load i32, i32* @sbser_sendall.nuser
%10 = add nsw i32 %9, 1
store i32 %10, i32* @sbser_sendall.nuser
%11 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%12 = uitofp i32 %3 to double
%13 = fmul double %12, 1.000000e-03
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%15 = load i32, i32* %14
%16 = sitofp i32 %15 to double
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%18 = load i32, i32* %17
%19 = sitofp i32 %18 to double
%20 = tail call double @llvm.fmuladd.f64(double %19, double 0x3EB0C6F7A0B5ED8D, double %16)
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
%22 = icmp eq i64 %2, 0
br i1 %22, label %58, label %23
23: ; preds = %4
%24 = icmp eq i32 %3, 0
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
br label %26
26: ; preds = %54, %23
%27 = phi i64 [ 0, %23 ], [ %55, %54 ]
%28 = phi i32 [ 0, %23 ], [ %38, %54 ]
%29 = load i32, i32* @sbser_sendall.nuser
%30 = icmp eq i32 %29, 1
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = getelementptr inbounds i8, i8* %1, i64 %27
%34 = sub i64 %2, %27
%35 = tail call i32 @sbser_send(i8* %0, i8* %33, i64 %34)
%36 = icmp sgt i32 %35, 0
%37 = select i1 %36, i32 %35, i32 0
%38 = add i32 %37, %28
%39 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
br i1 %24, label %51, label %40
40: ; preds = %26
%41 = load i32, i32* %21
%42 = sitofp i32 %41 to double
%43 = load i32, i32* %25
%44 = sitofp i32 %43 to double
%45 = tail call double @llvm.fmuladd.f64(double %42, double 0x3EB0C6F7A0B5ED8D, double %44)
%46 = fsub double %45, %20
%47 = fcmp ult double %46, %13
%48 = zext i32 %38 to i64
%49 = icmp ult i64 %48, %2
%50 = select i1 %47, i1 %49, i1 false
br i1 %50, label %54, label %56
51: ; preds = %26
%52 = zext i32 %38 to i64
%53 = icmp ult i64 %52, %2
br i1 %53, label %54, label %56
54: ; preds = %51, %40
%55 = phi i64 [ %52, %51 ], [ %48, %40 ]
br label %26
56: ; preds = %40, %51
%57 = zext i32 %38 to i64
br label %58
58: ; preds = %56, %4
%59 = phi i64 [ 0, %4 ], [ %57, %56 ]
%60 = load i32, i32* @sbser_sendall.nuser
%61 = add nsw i32 %60, -1
store i32 %61, i32* @sbser_sendall.nuser
%62 = icmp ne i64 %59, %2
%63 = sext i1 %62 to i32
ret i32 %63
}