#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 /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ ntohs(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 5) {
    fprintf(stderr, "usage: %s <IP> <port> <MBs> <bytes per>\n", argv[0]);
    return 1;
  }
  struct sockaddr_in servaddr;
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = ntohs(atoi(argv[2]));
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) != 1) {
    fprintf(stderr, "invalid IP address\n");
    return 1;
  }
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("cannot create socket");
  }
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
    perror("cannot connect");
    return 1;
  }
  int total = atoi(argv[3]) << 20;
  int unit = atoi(argv[4]);
  char buf[unit];
  int i;
  for (i = 0; i < unit; ++i) {
    buf[i] = 1;
  }
  int current;
  for (current = 0; current < total;) {
    int ret = send(sockfd, buf, unit, 0);
    if (ret < 0) {
      break;
    }
    current += ret;
  }
  close(sockfd);
  printf("sent %d bytes\n", current);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     11fa <main+0x4a>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b5 <main+0x205>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1080 <ntohs@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1070 <inet_pton@plt>
cmp    $0x1,%eax
je     1271 <main+0xc1>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b5 <main+0x205>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12a3 <main+0xf3>
lea    0xd9f(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
je     12d6 <main+0x126>
lea    0xd8d(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b5 <main+0x205>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
shl    $0x14,%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    134c <main+0x19c>
mov    -0x58(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1326 <main+0x176>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1390 <main+0x1e0>
mov    -0x58(%rbp),%rsi
mov    -0x24(%rbp),%edi
mov    -0x2c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1382 <main+0x1d2>
jmp    1390 <main+0x1e0>
mov    -0x4c(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    1353 <main+0x1a3>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x48(%rbp),%esi
lea    0xcbf(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
cmp    $0x5,%edi
jne    12ec <main+0x12c>
lea    -0x38(%rbp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2df3(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,-0x30(%rbp)
mov    0x10(%r14),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1090 <ntohs@plt>
mov    %eax,-0x34(%rbp)
mov    (%rbx),%edi
mov    0x8(%r14),%rsi
mov    %r15,%rdx
call   1080 <inet_pton@plt>
cmp    $0x1,%eax
jne    1308 <main+0x148>
mov    (%rbx),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
js     1333 <main+0x173>
lea    -0x38(%rbp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jne    1344 <main+0x184>
mov    0x18(%r14),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r12d
mov    0x20(%r14),%rdi
call   10c0 <atoi@plt>
mov    %rsp,-0x40(%rbp)
mov    %eax,%r14d
mov    %rsp,%r13
lea    0xf(%r14),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
test   %eax,%eax
jle    129c <main+0xdc>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
call   1050 <memset@plt>
shl    $0x14,%r12d
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ecx,%ebx
cmp    %r12d,%ecx
jge    12ce <main+0x10e>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
lea    (%rax,%rbx,1),%ecx
test   %eax,%eax
jns    12b0 <main+0xf0>
mov    %r15d,%edi
call   1060 <close@plt>
lea    0xd84(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %eax,%eax
mov    -0x40(%rbp),%rsp
jmp    1324 <main+0x164>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd01(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    131f <main+0x15f>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
lea    0xd11(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd03(%rip),%rdi
call   10a0 <perror@plt>
jmp    1241 <main+0x81>
lea    0xd07(%rip),%rdi
call   10a0 <perror@plt>
jmp    131f <main+0x15f>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [41 x i8] c"usage: %s <IP> <port> <MBs> <bytes per>\0A\00"
@AF_INET = global i32 0
@.str.1 = [20 x i8] c"invalid IP address\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"cannot create socket\00"
@.str.3 = [15 x i8] c"cannot connect\00"
@.str.4 = [15 x i8] c"sent %d bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 5
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %3
br label %105
23: ; preds = %2
%24 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
%31 = call i32 @ntohs(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @AF_INET
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%38 = call i32 @inet_pton(i32 %33, i8* %36, i32* %37)
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %43
40: ; preds = %23
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %105
43: ; preds = %23
%44 = load i32, i32* @AF_INET
%45 = load i32, i32* @SOCK_STREAM
%46 = call i32 @socket(i32 %44, i32 %45, i32 0)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %43
%52 = load i32, i32* %7
%53 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%54 = call i64 @connect(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %105
58: ; preds = %51
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 3
%61 = load i8*, i8** %60
%62 = call i32 @atoi(i8* %61)
%63 = shl i32 %62, 20
store i32 %63, i32* %8
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 4
%66 = load i8*, i8** %65
%67 = call i32 @atoi(i8* %66)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = zext i32 %68 to i64
%70 = call i8* @llvm.stacksave()
store i8* %70, i8** %10
%71 = alloca i8, i64 %69
store i64 %69, i64* %11
store i32 0, i32* %12
br label %72
72: ; preds = %80, %58
%73 = load i32, i32* %12
%74 = load i32, i32* %9
%75 = icmp slt i32 %73, %74
br i1 %75, label %76, label %83
76: ; preds = %72
%77 = load i32, i32* %12
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %71, i64 %78
store i8 1, i8* %79
br label %80
80: ; preds = %76
%81 = load i32, i32* %12
%82 = add nsw i32 %81, 1
store i32 %82, i32* %12
br label %72
83: ; preds = %72
store i32 0, i32* %13
br label %84
84: ; preds = %95, %83
%85 = load i32, i32* %13
%86 = load i32, i32* %8
%87 = icmp slt i32 %85, %86
br i1 %87, label %88, label %99
88: ; preds = %84
%89 = load i32, i32* %7
%90 = load i32, i32* %9
%91 = call i32 @send(i32 %89, i8* %71, i32 %90, i32 0)
store i32 %91, i32* %14
%92 = load i32, i32* %14
%93 = icmp slt i32 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %88
br label %99
95: ; preds = %88
%96 = load i32, i32* %14
%97 = load i32, i32* %13
%98 = add nsw i32 %97, %96
store i32 %98, i32* %13
br label %84
99: ; preds = %94, %84
%100 = load i32, i32* %7
%101 = call i32 @close(i32 %100)
%102 = load i32, i32* %13
%103 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %102)
store i32 0, i32* %3
%104 = load i8*, i8** %10
br label %105
105: ; preds = %99, %56, %40, %17
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [41 x i8] c"usage: %s <IP> <port> <MBs> <bytes per>\0A\00"
@AF_INET = global i32 0
@.str.1 = [20 x i8] c"invalid IP address\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"cannot create socket\00"
@.str.3 = [15 x i8] c"cannot connect\00"
@.str.4 = [15 x i8] c"sent %d bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp eq i32 %0, 5
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %67
9: ; preds = %2
%10 = bitcast %struct.sockaddr_in* %3 to i8*
%11 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = tail call i32 @ntohs(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%23 = call i32 @inet_pton(i32 %19, i8* %21, i32* %22)
%24 = icmp eq i32 %23, 1
br i1 %24, label %28, label %25
25: ; preds = %9
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %65
28: ; preds = %9
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%37 = call i64 @connect(i32 %31, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %65
41: ; preds = %35
%42 = getelementptr inbounds i8*, i8** %1, i64 3
%43 = load i8*, i8** %42
%44 = call i32 @atoi(i8* %43)
%45 = shl i32 %44, 20
%46 = getelementptr inbounds i8*, i8** %1, i64 4
%47 = load i8*, i8** %46
%48 = call i32 @atoi(i8* %47)
%49 = zext i32 %48 to i64
%50 = call i8* @llvm.stacksave()
%51 = alloca i8, i64 %49
%52 = icmp sgt i32 %48, 0
br i1 %52, label %53, label %54
53: ; preds = %41
br label %54
54: ; preds = %53, %41
br label %55
55: ; preds = %54, %58
%56 = phi i32 [ %61, %58 ], [ 0, %54 ]
%57 = icmp slt i32 %56, %45
br i1 %57, label %58, label %62
58: ; preds = %55
%59 = call i32 @send(i32 %31, i8* %51, i32 %48, i32 0)
%60 = icmp slt i32 %59, 0
%61 = add nsw i32 %59, %56
br i1 %60, label %62, label %55
62: ; preds = %58, %55
%63 = call i32 @close(i32 %31)
%64 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %56)
br label %65
65: ; preds = %39, %62, %25
%66 = phi i32 [ 1, %25 ], [ 1, %39 ], [ 0, %62 ]
br label %67
67: ; preds = %65, %5
%68 = phi i32 [ 1, %5 ], [ %66, %65 ]
ret i32 %68
}