#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct linger {
  int l_onoff;
  int /*<<< orphan*/ l_linger;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int MSG_DONTWAIT;
int MSG_NOSIGNAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_LINGER;
int atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct linger *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  char *addr;
  int port;
  int sock;
  struct sockaddr_in saddr;
  const struct linger nolinger = {.l_onoff = 1, .l_linger = 0};
  if (argc < 4) {
    fprintf(stderr,
            "usage : %s <addr> <port> <string>\n"
            "        This will connect to TCP port <addr>:<port> and send "
            "string <string>\n"
            "        then immediately reset.\n",
            argv[0]);
    exit(1);
  }
  addr = argv[1];
  port = atoi(argv[2]);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  bzero(&saddr, sizeof(saddr));
  saddr.sin_addr.s_addr = inet_addr(addr);
  saddr.sin_port = htons(port);
  saddr.sin_family = AF_INET;
  if (connect(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    perror("connect");
    exit(1);
  }
  send(sock, argv[3], strlen(argv[3]), MSG_DONTWAIT | MSG_NOSIGNAL);
  setsockopt(sock, SOL_SOCKET, SO_LINGER, (struct linger *)&nolinger,
             sizeof(struct linger));
  close(sock);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe13(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    1221 <main+0x51>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf6(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    -0x18(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    12bb <main+0xeb>
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
mov    0x2cce(%rip),%rax
mov    (%rax),%ecx
mov    0x2cbd(%rip),%rax
or     (%rax),%ecx
call   1060 <send@plt>
mov    -0x20(%rbp),%edi
mov    0x2ce4(%rip),%rax
mov    (%rax),%esi
mov    0x2ccb(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
movq   $0x1,0x10(%rsp)
cmp    $0x3,%edi
jg     1211 <main+0x41>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdfe(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x8(%rbx),%rbp
mov    0x10(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r15d
mov    0x2db8(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    %rbp,%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%r13),%eax
mov    %eax,(%rsp)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
jns    1299 <main+0xc9>
lea    0xe06(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x18(%rbx),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x2d0c(%rip),%rdx
mov    0x2cfd(%rip),%rcx
mov    (%rcx),%ecx
or     (%rdx),%ecx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <send@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %r14d,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.linger = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.nolinger = %struct.linger { i32 1, i32 0 }
@stderr = global i32 0
@.str = [144 x i8] c"usage : %s <addr> <port> <string>\0A This will connect to TCP port <addr>:<port> and send string <string>\0A then immediately reset.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@MSG_DONTWAIT = global i32 0
@MSG_NOSIGNAL = global i32 0
@SOL_SOCKET = global i32 0
@SO_LINGER = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.linger
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast %struct.linger* %10 to i8*
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 4
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([144 x i8], [144 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %6
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %7
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %8
%32 = call i32 @bzero(%struct.sockaddr_in* %9, i32 12)
%33 = load i8*, i8** %6
%34 = call i32 @inet_addr(i8* %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %7
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %38, i32* %39
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %8
%43 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%44 = call i64 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %21
%47 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %21
%50 = load i32, i32* %8
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 3
%53 = load i8*, i8** %52
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 3
%56 = load i8*, i8** %55
%57 = call i32 @strlen(i8* %56)
%58 = load i32, i32* @MSG_DONTWAIT
%59 = load i32, i32* @MSG_NOSIGNAL
%60 = or i32 %58, %59
%61 = call i32 @send(i32 %50, i8* %53, i32 %57, i32 %60)
%62 = load i32, i32* %8
%63 = load i32, i32* @SOL_SOCKET
%64 = load i32, i32* @SO_LINGER
%65 = call i32 @setsockopt(i32 %62, i32 %63, i32 %64, %struct.linger* %10, i32 8)
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
%68 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.linger = type { i32, i32 }
@stderr = global i32 0
@.str = [144 x i8] c"usage : %s <addr> <port> <string>\0A This will connect to TCP port <addr>:<port> and send string <string>\0A then immediately reset.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@MSG_DONTWAIT = global i32 0
@MSG_NOSIGNAL = global i32 0
@SOL_SOCKET = global i32 0
@SO_LINGER = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i64* %4 to i8*
store i64 1, i64* %4
%7 = icmp slt i32 %0, 4
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([144 x i8], [144 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%23 = tail call i32 @inet_addr(i8* %15)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %23, i32* %24
%25 = tail call i32 @htons(i32 %18)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i64 @connect(i32 %21, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %13
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %13
%36 = bitcast i64* %4 to %struct.linger*
%37 = getelementptr inbounds i8*, i8** %1, i64 3
%38 = load i8*, i8** %37
%39 = call i32 @strlen(i8* %38)
%40 = load i32, i32* @MSG_DONTWAIT
%41 = load i32, i32* @MSG_NOSIGNAL
%42 = or i32 %41, %40
%43 = call i32 @send(i32 %21, i8* %38, i32 %39, i32 %42)
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_LINGER
%46 = call i32 @setsockopt(i32 %21, i32 %44, i32 %45, %struct.linger* %36, i32 8)
%47 = call i32 @close(i32 %21)
%48 = call i32 @exit(i32 0)
unreachable
}