#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 {
  scalar_t__ sin_port;
  TYPE_1__ sin_addr;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ srv_addr;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ perror(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, scalar_t__, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockd;
  struct sockaddr_in my_addr, srv_addr;
  char buf[100];
  int count;
  int addrlen;
  if (argc < 3) {
    fprintf(stderr, "Usage: %s ip_address port_number\n", argv[0]);
    exit(1);
  }
  sockd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockd == -1) {
    perror("Socket creation error");
    exit(1);
  }
  my_addr.sin_family = AF_INET;
  my_addr.sin_addr.s_addr = INADDR_ANY;
  my_addr.sin_port = 0;
  strcpy(buf, "Hello world\n");
  srv_addr.sin_family = AF_INET;
  inet_aton(argv[1], &srv_addr.sin_addr);
  srv_addr.sin_port = htons(atoi(argv[2]));
  sendto(sockd, buf, strlen(buf) + 1, 0, (struct sockaddr *)&srv_addr,
         sizeof(srv_addr));
  addrlen = sizeof(srv_addr);
  count = recvfrom(sockd, buf, 100, 0, (struct sockaddr *)&srv_addr, &addrlen);
  write(1, buf, count);
  close(sockd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1219 <main+0x49>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf6(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%rdi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1256 <main+0x86>
lea    0xddb(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0xb0(%rbp),%rdi
lea    0xdb2(%rip),%rsi
call   1030 <strcpy@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x48(%rbp),%rsi
add    $0x8,%rsi
call   10a0 <inet_aton@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xc4(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0xb0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xc4(%rbp),%edi
mov    -0xc0(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
lea    -0x48(%rbp),%r8
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1070 <sendto@plt>
movl   $0x18,-0xb8(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xb0(%rbp),%rsi
lea    -0x48(%rbp),%r8
mov    $0x64,%edx
xor    %ecx,%ecx
lea    -0xb8(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,-0xb4(%rbp)
lea    -0xb0(%rbp),%rsi
mov    -0xb4(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r15
cmp    $0x2,%edi
jle    12be <main+0xee>
mov    0x2df7(%rip),%r12
mov    (%r12),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     12e0 <main+0x110>
mov    %eax,%ebx
lea    0xe21(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    (%r12),%rax
mov    %rax,0x18(%rsp)
mov    0x8(%r15),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <inet_aton@plt>
mov    0x10(%r15),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
lea    0x8(%rsp),%r15
mov    %ebx,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0x18,%r9d
call   1070 <sendto@plt>
movl   $0x18,0x4(%rsp)
lea    0x4(%rsp),%r9
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   10b0 <recvfrom@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xd2f(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd3b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [34 x i8] c"Usage: %s ip_address port_number\0A\00"
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Socket creation error\00"
@INADDR_ANY = global i32 0
@.str.2 = [13 x i8] c"Hello world\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca [100 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
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 ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8*, i8** @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i8* %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = load i8*, i8** @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i8* %31, i8** %32
%33 = load i32, i32* @INADDR_ANY
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i64 0, i64* %36
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%38 = call i32 @strcpy(i8* %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%39 = load i8*, i8** @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i8* %39, i8** %40
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%45 = call i32 @inet_aton(i8* %43, %struct.TYPE_2__* %44)
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 2
%48 = load i8*, i8** %47
%49 = call i32 @atoi(i8* %48)
%50 = call i64 @htons(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i64 %50, i64* %51
%52 = load i32, i32* %6
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%55 = call i64 @strlen(i8* %54)
%56 = add nsw i64 %55, 1
%57 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%58 = call i32 @sendto(i32 %52, i8* %53, i64 %56, i32 0, %struct.sockaddr* %57, i32 24)
store i32 24, i32* %11
%59 = load i32, i32* %6
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%61 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%62 = call i32 @recvfrom(i32 %59, i8* %60, i32 100, i32 0, %struct.sockaddr* %61, i32* %11)
store i32 %62, i32* %10
%63 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%64 = load i32, i32* %10
%65 = call i32 @write(i32 1, i8* %63, i32 %64)
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [34 x i8] c"Usage: %s ip_address port_number\0A\00"
@AF_INET = global i8* null
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Socket creation error\00"
@INADDR_ANY = global i32 0
@.str.2 = [13 x i8] c"Hello world\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%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 ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8*, i8** @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i8* %16, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = load i8*, i8** @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i8* %25, i8** %26
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%30 = call i32 @inet_aton(i8* %28, %struct.TYPE_2__* %29)
%31 = getelementptr inbounds i8*, i8** %1, i64 2
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
%34 = call i64 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i64 %34, i64* %35
%36 = call i64 @strlen(i8* %7)
%37 = add nsw i64 %36, 1
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i32 @sendto(i32 %18, i8* %7, i64 %37, i32 0, %struct.sockaddr* %38, i32 24)
store i32 24, i32* %5
%40 = call i32 @recvfrom(i32 %18, i8* %7, i32 100, i32 0, %struct.sockaddr* %38, i32* %5)
%41 = call i32 @write(i32 1, i8* %7, i32 %40)
%42 = call i32 @close(i32 %18)
ret i32 0
}