#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*/ serv_addr;
typedef int /*<<< orphan*/ send_buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd = 0;
  char *send_buff;
  struct sockaddr_in serv_addr;
  if (argc != 3) {
    printf("\n Usage: %s <ip of server> <msg to send>\n", argv[0]);
    return 1;
  }
  send_buff = argv[2];
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("\n Error : Could not create socket \n");
    return 1;
  }
  memset(&serv_addr, '0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(5000);
  if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0) {
    printf("\n inet_pton error occured\n");
    return 1;
  }
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    printf("\n Error : Connect Failed \n");
    return 1;
  }
  int bytes = write(sockfd, send_buff, sizeof(send_buff));
  if (bytes < 0) {
    printf("\n Send error \n");
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11c8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ed <main+0x16d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1213 <main+0x93>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ed <main+0x16d>
lea    -0x30(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x1388,%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x30(%rbp),%rdx
call   1080 <inet_pton@plt>
cmp    $0x0,%rax
jg     127d <main+0xfd>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ed <main+0x16d>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    12b2 <main+0x132>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ed <main+0x16d>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12de <main+0x15e>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    124f <main+0xbf>
mov    0x10(%rbx),%r14
mov    0x2e35(%rip),%r12
mov    (%r12),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1262 <main+0xd2>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    $0x1388,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%edi
mov    0x8(%rbx),%rsi
mov    %r15,%rdx
call   1090 <inet_pton@plt>
test   %rax,%rax
jle    126b <main+0xdb>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     1274 <main+0xe4>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    1244 <main+0xb4>
lea    0xdeb(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    1285 <main+0xf5>
mov    (%rbx),%rsi
lea    0xda7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1280 <main+0xf0>
lea    0xe03(%rip),%rdi
jmp    127b <main+0xeb>
lea    0xde0(%rip),%rdi
jmp    127b <main+0xeb>
lea    0xdbd(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [42 x i8] c"\0A Usage: %s <ip of server> <msg to send>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [36 x i8] c"\0A Error : Could not create socket \0A\00"
@.str.2 = [27 x i8] c"\0A inet_pton error occured\0A\00"
@.str.3 = [27 x i8] c"\0A Error : Connect Failed \0A\00"
@.str.4 = [15 x i8] c"\0A Send error \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %60
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
store i8* %20, i8** %7
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %60
27: ; preds = %17
%28 = call i32 @memset(%struct.sockaddr_in* %8, i32 48, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @htons(i32 5000)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, 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* %8, i32 0, i32 0
%38 = call i64 @inet_pton(i32 %33, i8* %36, i32* %37)
%39 = icmp sle i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %27
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %60
42: ; preds = %27
%43 = load i32, i32* %6
%44 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%45 = call i64 @connect(i32 %43, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %60
49: ; preds = %42
%50 = load i32, i32* %6
%51 = load i8*, i8** %7
%52 = call i32 @write(i32 %50, i8* %51, i32 8)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %55, %49
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
store i32 0, i32* %3
br label %60
60: ; preds = %57, %47, %40, %25, %12
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [42 x i8] c"\0A Usage: %s <ip of server> <msg to send>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [14 x i8] c"\0A Send error \00"
@str.5 = [26 x i8] c"\0A Error : Connect Failed \00"
@str.6 = [26 x i8] c"\0A inet_pton error occured\00"
@str.7 = [35 x i8] c"\0A Error : Could not create socket \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %45
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.7, i64 0, i64 0))
br label %45
18: ; preds = %9
%19 = call i32 @memset(%struct.sockaddr_in* %3, i32 48, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 5000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%28 = call i64 @inet_pton(i32 %24, i8* %26, i32* %27)
%29 = icmp slt i64 %28, 1
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0))
br label %45
32: ; preds = %18
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @connect(i32 %14, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.5, i64 0, i64 0))
br label %45
38: ; preds = %32
%39 = call i32 @write(i32 %14, i8* %11, i32 8)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = call i32 @close(i32 %14)
br label %45
45: ; preds = %43, %36, %30, %16, %6
%46 = phi i32 [ 1, %6 ], [ 1, %16 ], [ 1, %30 ], [ 1, %36 ], [ 0, %43 ]
ret i32 %46
}