#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;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ length;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ help();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int write(int, ...);
int main(int argc, char **argv) {
  int num_msgs;
  int length;
  int sockfd, port, n;
  struct sockaddr_in serveraddr;
  struct hostent *server;
  char *server_ip;
  char *msg;
  if (argc != 5) {
    help();
    return 1;
  }
  server_ip = argv[1];
  port = atoi(argv[2]);
  msg = argv[3];
  num_msgs = atoi(argv[4]);
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) <= 0) {
    printf("Failed to open socket\n");
    return 1;
  }
  memset(&serveraddr, 0, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr = inet_addr(server_ip);
  serveraddr.sin_port = htons(port);
  if (connect(sockfd, (const struct sockaddr *)&serveraddr,
              sizeof(serveraddr)) < 0) {
    printf("Failed to connect\n");
    return 0;
  }
  int count = 0;
  length = strlen(msg) + 1;
  while (count < num_msgs) {
    if (write(sockfd, &length, sizeof(length)) != sizeof(length) ||
        write(sockfd, msg, length) != length) {
      printf("Failed to send message\n");
      return 1;
    }
    count++;
  }
  close(sockfd);
  return 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)
cmpl   $0x5,-0x8(%rbp)
je     11e3 <main+0x33>
mov    $0x0,%al
call   10a0 <help@plt>
movl   $0x1,-0x4(%rbp)
jmp    134e <main+0x19e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jg     1258 <main+0xa8>
lea    0xdb9(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134e <main+0x19e>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x40(%rbp),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    12be <main+0x10e>
lea    0xd6a(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134e <main+0x19e>
movl   $0x0,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    133f <main+0x18f>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
jne    1319 <main+0x169>
mov    -0x1c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     1331 <main+0x181>
lea    0xd0a(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134e <main+0x19e>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    12d4 <main+0x124>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x5,%edi
jne    12bd <main+0x10d>
mov    %rsi,%rbx
mov    0x8(%rsi),%r13
mov    0x10(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r12d
mov    0x18(%rbx),%r14
mov    0x20(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
jle    12c6 <main+0x116>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r13,%rdi
call   1090 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    %r12d,%edi
call   1060 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
js     12cf <main+0x11f>
mov    %r14,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,0x4(%rsp)
test   %ebp,%ebp
jle    12b1 <main+0x101>
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <write@plt>
cmp    $0x4,%eax
jne    12dd <main+0x12d>
mov    0x4(%rsp),%edx
mov    %ebx,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <write@plt>
cmp    0x4(%rsp),%eax
jne    12dd <main+0x12d>
add    $0xffffffff,%ebp
jne    1280 <main+0xd0>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %r15d,%r15d
jmp    12ef <main+0x13f>
xor    %eax,%eax
call   10a0 <help@plt>
jmp    12e9 <main+0x139>
lea    0xd5c(%rip),%rdi
jmp    12e4 <main+0x134>
lea    0xd41(%rip),%rdi
call   1030 <puts@plt>
jmp    12ef <main+0x13f>
lea    0xd1c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Failed to open socket\0A\00"
@.str.1 = [19 x i8] c"Failed to connect\0A\00"
@.str.2 = [24 x i8] c"Failed to send message\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 i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 5
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (...) @help()
store i32 1, i32* %3
br label %86
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %13
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %9
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 3
%30 = load i8*, i8** %29
store i8* %30, i8** %14
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 4
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %6
%35 = load i32, i32* @AF_INET
%36 = load i32, i32* @SOCK_STREAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %8
%38 = icmp sle i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %20
%40 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %86
41: ; preds = %20
%42 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%43 = load i32, i32* @AF_INET
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %43, i32* %44
%45 = load i8*, i8** %13
%46 = call i32 @inet_addr(i8* %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
store i32 %46, i32* %48
%49 = load i32, i32* %9
%50 = call i32 @htons(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %8
%53 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%54 = call i64 @connect(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %41
%57 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %86
58: ; preds = %41
store i32 0, i32* %15
%59 = load i8*, i8** %14
%60 = call i32 @strlen(i8* %59)
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %62
62: ; preds = %80, %58
%63 = load i32, i32* %15
%64 = load i32, i32* %6
%65 = icmp slt i32 %63, %64
br i1 %65, label %66, label %83
66: ; preds = %62
%67 = load i32, i32* %8
%68 = call i32 (i32, ...) @write(i32 %67, i32* %7, i64 4)
%69 = sext i32 %68 to i64
%70 = icmp ne i64 %69, 4
br i1 %70, label %78, label %71
71: ; preds = %66
%72 = load i32, i32* %8
%73 = load i8*, i8** %14
%74 = load i32, i32* %7
%75 = call i32 (i32, ...) @write(i32 %72, i8* %73, i32 %74)
%76 = load i32, i32* %7
%77 = icmp ne i32 %75, %76
br i1 %77, label %78, label %80
78: ; preds = %71, %66
%79 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %86
80: ; preds = %71
%81 = load i32, i32* %15
%82 = add nsw i32 %81, 1
store i32 %82, i32* %15
br label %62
83: ; preds = %62
%84 = load i32, i32* %8
%85 = call i32 @close(i32 %84)
store i32 0, i32* %3
br label %86
86: ; preds = %83, %78, %56, %39, %18
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [23 x i8] c"Failed to send message\00"
@str.3 = [18 x i8] c"Failed to connect\00"
@str.4 = [22 x i8] c"Failed to open socket\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 5
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 (...) @help()
br label %60
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 3
%17 = load i8*, i8** %16
%18 = getelementptr inbounds i8*, i8** %1, i64 4
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %10
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.4, i64 0, i64 0))
br label %60
27: ; preds = %10
%28 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @inet_addr(i8* %12)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %31, i32* %32
%33 = call i32 @htons(i32 %15)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i64 @connect(i32 %23, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %27
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.3, i64 0, i64 0))
br label %60
40: ; preds = %27
%41 = call i32 @strlen(i8* %17)
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
%43 = icmp sgt i32 %20, 0
br i1 %43, label %46, label %58
44: ; preds = %50
%45 = icmp eq i32 %55, %20
br i1 %45, label %58, label %46
46: ; preds = %40, %44
%47 = phi i32 [ %55, %44 ], [ 0, %40 ]
%48 = call i32 (i32, ...) @write(i32 %23, i32* %3, i64 4)
%49 = icmp eq i32 %48, 4
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = load i32, i32* %3
%52 = call i32 (i32, ...) @write(i32 %23, i8* %17, i32 %51)
%53 = load i32, i32* %3
%54 = icmp eq i32 %52, %53
%55 = add nuw nsw i32 %47, 1
br i1 %54, label %44, label %56
56: ; preds = %50, %46
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %60
58: ; preds = %44, %40
%59 = call i32 @close(i32 %23)
br label %60
60: ; preds = %56, %58, %38, %25, %8
%61 = phi i32 [ 1, %8 ], [ 1, %25 ], [ 0, %38 ], [ 1, %56 ], [ 0, %58 ]
ret i32 %61
}