#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ errx(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fill_buffer(long *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int send(int, long *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ srandom(long);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void tcpstream_client(struct sockaddr_in sin, long seed) {
  long buffer[1024];
  ssize_t len;
  int i, j, sock;
  srandom(seed);
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock == -1)
    errx(-1, "socket: %s", strerror(errno));
  if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
    errx(-1, "connect: %s", strerror(errno));
  for (j = 0; j < 10240; j++) {
    for (i = 0; i < 1024; i++) {
      fill_buffer(buffer, i);
      len = send(sock, buffer, i * sizeof(long), 0);
      if (len == -1) {
        printf("%d bytes written of %d expected\n", len, i * sizeof(long));
        fflush(stdout);
        perror("send");
        goto done;
      }
    }
  }
done:
  close(sock);
}


x86 assembly (clang, -O0):
<tcpstream_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <srandom@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x2020(%rbp)
cmpl   $0xffffffff,-0x2020(%rbp)
jne    1218 <tcpstream_client+0x68>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%edx
mov    $0xffffffff,%edi
lea    0xded(%rip),%rsi
call   1050 <errx@plt>
mov    -0x2020(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1256 <tcpstream_client+0xa6>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %eax,%edx
mov    $0xffffffff,%edi
lea    0xdba(%rip),%rsi
call   1050 <errx@plt>
movl   $0x0,-0x201c(%rbp)
cmpl   $0x2800,-0x201c(%rbp)
jge    1340 <tcpstream_client+0x190>
movl   $0x0,-0x2018(%rbp)
cmpl   $0x400,-0x2018(%rbp)
jge    1327 <tcpstream_client+0x177>
lea    -0x2010(%rbp),%rdi
mov    -0x2018(%rbp),%esi
call   1040 <fill_buffer@plt>
mov    -0x2020(%rbp),%edi
lea    -0x2010(%rbp),%rsi
movslq -0x2018(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x2014(%rbp)
cmpl   $0xffffffff,-0x2014(%rbp)
jne    130e <tcpstream_client+0x15e>
mov    -0x2014(%rbp),%esi
movslq -0x2018(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
lea    0xd2d(%rip),%rdi
call   1070 <printf@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xd34(%rip),%rdi
call   10a0 <perror@plt>
jmp    1345 <tcpstream_client+0x195>
jmp    1313 <tcpstream_client+0x163>
mov    -0x2018(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2018(%rbp)
jmp    127a <tcpstream_client+0xca>
jmp    132c <tcpstream_client+0x17c>
mov    -0x201c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x201c(%rbp)
jmp    1260 <tcpstream_client+0xb0>
jmp    1345 <tcpstream_client+0x195>
mov    -0x2020(%rbp),%edi
call   1080 <close@plt>
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpstream_client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2010,%rsp
mov    %edi,0x8(%rsp)
mov    %rsi,%rdi
call   1030 <srandom@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    120d <tcpstream_client+0x5d>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdff(%rip),%rsi
mov    $0xffffffff,%edi
mov    %eax,%edx
call   1050 <errx@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x4,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1245 <tcpstream_client+0x95>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdd2(%rip),%rsi
mov    $0xffffffff,%edi
mov    %eax,%edx
call   1050 <errx@plt>
xor    %r12d,%r12d
lea    0x10(%rsp),%r15
nopl   (%rax)
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %ebx,%esi
call   1040 <fill_buffer@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
je     129b <tcpstream_client+0xeb>
add    $0x1,%ebx
add    $0x8,%ebp
cmp    $0x400,%ebx
jne    1260 <tcpstream_client+0xb0>
add    $0x1,%r12d
cmp    $0x2800,%r12d
jne    1250 <tcpstream_client+0xa0>
jmp    12c8 <tcpstream_client+0x118>
lea    0xd75(%rip),%rdi
mov    $0xffffffff,%esi
mov    %ebp,%edx
call   1070 <printf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xd75(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1080 <close@plt>
add    $0x2010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socket: %s\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"connect: %s\00"
@.str.2 = [33 x i8] c"%d bytes written of %d expected\0A\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"send\00"
define void @tcpstream_client(i32 %0, i64 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = alloca [1024 x i64]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %0, i32* %10
store i64 %1, i64* %4
%11 = load i64, i64* %4
%12 = call i32 @srandom(i64 %11)
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %2
%23 = load i32, i32* %9
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @connect(i32 %23, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %22
store i32 0, i32* %8
br label %32
32: ; preds = %67, %31
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 10240
br i1 %34, label %35, label %70
35: ; preds = %32
store i32 0, i32* %7
br label %36
36: ; preds = %63, %35
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 1024
br i1 %38, label %39, label %66
39: ; preds = %36
%40 = getelementptr inbounds [1024 x i64], [1024 x i64]* %5, i64 0, i64 0
%41 = load i32, i32* %7
%42 = call i32 @fill_buffer(i64* %40, i32 %41)
%43 = load i32, i32* %9
%44 = getelementptr inbounds [1024 x i64], [1024 x i64]* %5, i64 0, i64 0
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = mul i64 %46, 8
%48 = trunc i64 %47 to i32
%49 = call i32 @send(i32 %43, i64* %44, i32 %48, i32 0)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %62
52: ; preds = %39
%53 = load i32, i32* %6
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = mul i64 %55, 8
%57 = trunc i64 %56 to i32
%58 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %53, i32 %57)
%59 = load i32, i32* @stdout
%60 = call i32 @fflush(i32 %59)
%61 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %71
62: ; preds = %39
br label %63
63: ; preds = %62
%64 = load i32, i32* %7
%65 = add nsw i32 %64, 1
store i32 %65, i32* %7
br label %36
66: ; preds = %36
br label %67
67: ; preds = %66
%68 = load i32, i32* %8
%69 = add nsw i32 %68, 1
store i32 %69, i32* %8
br label %32
70: ; preds = %32
br label %71
71: ; preds = %70, %52
%72 = load i32, i32* %9
%73 = call i32 @close(i32 %72)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socket: %s\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"connect: %s\00"
@.str.2 = [33 x i8] c"%d bytes written of %d expected\0A\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"send\00"
define void @tcpstream_client(i32 %0, i64 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i64]
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %0, i32* %5
%6 = bitcast [1024 x i64]* %4 to i8*
%7 = tail call i32 @srandom(i64 %1)
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @errx(i32 -1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %2
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i32 @connect(i32 %10, %struct.sockaddr* %17, i32 4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %20, %16
%25 = getelementptr inbounds [1024 x i64], [1024 x i64]* %4, i64 0, i64 0
br label %26
26: ; preds = %24, %42
%27 = phi i32 [ 0, %24 ], [ %43, %42 ]
br label %31
28: ; preds = %31
%29 = add nuw nsw i32 %32, 1
%30 = icmp eq i32 %29, 1024
br i1 %30, label %42, label %31
31: ; preds = %26, %28
%32 = phi i32 [ 0, %26 ], [ %29, %28 ]
%33 = call i32 @fill_buffer(i64* %25, i32 %32)
%34 = shl nuw nsw i32 %32, 3
%35 = call i32 @send(i32 %10, i64* %25, i32 %34, i32 0)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %28
37: ; preds = %31
%38 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 -1, i32 %34)
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %45
42: ; preds = %28
%43 = add nuw nsw i32 %27, 1
%44 = icmp eq i32 %43, 10240
br i1 %44, label %45, label %26
45: ; preds = %42, %37
%46 = call i32 @close(i32 %10)
ret void
}