#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 hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serveraddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
             struct sockaddr_in *, int *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr_in *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int sockfd, portno, n;
  int serverlen;
  struct sockaddr_in serveraddr;
  struct hostent *server;
  char *hostname;
  char buf[1024];
  if (argc != 3) {
    fprintf(stderr, "usage: %s <hostname> <port>\n", argv[0]);
    exit(0);
  }
  hostname = argv[1];
  portno = atoi(argv[2]);
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  server = gethostbyname(hostname);
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host as %s\n", hostname);
    exit(0);
  }
  bzero((char *)&serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr,
        server->h_length);
  serveraddr.sin_port = htons(portno);
  bzero(buf, 1024);
  printf("Please enter msg: ");
  fgets(buf, 1024, stdin);
  serverlen = sizeof(serveraddr);
  n = sendto(sockfd, buf, strlen(buf), 0, &serveraddr, serverlen);
  if (n < 0)
    error("ERROR in sendto");
  n = recvfrom(sockfd, buf, strlen(buf), 0, &serveraddr, &serverlen);
  if (n < 0)
    error("ERROR in recvfrom");
  printf("Echo from server: %s", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1226 <main+0x46>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde6(%rip),%rsi
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1274 <main+0x94>
lea    0xdae(%rip),%rdi
call   10c0 <error@plt>
mov    -0x40(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12ac <main+0xcc>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
lea    0xd92(%rip),%rsi
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   10b0 <bcopy@plt>
mov    -0x18(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
lea    -0x440(%rbp),%rdi
mov    $0x400,%esi
call   10d0 <bzero@plt>
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x440(%rbp),%rdi
mov    0x2cc7(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x44c(%rbp)
lea    -0x440(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x440(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x44c(%rbp),%edi
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
mov    -0x20(%rbp),%r9d
xor    %ecx,%ecx
lea    -0x30(%rbp),%r8
call   1060 <sendto@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1386 <main+0x1a6>
lea    0xcdf(%rip),%rdi
call   10c0 <error@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x45c(%rbp)
lea    -0x440(%rbp),%rax
mov    %rax,-0x458(%rbp)
lea    -0x440(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x45c(%rbp),%edi
mov    -0x458(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
lea    -0x30(%rbp),%r8
lea    -0x20(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    13e0 <main+0x200>
lea    0xc95(%rip),%rdi
call   10c0 <error@plt>
lea    -0x440(%rbp),%rsi
lea    0xc94(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x420,%rsp
cmp    $0x3,%edi
jne    1347 <main+0x167>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%rdi
call   10e0 <atoi@plt>
mov    %eax,%r15d
mov    0x2dd9(%rip),%r12
mov    (%r12),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1235 <main+0x55>
lea    0xded(%rip),%rdi
call   10c0 <error@plt>
mov    %r14,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     135c <main+0x17c>
mov    %rax,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rbp),%rsi
lea    0x14(%rsp),%rdi
mov    0x0(%rbp),%edx
call   10b0 <memmove@plt>
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
call   10d0 <bzero@plt>
lea    0xdb4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
movl   $0xc,0xc(%rsp)
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
test   %eax,%eax
jns    12ed <main+0x10d>
lea    0xd78(%rip),%rdi
call   10c0 <error@plt>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   10a0 <recvfrom@plt>
test   %eax,%eax
jns    1322 <main+0x142>
lea    0xd53(%rip),%rdi
call   10c0 <error@plt>
lea    0xd59(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xca6(%rip),%rsi
jmp    136f <main+0x18f>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
lea    0xcc6(%rip),%rsi
mov    %r14,%rdx
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s <hostname> <port>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@.str.2 = [27 x i8] c"ERROR, no such host as %s\0A\00"
@.str.3 = [19 x i8] c"Please enter msg: \00"
@stdin = global i32 0
@.str.4 = [16 x i8] c"ERROR in sendto\00"
@.str.5 = [18 x i8] c"ERROR in recvfrom\00"
@.str.6 = [21 x i8] c"Echo from server: %s\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 %struct.sockaddr_in
%11 = alloca %struct.hostent*
%12 = alloca i8*
%13 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne 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 ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
store i8* %26, i8** %12
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %7
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_DGRAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %23
%37 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %23
%39 = load i8*, i8** %12
%40 = call %struct.hostent* @gethostbyname(i8* %39)
store %struct.hostent* %40, %struct.hostent** %11
%41 = load %struct.hostent*, %struct.hostent** %11
%42 = icmp eq %struct.hostent* %41, null
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %12
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %38
%49 = bitcast %struct.sockaddr_in* %10 to i8*
%50 = call i32 @bzero(i8* %49, i32 12)
%51 = load i32, i32* @AF_INET
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %51, i32* %52
%53 = load %struct.hostent*, %struct.hostent** %11
%54 = getelementptr inbounds %struct.hostent, %struct.hostent* %53, i32 0, i32 1
%55 = load i64, i64* %54
%56 = inttoptr i64 %55 to i8*
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%58 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %57, i32 0, i32 0
%59 = bitcast i32* %58 to i8*
%60 = load %struct.hostent*, %struct.hostent** %11
%61 = getelementptr inbounds %struct.hostent, %struct.hostent* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @bcopy(i8* %56, i8* %59, i32 %62)
%64 = load i32, i32* %7
%65 = call i32 @htons(i32 %64)
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %65, i32* %66
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%68 = call i32 @bzero(i8* %67, i32 1024)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%71 = load i32, i32* @stdin
%72 = call i32 @fgets(i8* %70, i32 1024, i32 %71)
store i32 12, i32* %9
%73 = load i32, i32* %6
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%76 = call i32 @strlen(i8* %75)
%77 = load i32, i32* %9
%78 = call i32 @sendto(i32 %73, i8* %74, i32 %76, i32 0, %struct.sockaddr_in* %10, i32 %77)
store i32 %78, i32* %8
%79 = load i32, i32* %8
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %48
%82 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %83
83: ; preds = %81, %48
%84 = load i32, i32* %6
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%87 = call i32 @strlen(i8* %86)
%88 = call i32 @recvfrom(i32 %84, i8* %85, i32 %87, i32 0, %struct.sockaddr_in* %10, i32* %9)
store i32 %88, i32* %8
%89 = load i32, i32* %8
%90 = icmp slt i32 %89, 0
br i1 %90, label %91, label %93
91: ; preds = %83
%92 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %93
93: ; preds = %91, %83
%94 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%95 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %94)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
@stderr = global i32 0
@.str = [29 x i8] c"usage: %s <hostname> <port>\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@.str.2 = [27 x i8] c"ERROR, no such host as %s\0A\00"
@.str.3 = [19 x i8] c"Please enter msg: \00"
@stdin = global i32 0
@.str.4 = [16 x i8] c"ERROR in sendto\00"
@.str.5 = [18 x i8] c"ERROR in recvfrom\00"
@.str.6 = [21 x i8] c"Echo from server: %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [1024 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 3
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %15
%28 = tail call %struct.hostent* @gethostbyname(i8* %17)
%29 = icmp eq %struct.hostent* %28, null
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%33 = tail call i32 @exit(i32 0)
unreachable
34: ; preds = %27
%35 = call i32 @bzero(i8* %7, i32 12)
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i64 0, i32 1
%39 = load i64, i64* %38
%40 = inttoptr i64 %39 to i8*
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%42 = bitcast i32* %41 to i8*
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i64 0, i32 0
%44 = load i32, i32* %43
%45 = tail call i32 @htons(i32 %20)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %45, i32* %46
%47 = call i32 @bzero(i8* %8, i32 1024)
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* @stdin
%50 = call i32 @fgets(i8* %8, i32 1024, i32 %49)
store i32 12, i32* %3
%51 = call i32 @strlen(i8* %8)
%52 = call i32 @sendto(i32 %23, i8* %8, i32 %51, i32 0, %struct.sockaddr_in* %4, i32 12)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %34
%55 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %34
%57 = call i32 @strlen(i8* %8)
%58 = call i32 @recvfrom(i32 %23, i8* %8, i32 %57, i32 0, %struct.sockaddr_in* %4, i32* %3)
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %62
62: ; preds = %60, %56
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %8)
ret i32 0
}