#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 */
scalar_t__ socketWriteNoEnd(int, char *, int);
int /*<<< orphan*/ usleep(int);
int socketWriteByPackages(int new_fd, char *writebuff, int datalen,
                          int package_num, int time_interval) {
  int packages, residual;
  int i;
  packages = datalen / package_num;
  residual = datalen - packages * package_num;
  if (packages) {
    for (i = 0; i < packages; i++) {
      if (socketWriteNoEnd(new_fd, writebuff + i * package_num, package_num) <
          0)
        return -1;
      usleep(time_interval);
    }
  }
  if (socketWriteNoEnd(new_fd, writebuff + packages * package_num, residual) <
      0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<socketWriteByPackages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%ecx
imul   -0x18(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11bb <socketWriteByPackages+0x9b>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11b6 <socketWriteByPackages+0x96>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <socketWriteNoEnd@plt>
cmp    $0x0,%rax
jge    11a0 <socketWriteByPackages+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <socketWriteByPackages+0xd3>
mov    -0x1c(%rbp),%edi
call   1040 <usleep@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1163 <socketWriteByPackages+0x43>
jmp    11bb <socketWriteByPackages+0x9b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x24(%rbp),%edx
call   1030 <socketWriteNoEnd@plt>
cmp    $0x0,%rax
jge    11ec <socketWriteByPackages+0xcc>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <socketWriteByPackages+0xd3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketWriteByPackages>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %edx,%eax
mov    %rsi,0x10(%rsp)
mov    %edi,%ebp
cltd
idiv   %ecx
mov    %edx,0xc(%rsp)
mov    %eax,0x8(%rsp)
test   %eax,%eax
jle    1182 <socketWriteByPackages+0x62>
mov    %r8d,%r12d
movslq %ebx,%r14
mov    0x8(%rsp),%r13d
mov    0x10(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <socketWriteNoEnd@plt>
test   %rax,%rax
js     11a4 <socketWriteByPackages+0x84>
mov    %r12d,%edi
call   1040 <usleep@plt>
add    %r14,%r15
add    $0xffffffffffffffff,%r13
jne    1160 <socketWriteByPackages+0x40>
mov    0x8(%rsp),%eax
imul   %ebx,%eax
cltq
mov    0x10(%rsp),%rsi
add    %rax,%rsi
mov    %ebp,%edi
mov    0xc(%rsp),%edx
call   1030 <socketWriteNoEnd@plt>
sar    $0x3f,%rax
jmp    11a9 <socketWriteByPackages+0x89>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @socketWriteByPackages(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%15 = load i32, i32* %9
%16 = load i32, i32* %10
%17 = sdiv i32 %15, %16
store i32 %17, i32* %12
%18 = load i32, i32* %9
%19 = load i32, i32* %12
%20 = load i32, i32* %10
%21 = mul nsw i32 %19, %20
%22 = sub nsw i32 %18, %21
store i32 %22, i32* %13
%23 = load i32, i32* %12
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %49
25: ; preds = %5
store i32 0, i32* %14
br label %26
26: ; preds = %45, %25
%27 = load i32, i32* %14
%28 = load i32, i32* %12
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %48
30: ; preds = %26
%31 = load i32, i32* %7
%32 = load i8*, i8** %8
%33 = load i32, i32* %14
%34 = load i32, i32* %10
%35 = mul nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %32, i64 %36
%38 = load i32, i32* %10
%39 = call i64 @socketWriteNoEnd(i32 %31, i8* %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %30
store i32 -1, i32* %6
br label %62
42: ; preds = %30
%43 = load i32, i32* %11
%44 = call i32 @usleep(i32 %43)
br label %45
45: ; preds = %42
%46 = load i32, i32* %14
%47 = add nsw i32 %46, 1
store i32 %47, i32* %14
br label %26
48: ; preds = %26
br label %49
49: ; preds = %48, %5
%50 = load i32, i32* %7
%51 = load i8*, i8** %8
%52 = load i32, i32* %12
%53 = load i32, i32* %10
%54 = mul nsw i32 %52, %53
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %51, i64 %55
%57 = load i32, i32* %13
%58 = call i64 @socketWriteNoEnd(i32 %50, i8* %56, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %49
store i32 -1, i32* %6
br label %62
61: ; preds = %49
store i32 0, i32* %6
br label %62
62: ; preds = %61, %60, %41
%63 = load i32, i32* %6
ret i32 %63
}

LLVM IR (-O2):
define i32 @socketWriteByPackages(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = sdiv i32 %2, %3
%7 = mul nsw i32 %6, %3
%8 = srem i32 %2, %3
%9 = icmp sgt i32 %6, 0
br i1 %9, label %10, label %23
10: ; preds = %5
%11 = sext i32 %3 to i64
%12 = zext i32 %6 to i64
br label %13
13: ; preds = %10, %19
%14 = phi i64 [ 0, %10 ], [ %21, %19 ]
%15 = mul nsw i64 %14, %11
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = tail call i64 @socketWriteNoEnd(i32 %0, i8* %16, i32 %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %13
%20 = tail call i32 @usleep(i32 %4)
%21 = add nuw nsw i64 %14, 1
%22 = icmp eq i64 %21, %12
br i1 %22, label %23, label %13
23: ; preds = %19, %5
%24 = sext i32 %7 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
%26 = tail call i64 @socketWriteNoEnd(i32 %0, i8* %25, i32 %8)
%27 = ashr i64 %26, 63
%28 = trunc i64 %27 to i32
br label %29
29: ; preds = %13, %23
%30 = phi i32 [ %28, %23 ], [ -1, %13 ]
ret i32 %30
}