#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 /*<<< orphan*/ free(char *);
int getpid();
scalar_t__ malloc(long);
int random();
int /*<<< orphan*/ write(int, char *, long);
void net_generate_and_write(int sock, long chunk_size,
                            unsigned long long size) {
  int i, ii;
  char *str;
  unsigned long long num_calls = 0;
  num_calls = (size / chunk_size);
  if (num_calls * chunk_size < size)
    num_calls++;
  str = (char *)malloc(chunk_size * sizeof(char));
  for (i = 0; i < num_calls; i++) {
    for (ii = 0; ii < chunk_size; ii++)
      str[ii] = (random() + getpid()) % 256;
    write(sock, str, chunk_size);
  }
  free(str);
}


x86 assembly (clang, -O0):
<net_generate_and_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
imul   -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119a <net_generate_and_write+0x4a>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    122c <net_generate_and_write+0xdc>
movl   $0x0,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    120e <net_generate_and_write+0xbe>
mov    $0x0,%al
call   1060 <random@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c7 <net_generate_and_write+0x77>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b2 <net_generate_and_write+0x62>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_generate_and_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rcx
mov    %rsi,%r12
mov    %edi,0x4(%rsp)
mov    %rdx,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     117e <net_generate_and_write+0x2e>
mov    %rcx,%rax
xor    %edx,%edx
div    %r12
mov    %rax,%r15
jmp    1188 <net_generate_and_write+0x38>
mov    %ecx,%eax
xor    %edx,%edx
div    %r12d
mov    %eax,%r15d
mov    %r15,%rax
imul   %r12,%rax
xor    %r13d,%r13d
cmp    %rcx,%rax
setb   %r13b
mov    %r12,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbp
add    %r15,%r13
je     11f2 <net_generate_and_write+0xa2>
xor    %r15d,%r15d
jmp    11c8 <net_generate_and_write+0x78>
xchg   %ax,%ax
mov    0x4(%rsp),%edi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1040 <write@plt>
add    $0x1,%r15
cmp    %r13,%r15
je     11f2 <net_generate_and_write+0xa2>
test   %r12,%r12
jle    11b0 <net_generate_and_write+0x60>
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <random@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
add    %ebx,%eax
mov    %al,0x0(%rbp,%r14,1)
add    $0x1,%r14
cmp    %r14,%r12
jne    11d0 <net_generate_and_write+0x80>
jmp    11b0 <net_generate_and_write+0x60>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

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

LLVM IR (-O2):
define void @net_generate_and_write(i32 %0, i64 %1, i64 %2) {
%4 = udiv i64 %2, %1
%5 = mul i64 %4, %1
%6 = icmp ult i64 %5, %2
%7 = zext i1 %6 to i64
%8 = add i64 %4, %7
%9 = tail call i64 @malloc(i64 %1)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %8, 0
br i1 %11, label %29, label %12
12: ; preds = %3
%13 = icmp sgt i64 %1, 0
br label %14
14: ; preds = %12, %25
%15 = phi i64 [ 0, %12 ], [ %27, %25 ]
br i1 %13, label %16, label %25
16: ; preds = %14, %16
%17 = phi i64 [ %23, %16 ], [ 0, %14 ]
%18 = tail call i32 (...) @random()
%19 = tail call i32 (...) @getpid()
%20 = add nsw i32 %19, %18
%21 = trunc i32 %20 to i8
%22 = getelementptr inbounds i8, i8* %10, i64 %17
store i8 %21, i8* %22
%23 = add nuw nsw i64 %17, 1
%24 = icmp eq i64 %23, %1
br i1 %24, label %25, label %16
25: ; preds = %16, %14
%26 = tail call i32 @write(i32 %0, i8* %10, i64 %1)
%27 = add nuw i64 %15, 1
%28 = icmp eq i64 %27, %8
br i1 %28, label %29, label %14
29: ; preds = %25, %3
%30 = tail call i32 @free(i8* %10)
ret void
}