#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, int /*<<< orphan*/ *, struct addrinfo **);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  signal(SIGPIPE, SIG_IGN);
  if (argc < 3) {
    printf("Usage:\n");
    printf("trafficgen [TARGET] [PORT]\n");
    printf("\n");
    printf("This will send dummy data to target until the target "
           "closes connection.\n");
    return 0;
  }
  struct addrinfo *addr_result;
  int result = getaddrinfo(argv[1], argv[2], NULL, &addr_result);
  if (result != 0) {
    fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(result));
    return -1;
  }
  int s = socket(addr_result->ai_family, SOCK_STREAM, 0);
  if (s == -1) {
    perror("socket");
    return -1;
  }
  if (connect(s, addr_result->ai_addr, addr_result->ai_addrlen) != 0) {
    perror("connect");
    return -1;
  }
  int mytrue = 1;
  setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &mytrue, sizeof(int));
  char dummybuf[1024];
  memset(dummybuf, 'b', 1024);
  while (1) {
    int result = send(s, dummybuf, 1024, 0);
    if (result == -1 && errno == EINTR)
      continue;
    if (result <= 0)
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
cmpl   $0x3,-0x8(%rbp)
jge    1226 <main+0x76>
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
lea    0xe0b(%rip),%rdi
call   1050 <printf@plt>
lea    0xe62(%rip),%rdi
call   1050 <printf@plt>
lea    0xe0f(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   10c0 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1288 <main+0xd8>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0x438(%rbp),%edi
mov    %rax,%rdx
lea    0xdf6(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12c4 <main+0x114>
lea    0xdcd(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
je     12fb <main+0x14b>
lea    0xd9d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <main+0x1f2>
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2cd4(%rip),%rax
mov    (%rax),%esi
mov    0x2cd3(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x430(%rbp),%rdi
mov    $0x62,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x20(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0xffffffff,-0x434(%rbp)
jne    1384 <main+0x1d4>
mov    0x2c44(%rip),%rax
mov    (%rax),%rax
mov    0x2c32(%rip),%rcx
cmp    (%rcx),%rax
jne    1384 <main+0x1d4>
jmp    133c <main+0x18c>
cmpl   $0x0,-0x434(%rbp)
jg     1396 <main+0x1e6>
jmp    139b <main+0x1eb>
jmp    133c <main+0x18c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
cmp    $0x2,%ebp
jg     1220 <main+0x60>
lea    0xe2d(%rip),%rdi
call   1040 <puts@plt>
lea    0xe28(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe2d(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    125e <main+0x9e>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   10d0 <getaddrinfo@plt>
test   %eax,%eax
je     126c <main+0xac>
mov    0x2d89(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1070 <gai_strerror@plt>
lea    0xdb1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   10a0 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rax
mov    0x8(%rax),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     132c <main+0x16c>
mov    %eax,%ebx
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   10c0 <connect@plt>
test   %rax,%rax
jne    1335 <main+0x175>
movl   $0x1,0x4(%rsp)
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
mov    0x2d27(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x62,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    0x2cbd(%rip),%r15
mov    0x2cae(%rip),%rbp
jmp    130d <main+0x14d>
nopl   0x0(%rax)
mov    (%r15),%rax
cmp    0x0(%rbp),%rax
jne    121c <main+0x5c>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x140>
test   %eax,%eax
jg     130d <main+0x14d>
jmp    121c <main+0x5c>
lea    0xce0(%rip),%rdi
jmp    133c <main+0x17c>
lea    0xcde(%rip),%rdi
call   10b0 <perror@plt>
jmp    1259 <main+0x99>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [8 x i8] c"Usage:\0A\00"
@.str.1 = [28 x i8] c"trafficgen [TARGET] [PORT]\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [73 x i8] c"This will send dummy data to target until the target closes connection.\0A\00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"getaddrinfo(): %s\0A\00"
@SOCK_STREAM = global i32 0
@.str.5 = [7 x i8] c"socket\00"
@.str.6 = [8 x i8] c"connect\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @SIGPIPE
%13 = load i32, i32* @SIG_IGN
%14 = call i32 @signal(i32 %12, i32 %13)
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %83
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @getaddrinfo(i8* %25, i8* %28, i32* null, %struct.addrinfo** %6)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %22
%33 = load i32, i32* @stderr
%34 = load i32, i32* %7
%35 = call i8* @gai_strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %35)
store i32 -1, i32* %3
br label %83
37: ; preds = %22
%38 = load %struct.addrinfo*, %struct.addrinfo** %6
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = load i32, i32* @SOCK_STREAM
%42 = call i32 @socket(i32 %40, i32 %41, i32 0)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %83
47: ; preds = %37
%48 = load i32, i32* %8
%49 = load %struct.addrinfo*, %struct.addrinfo** %6
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load %struct.addrinfo*, %struct.addrinfo** %6
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i64 @connect(i32 %48, i32 %51, i32 %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %47
%58 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %83
59: ; preds = %47
store i32 1, i32* %9
%60 = load i32, i32* %8
%61 = load i32, i32* @IPPROTO_TCP
%62 = load i32, i32* @TCP_NODELAY
%63 = call i32 @setsockopt(i32 %60, i32 %61, i32 %62, i32* %9, i32 4)
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%65 = call i32 @memset(i8* %64, i8 signext 98, i32 1024)
br label %66
66: ; preds = %59, %76, %81
%67 = load i32, i32* %8
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%69 = call i32 @send(i32 %67, i8* %68, i32 1024, i32 0)
store i32 %69, i32* %11
%70 = load i32, i32* %11
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %77
72: ; preds = %66
%73 = load i64, i64* @errno
%74 = load i64, i64* @EINTR
%75 = icmp eq i64 %73, %74
br i1 %75, label %76, label %77
76: ; preds = %72
br label %66
77: ; preds = %72, %66
%78 = load i32, i32* %11
%79 = icmp sle i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %77
br label %82
81: ; preds = %77
br label %66
82: ; preds = %80
store i32 0, i32* %3
br label %83
83: ; preds = %82, %57, %45, %32, %17
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@stderr = global i32 0
@.str.4 = [19 x i8] c"getaddrinfo(): %s\0A\00"
@SOCK_STREAM = global i32 0
@.str.5 = [7 x i8] c"socket\00"
@.str.6 = [8 x i8] c"connect\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@str = [7 x i8] c"Usage:\00"
@str.7 = [27 x i8] c"trafficgen [TARGET] [PORT]\00"
@str.8 = [72 x i8] c"This will send dummy data to target until the target closes connection.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = load i32, i32* @SIGPIPE
%7 = load i32, i32* @SIG_IGN
%8 = tail call i32 @signal(i32 %6, i32 %7)
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
%13 = tail call i32 @putchar(i32 10)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @str.8, i64 0, i64 0))
br label %66
15: ; preds = %2
%16 = bitcast %struct.addrinfo** %3 to i8*
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @getaddrinfo(i8* %18, i8* %20, i32* null, %struct.addrinfo** %3)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %15
%24 = load i32, i32* @stderr
%25 = call i8* @gai_strerror(i32 %21)
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %25)
br label %64
27: ; preds = %15
%28 = load %struct.addrinfo*, %struct.addrinfo** %3
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%30 = load i32, i32* %29
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %64
36: ; preds = %27
%37 = load %struct.addrinfo*, %struct.addrinfo** %3
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i64 @connect(i32 %32, i32 %39, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %36
%45 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %64
46: ; preds = %36
%47 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%48 = load i32, i32* @IPPROTO_TCP
%49 = load i32, i32* @TCP_NODELAY
%50 = call i32 @setsockopt(i32 %32, i32 %48, i32 %49, i32* %4, i32 4)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%52 = call i32 @memset(i8* %51, i8 signext 98, i32 1024)
br label %53
53: ; preds = %62, %46
%54 = call i32 @send(i32 %32, i8* %51, i32 1024, i32 0)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i64, i64* @errno
%58 = load i64, i64* @EINTR
%59 = icmp eq i64 %57, %58
br i1 %59, label %62, label %63
60: ; preds = %53
%61 = icmp sgt i32 %54, 0
br i1 %61, label %62, label %63
62: ; preds = %60, %56
br label %53
63: ; preds = %56, %60
br label %64
64: ; preds = %34, %44, %63, %23
%65 = phi i32 [ -1, %23 ], [ -1, %34 ], [ -1, %44 ], [ 0, %63 ]
br label %66
66: ; preds = %64, %10
%67 = phi i32 [ 0, %10 ], [ %65, %64 ]
ret i32 %67
}