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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
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 /*<<< orphan*/);
int inet_pton(int /*<<< orphan*/, char *, void *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int client_fd, port, len;
  struct sockaddr_in server;
  char tx_buf[] = "Test message!\n";
  char rx_buf[256] = {0};
  if (argc < 3) {
    fprintf(stderr, "Usage: %s ip_addr port\n", argv[0]);
    exit(0);
  }
  server.sin_family = AF_INET;
  if (inet_pton(AF_INET, argv[1], (void *)&server.sin_addr.s_addr) != 1) {
    perror("Error converting IP address");
    exit(-1);
  }
  server.sin_port = htons(atoi(argv[2]));
  client_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (client_fd < 0) {
    perror("Error getting socket");
    exit(-1);
  }
  if (connect(client_fd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("Error connecting to address");
    close(client_fd);
    exit(-1);
  }
  len = write(client_fd, tx_buf, strlen(tx_buf));
  if (len < 0) {
    perror("Error writing to address");
    close(client_fd);
    exit(-1);
  }
  len = read(client_fd, rx_buf, 255);
  if (len < 0) {
    perror("Error reading from socket");
    close(client_fd);
    exit(-1);
  }
  printf("Reply received: %s\n", rx_buf);
  close(client_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe00(%rip),%rax
mov    %rax,-0x37(%rbp)
mov    0xdfe(%rip),%eax
mov    %eax,-0x2f(%rbp)
mov    0xdf8(%rip),%ax
mov    %ax,-0x2b(%rbp)
mov    0xdf0(%rip),%al
mov    %al,-0x29(%rbp)
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
cmpl   $0x3,-0x8(%rbp)
jge    1261 <main+0x81>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdba(%rip),%rsi
call   10a0 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   10b0 <inet_pton@plt>
cmp    $0x1,%eax
je     12aa <main+0xca>
lea    0xd8c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12fd <main+0x11d>
lea    0xd55(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0x0,%rax
jge    1336 <main+0x156>
lea    0xd39(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x37(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x37(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1392 <main+0x1b2>
lea    0xcf9(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xff,%edx
call   1090 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    13d1 <main+0x1f1>
lea    0xcd3(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>
lea    -0x140(%rbp),%rsi
lea    0xcc8(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x128,%rsp
mov    %rsi,%rbx
movabs $0xa216567617373,%rax
mov    %rax,0x17(%rsp)
movabs $0x73656d2074736554,%rax
6d 65 73
mov    %rax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
cmp    $0x2,%edi
jle    133d <main+0x16d>
mov    0x2d6c(%rip),%r14
mov    (%r14),%edi
mov    %edi,0x8(%rsp)
mov    0x8(%rbx),%rsi
lea    0x4(%rsp),%rdx
call   10a0 <inet_pton@plt>
cmp    $0x1,%eax
jne    135c <main+0x18c>
mov    0x10(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    (%r14),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1365 <main+0x195>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     137b <main+0x1ab>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1384 <main+0x1b4>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0xff,%edx
call   1080 <read@plt>
test   %eax,%eax
js     138d <main+0x1bd>
lea    0xd88(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xcbf(%rip),%rsi
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xcc4(%rip),%rdi
jmp    136c <main+0x19c>
lea    0xcd7(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xcd6(%rip),%rdi
jmp    1394 <main+0x1c4>
lea    0xce9(%rip),%rdi
jmp    1394 <main+0x1c4>
lea    0xcf9(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.tx_buf = [15 x i8] c"Test message!\0A\00"
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s ip_addr port\0A\00"
@AF_INET = global i32 0
@.str.1 = [28 x i8] c"Error converting IP address\00"
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"Error getting socket\00"
@.str.3 = [28 x i8] c"Error connecting to address\00"
@.str.4 = [25 x i8] c"Error writing to address\00"
@.str.5 = [26 x i8] c"Error reading from socket\00"
@.str.6 = [20 x i8] c"Reply received: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca [15 x i8]
%11 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [15 x i8]* %10 to i8*
%13 = bitcast [256 x i8]* %11 to i8*
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %2
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
%32 = bitcast i32* %31 to i8*
%33 = call i32 @inet_pton(i32 %26, i8* %29, i8* %32)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %38
35: ; preds = %23
%36 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %23
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* @AF_INET
%46 = load i32, i32* @SOCK_STREAM
%47 = call i32 @socket(i32 %45, i32 %46, i32 0)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %38
%51 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %38
%54 = load i32, i32* %6
%55 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%56 = call i64 @connect(i32 %54, %struct.sockaddr* %55, i32 12)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
%62 = call i32 @exit(i32 -1)
unreachable
63: ; preds = %53
%64 = load i32, i32* %6
%65 = getelementptr inbounds [15 x i8], [15 x i8]* %10, i64 0, i64 0
%66 = getelementptr inbounds [15 x i8], [15 x i8]* %10, i64 0, i64 0
%67 = call i32 @strlen(i8* %66)
%68 = call i32 @write(i32 %64, i8* %65, i32 %67)
store i32 %68, i32* %8
%69 = load i32, i32* %8
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %76
71: ; preds = %63
%72 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
%75 = call i32 @exit(i32 -1)
unreachable
76: ; preds = %63
%77 = load i32, i32* %6
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%79 = call i32 @read(i32 %77, i8* %78, i32 255)
store i32 %79, i32* %8
%80 = load i32, i32* %8
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %87
82: ; preds = %76
%83 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
%86 = call i32 @exit(i32 -1)
unreachable
87: ; preds = %76
%88 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%89 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %88)
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.tx_buf = [15 x i8] c"Test message!\0A\00"
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s ip_addr port\0A\00"
@AF_INET = global i32 0
@.str.1 = [28 x i8] c"Error converting IP address\00"
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"Error getting socket\00"
@.str.3 = [28 x i8] c"Error connecting to address\00"
@.str.4 = [25 x i8] c"Error writing to address\00"
@.str.5 = [26 x i8] c"Error reading from socket\00"
@.str.6 = [20 x i8] c"Reply received: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [15 x i8]
%5 = alloca [256 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%21 = bitcast i32* %20 to i8*
%22 = call i32 @inet_pton(i32 %16, i8* %19, i8* %21)
%23 = icmp eq i32 %22, 1
br i1 %23, label %27, label %24
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %15
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_STREAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %27
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i64 @connect(i32 %35, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @close(i32 %35)
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %40
%49 = call i32 @strlen(i8* %7)
%50 = call i32 @write(i32 %35, i8* %7, i32 %49)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %48
%53 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @close(i32 %35)
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %48
%57 = call i32 @read(i32 %35, i8* %8, i32 255)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%61 = call i32 @close(i32 %35)
%62 = call i32 @exit(i32 -1)
unreachable
63: ; preds = %56
%64 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %8)
%65 = call i32 @close(i32 %35)
ret i32 0
}