#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_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int strlen(char *);
main(int argc, char **argv) {
  struct sockaddr_in addr;
  struct hostent *hp;
  int fd;
  int len;
  int sizeof_addr;
  char buf[512 * 2];
  int ret;
  if (argc != 2) {
    printf("Usage: udp_echo_client hostname\n");
    exit(1);
  }
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    exit(1);
  }
  if ((hp = gethostbyname(argv[1])) == NULL) {
    perror("gethostbyname");
    exit(1);
  }
  memset((void *)&addr, 0, sizeof(addr));
  memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(54321);
  printf("Enter message to AIBO>");
  while (fgets(buf, 512, stdin) != NULL) {
    len = strlen(buf) + 1;
    if (sendto(fd, buf, len, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
      perror("sendto");
      close(fd);
      exit(1);
    }
    sizeof_addr = sizeof(addr);
    ret = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr,
                   &sizeof_addr);
    if (ret < 0) {
      perror("recvfrom");
      close(fd);
      exit(1);
    }
    buf[ret] = '\0';
    printf("Receive from AIBO : %s", buf);
    printf("Enter message to AIBO>");
  }
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120b <main+0x3b>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1246 <main+0x76>
lea    0xdea(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1277 <main+0xa7>
lea    0xdc0(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10b0 <memcpy@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0xd431,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x440(%rbp),%rdi
mov    0x2d0a(%rip),%rax
mov    (%rax),%edx
mov    $0x200,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     13d1 <main+0x201>
lea    -0x440(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
cmp    $0x0,%rax
jge    1344 <main+0x174>
lea    0xd20(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
movl   $0xc,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x440(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   10c0 <recvfrom@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jge    139a <main+0x1ca>
lea    0xcd1(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
movslq -0x444(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rsi
lea    0xca6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xc71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12c8 <main+0xf8>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
cmp    $0x2,%edi
jne    1220 <main+0x40>
mov    %rsi,%rbx
mov    0x2de8(%rip),%r14
mov    (%r14),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
jns    1236 <main+0x56>
lea    0xde2(%rip),%rdi
jmp    124d <main+0x6d>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   10b0 <gethostbyname@plt>
test   %rax,%rax
jne    125c <main+0x7c>
lea    0xdba(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
lea    0x18(%rsp),%rdi
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
call   10c0 <memcpy@plt>
mov    (%r14),%eax
mov    %eax,0x14(%rsp)
mov    $0xd431,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0xd7a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rdi
mov    $0x200,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
je     1365 <main+0x185>
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xd63(%rip),%r12
lea    0xd35(%rip),%r13
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
test   %rax,%rax
js     1373 <main+0x193>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   10d0 <recvfrom@plt>
test   %eax,%eax
js     137c <main+0x19c>
mov    %eax,%eax
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c93(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x200,%esi
call   10a0 <fgets@plt>
test   %rax,%rax
jne    12e0 <main+0x100>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
lea    0xcb2(%rip),%rdi
jmp    1383 <main+0x1a3>
lea    0xcb0(%rip),%rdi
call   10e0 <perror@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [33 x i8] c"Usage: udp_echo_client hostname\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [14 x i8] c"gethostbyname\00"
@.str.3 = [23 x i8] c"Enter message to AIBO>\00"
@stdin = global i32 0
@.str.4 = [7 x i8] c"sendto\00"
@.str.5 = [9 x i8] c"recvfrom\00"
@.str.6 = [23 x i8] c"Receive from AIBO : %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call %struct.hostent* @gethostbyname(i8* %29)
store %struct.hostent* %30, %struct.hostent** %7
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %26
%36 = bitcast %struct.sockaddr_in* %6 to i8*
%37 = call i32 @memset(i8* %36, i32 0, i32 12)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%39 = load %struct.hostent*, %struct.hostent** %7
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 1
%41 = load i32, i32* %40
%42 = load %struct.hostent*, %struct.hostent** %7
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @memcpy(i32* %38, i32 %41, i32 %44)
%46 = load i32, i32* @AF_INET
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %46, i32* %47
%48 = call i32 @htons(i32 54321)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %48, i32* %49
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %83, %35
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%53 = load i32, i32* @stdin
%54 = call i32* @fgets(i8* %52, i32 512, i32 %53)
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %90
56: ; preds = %51
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%58 = call i32 @strlen(i8* %57)
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
%60 = load i32, i32* %8
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%62 = load i32, i32* %9
%63 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%64 = call i64 @sendto(i32 %60, i8* %61, i32 %62, i32 0, %struct.sockaddr* %63, i32 12)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %56
%67 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %56
store i32 12, i32* %10
%72 = load i32, i32* %8
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%74 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%75 = call i32 @recvfrom(i32 %72, i8* %73, i32 1024, i32 0, %struct.sockaddr* %74, i32* %10)
store i32 %75, i32* %12
%76 = load i32, i32* %12
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %71
%79 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%80 = load i32, i32* %8
%81 = call i32 @close(i32 %80)
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %71
%84 = load i32, i32* %12
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 %85
store i8 0, i8* %86
%87 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %87)
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %51
90: ; preds = %51
%91 = load i32, i32* %8
%92 = call i32 @close(i32 %91)
%93 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [14 x i8] c"gethostbyname\00"
@.str.3 = [23 x i8] c"Enter message to AIBO>\00"
@stdin = global i32 0
@.str.4 = [7 x i8] c"sendto\00"
@.str.5 = [9 x i8] c"recvfrom\00"
@.str.6 = [23 x i8] c"Receive from AIBO : %s\00"
@str = [32 x i8] c"Usage: udp_echo_client hostname\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call %struct.hostent* @gethostbyname(i8* %23)
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = call i32 @memset(i8* %6, i32 0, i32 12)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i32* %31, i32 %33, i32 %35)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %37, i32* %38
%39 = call i32 @htons(i32 54321)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %39, i32* %40
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%42 = load i32, i32* @stdin
%43 = call i32* @fgets(i8* %8, i32 512, i32 %42)
%44 = icmp eq i32* %43, null
br i1 %44, label %71, label %45
45: ; preds = %29
%46 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %47
47: ; preds = %45, %63
%48 = call i32 @strlen(i8* %8)
%49 = add nsw i32 %48, 1
%50 = call i64 @sendto(i32 %16, i8* %8, i32 %49, i32 0, %struct.sockaddr* %46, i32 12)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @close(i32 %16)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %47
store i32 12, i32* %4
%57 = call i32 @recvfrom(i32 %16, i8* %8, i32 1024, i32 0, %struct.sockaddr* %46, i32* %4)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%61 = call i32 @close(i32 %16)
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %56
%64 = zext i32 %57 to i64
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %64
store i8 0, i8* %65
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %8)
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%68 = load i32, i32* @stdin
%69 = call i32* @fgets(i8* %8, i32 512, i32 %68)
%70 = icmp eq i32* %69, null
br i1 %70, label %71, label %47
71: ; preds = %63, %29
%72 = call i32 @close(i32 %16)
%73 = call i32 @exit(i32 0)
unreachable
}