#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*/ si_other;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getline(char **, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, size_t *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, size_t);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int /*<<< orphan*/ strlen(char *);
int main(void) {
  struct sockaddr_in si_other;
  int sockHandle;
  size_t slen = sizeof si_other;
  size_t recv_len;
  char buf[512];
  char *message;
  size_t messageLen;
  char *s;
  if ((sockHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    die("socket");
  }
  message = (char *)malloc((512 + 1) * sizeof(char));
  memset(message, 0, 512 + 1);
  memset((char *)&si_other, 0, sizeof(si_other));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(8888);
  if (inet_aton("127.0.0.1", &si_other.sin_addr) == 0) {
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
  }
  while (1) {
    printf("Enter message : ");
    getline(&message, &messageLen, stdin);
    if ((s = strchr(message, '\n')) != NULL)
      *s = '\0';
    if (sendto(sockHandle, message, strlen(message), 0,
               (struct sockaddr *)&si_other, slen) == -1) {
      die("sendto()");
    }
    memset(buf, '\0', 512);
    if (recvfrom(sockHandle, buf, 512, 0, (struct sockaddr *)&si_other,
                 &recv_len) == -1) {
      die("recvfrom()");
    }
    printf("RX %d: [%s]\n", recv_len, buf);
  }
  close(sockHandle);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0xc,-0x20(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1232 <main+0x52>
lea    0xdd3(%rip),%rdi
call   10d0 <die@plt>
mov    $0x201,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1070 <memset@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
lea    0xd7d(%rip),%rdi
call   10a0 <inet_aton@plt>
cmp    $0x0,%rax
jne    12b8 <main+0xd8>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    12bd <main+0xdd>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edx
lea    -0x238(%rbp),%rdi
lea    -0x240(%rbp),%rsi
call   10e0 <getline@plt>
mov    -0x238(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x248(%rbp)
cmp    $0x0,%rax
je     1313 <main+0x133>
mov    -0x248(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x254(%rbp)
mov    -0x238(%rbp),%rax
mov    %rax,-0x250(%rbp)
mov    -0x238(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x254(%rbp),%edi
mov    -0x250(%rbp),%rsi
mov    %eax,%edx
lea    -0x10(%rbp),%r8
mov    -0x20(%rbp),%r9
xor    %ecx,%ecx
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1369 <main+0x189>
lea    0xcd2(%rip),%rdi
call   10d0 <die@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x10(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x28(%rbp),%r9
call   10c0 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    13af <main+0x1cf>
lea    0xc95(%rip),%rdi
call   10d0 <die@plt>
mov    -0x28(%rbp),%rsi
lea    -0x230(%rbp),%rdx
lea    0xc89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12bd <main+0xdd>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
mov    0x2df0(%rip),%r14
mov    (%r14),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1225 <main+0x45>
lea    0xde0(%rip),%rdi
call   10d0 <die@plt>
mov    $0x201,%edi
call   10b0 <malloc@plt>
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x201,%edx
call   1070 <memset@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xd94(%rip),%rdi
mov    %rbp,%rsi
call   10a0 <inet_aton@plt>
test   %rax,%rax
je     1367 <main+0x187>
lea    0x10(%rsp),%r13
lea    0x30(%rsp),%rbp
lea    0x20(%rsp),%r14
lea    0xdb0(%rip),%r15
jmp    12b2 <main+0xd2>
nopl   0x0(%rax)
mov    0x20(%rsp),%rsi
mov    %r15,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd6c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x28(%rsp),%rsi
call   10e0 <getline@plt>
mov    0x8(%rsp),%r12
mov    %r12,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12f7 <main+0x117>
movb   $0x0,(%rax)
mov    0x8(%rsp),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    $0xc,%r9d
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1327 <main+0x147>
lea    0xd14(%rip),%rdi
call   10d0 <die@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    %r14,%r9
call   10c0 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    12a0 <main+0xc0>
lea    0xce2(%rip),%rdi
call   10d0 <die@plt>
jmp    12a0 <main+0xc0>
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
lea    0xc9a(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"inet_aton() failed\0A\00"
@.str.3 = [17 x i8] c"Enter message : \00"
@stdin = global i32 0
@.str.4 = [9 x i8] c"sendto()\00"
@.str.5 = [11 x i8] c"recvfrom()\00"
@.str.6 = [13 x i8] c"RX %d: [%s]\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i32 0, i32* %1
store i64 12, i64* %4
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = call i64 @malloc(i32 513)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = call i32 @memset(i8* %20, i8 signext 0, i32 513)
%22 = bitcast %struct.sockaddr_in* %2 to i8*
%23 = call i32 @memset(i8* %22, i8 signext 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @htons(i32 8888)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%29 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %17
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %17
br label %36
36: ; preds = %35, %66
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @stdin
%39 = call i32 @getline(i8** %7, i64* %8, i32 %38)
%40 = load i8*, i8** %7
%41 = call i8* @strchr(i8* %40, i8 signext 10)
store i8* %41, i8** %9
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = load i8*, i8** %9
store i8 0, i8* %44
br label %45
45: ; preds = %43, %36
%46 = load i32, i32* %3
%47 = load i8*, i8** %7
%48 = load i8*, i8** %7
%49 = call i32 @strlen(i8* %48)
%50 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%51 = load i64, i64* %4
%52 = call i32 @sendto(i32 %46, i8* %47, i32 %49, i32 0, %struct.sockaddr* %50, i64 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %45
%55 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %45
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%58 = call i32 @memset(i8* %57, i8 signext 0, i32 512)
%59 = load i32, i32* %3
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%61 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%62 = call i32 @recvfrom(i32 %59, i8* %60, i32 512, i32 0, %struct.sockaddr* %61, i64* %5)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %56
%65 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %66
66: ; preds = %64, %56
%67 = load i64, i64* %5
%68 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i64 %67, i8* %68)
br label %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"inet_aton() failed\0A\00"
@.str.3 = [17 x i8] c"Enter message : \00"
@stdin = global i32 0
@.str.4 = [9 x i8] c"sendto()\00"
@.str.5 = [11 x i8] c"recvfrom()\00"
@.str.6 = [13 x i8] c"RX %d: [%s]\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i64
%3 = alloca [512 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = bitcast i64* %2 to i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%9 = bitcast i8** %4 to i8*
%10 = bitcast i64* %5 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = load i32, i32* @IPPROTO_UDP
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %0
%19 = tail call i64 @malloc(i32 513)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %4
%21 = tail call i32 @memset(i8* %20, i8 signext 0, i32 513)
%22 = call i32 @memset(i8* %6, i8 signext 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @htons(i32 8888)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%28 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %18
%31 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %36
32: ; preds = %18
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30, %58
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @stdin
%39 = call i32 @getline(i8** %4, i64* %5, i32 %38)
%40 = load i8*, i8** %4
%41 = call i8* @strchr(i8* dereferenceable(1) %40, i8 signext 10)
%42 = icmp eq i8* %41, null
br i1 %42, label %45, label %43
43: ; preds = %36
store i8 0, i8* %41
%44 = load i8*, i8** %4
br label %45
45: ; preds = %43, %36
%46 = phi i8* [ %44, %43 ], [ %40, %36 ]
%47 = call i32 @strlen(i8* %46)
%48 = call i32 @sendto(i32 %14, i8* %46, i32 %47, i32 0, %struct.sockaddr* %31, i64 12)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %45
%53 = call i32 @memset(i8* %8, i8 signext 0, i32 512)
%54 = call i32 @recvfrom(i32 %14, i8* %8, i32 512, i32 0, %struct.sockaddr* %31, i64* %2)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %56, %52
%59 = load i64, i64* %2
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i64 %59, i8* %8)
br label %36
}