#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 in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOF;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memset(void *, char, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ recvfrom(int, char *, int, int /*<<< orphan*/,
                            struct sockaddr *, int *);
scalar_t__ scanf(char *, char *);
int /*<<< orphan*/ sendto(int, char *, scalar_t__, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int main(int argc, char **argv) {
  int fd;
  const char *hname = "localhost";
  struct hostent *hostptr;
  struct sockaddr_in serveraddr, clientaddr;
  int addrlen;
  if (argc > 1) {
    hname = argv[1];
  }
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  hostptr = gethostbyname(hname);
  memset((void *)&serveraddr, '\0', sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr =
      ((struct in_addr *)(hostptr->h_addr_list[0]))->s_addr;
  serveraddr.sin_port = htons((unsigned short)59000);
  addrlen = sizeof(serveraddr);
  for (;;) {
    char inbuff[100];
    if (scanf("%s", inbuff) == EOF) {
      break;
    }
    inbuff[99] = '\0';
    sendto(fd, inbuff, strlen(inbuff) + 1, 0, (struct sockaddr *)&serveraddr,
           addrlen);
    char buffer[4 << 10];
    recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr *)&serveraddr,
             &addrlen);
    buffer[sizeof(buffer) - 1] = '\0';
    printf("%s\n", buffer);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe40(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11da <main+0x3a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0xe678,%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
movl   $0xc,-0x4c(%rbp)
lea    -0xb0(%rbp),%rsi
lea    0xdba(%rip),%rdi
call   10b0 <scanf@plt>
mov    0x2d84(%rip),%rcx
cmp    (%rcx),%rax
jne    126a <main+0xca>
jmp    12f1 <main+0x151>
movb   $0x0,-0x4d(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10bc(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0x10b8(%rbp)
lea    -0xb0(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x10bc(%rbp),%edi
mov    -0x10b8(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
lea    -0x38(%rbp),%r8
mov    -0x4c(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
mov    -0x14(%rbp),%edi
lea    -0x10b0(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0x1000,%edx
xor    %ecx,%ecx
lea    -0x4c(%rbp),%r9
call   10a0 <recvfrom@plt>
movb   $0x0,-0xb1(%rbp)
lea    -0x10b0(%rbp),%rsi
lea    0xd26(%rip),%rdi
call   1050 <printf@plt>
jmp    1242 <main+0xa2>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x10c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1098,%rsp
cmp    $0x2,%edi
jl     11bc <main+0x1c>
mov    0x8(%rsi),%rbx
jmp    11c3 <main+0x23>
lea    0xe3d(%rip),%rbx
mov    0x2e1e(%rip),%r15
mov    (%r15),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1090 <gethostbyname@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0xc,%ebp
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    (%rbx),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0xe678,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0xc,0xc(%rsp)
lea    0xdd9(%rip),%rdi
lea    0x20(%rsp),%rsi
call   10b0 <scanf@plt>
mov    0x2d9e(%rip),%rcx
cmp    (%rcx),%rax
jne    1263 <main+0xc3>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x1098,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%r15
lea    0x90(%rsp),%r12
lea    0xd8e(%rip),%r13
nopl   0x0(%rax)
movb   $0x0,0x83(%rsp)
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %r15,%r8
mov    %ebp,%r9d
call   1070 <sendto@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    0xc(%rsp),%r9
call   10a0 <recvfrom@plt>
movb   $0x0,0x108f(%rsp)
lea    0xd3d(%rip),%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   10b0 <scanf@plt>
mov    0x2cf6(%rip),%rcx
cmp    (%rcx),%rax
je     1247 <main+0xa7>
mov    0xc(%rsp),%ebp
jmp    1280 <main+0xe0>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@EOF = global i64 0
@.str.2 = [4 x i8] c"%s\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.hostent*
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca [100 x i8]
%13 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %7
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %7
br label %20
20: ; preds = %16, %2
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = load i8*, i8** %7
%25 = call %struct.hostent* @gethostbyname(i8* %24)
store %struct.hostent* %25, %struct.hostent** %8
%26 = bitcast %struct.sockaddr_in* %9 to i8*
%27 = call i32 @memset(i8* %26, i8 signext 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %28, i32* %29
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 0
%32 = load i64*, i64** %31
%33 = getelementptr inbounds i64, i64* %32, i64 0
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to %struct.in_addr*
%36 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = call i32 @htons(i16 zeroext -6536)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %40, i32* %41
store i32 12, i32* %11
br label %42
42: ; preds = %48, %20
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%44 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = load i64, i64* @EOF
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %42
br label %65
48: ; preds = %42
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 99
store i8 0, i8* %49
%50 = load i32, i32* %6
%51 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%53 = call i64 @strlen(i8* %52)
%54 = add nsw i64 %53, 1
%55 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%56 = load i32, i32* %11
%57 = call i32 @sendto(i32 %50, i8* %51, i64 %54, i32 0, %struct.sockaddr* %55, i32 %56)
%58 = load i32, i32* %6
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%60 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%61 = call i32 @recvfrom(i32 %58, i8* %59, i32 4096, i32 0, %struct.sockaddr* %60, i32* %11)
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 4095
store i8 0, i8* %62
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%64 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %63)
br label %42
65: ; preds = %47
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@EOF = global i64 0
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca [4096 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
br label %13
13: ; preds = %10, %2
%14 = phi i8* [ %12, %10 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = tail call %struct.hostent* @gethostbyname(i8* %14)
%19 = call i32 @memset(i8* %7, i8 signext 0, 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 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i64 0, i32 0
%23 = load i64*, i64** %22
%24 = load i64, i64* %23
%25 = inttoptr i64 %24 to %struct.in_addr*
%26 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %25, i64 0, i32 0
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %27, i32* %28
%29 = call i32 @htons(i16 zeroext -6536)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
store i32 12, i32* %4
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%32 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i64, i64* @EOF
%34 = icmp eq i64 %32, %33
br i1 %34, label %52, label %35
35: ; preds = %13
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 99
%37 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 4095
br label %40
40: ; preds = %50, %35
%41 = phi i32 [ %51, %50 ], [ 12, %35 ]
store i8 0, i8* %36
%42 = call i64 @strlen(i8* %31)
%43 = add nsw i64 %42, 1
%44 = call i32 @sendto(i32 %17, i8* %31, i64 %43, i32 0, %struct.sockaddr* %37, i32 %41)
%45 = call i32 @recvfrom(i32 %17, i8* %38, i32 4096, i32 0, %struct.sockaddr* %37, i32* %4)
store i8 0, i8* %39
%46 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%47 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%48 = load i64, i64* @EOF
%49 = icmp eq i64 %47, %48
br i1 %49, label %52, label %50
50: ; preds = %40
%51 = load i32, i32* %4
br label %40
52: ; preds = %40, %13
%53 = call i32 @close(i32 %17)
ret i32 0
}