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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int main(void) {
  int sock;
  struct sockaddr_in addr;
  char sendbuf[1024] = {0};
  char recvbuf[1024] = {0};
  int retval = 0;
  if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
    printf("socket error\n");
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8888);
  addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  sprintf(sendbuf, "%s\n", "Hello World!");
  sendto(sock, sendbuf, sizeof(sendbuf), 0, (struct sockaddr *)&addr,
         sizeof(addr));
  retval = recvfrom(sock, recvbuf, sizeof(recvbuf), 0, NULL, NULL);
  if (retval == -1) {
    printf("recvfrom error!\n");
  } else if (retval > 0) {
    printf("%s\n", recvbuf);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
movl   $0x0,-0x824(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1205 <main+0x75>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x22b8,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    0xdd9(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
lea    0xdcd(%rip),%rsi
lea    0xdca(%rip),%rdx
call   10a0 <sprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x18(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    -0x8(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1090 <recvfrom@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0xffffffff,-0x824(%rbp)
jne    12bb <main+0x12b>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12e2 <main+0x152>
cmpl   $0x0,-0x824(%rbp)
jle    12dd <main+0x14d>
lea    -0x820(%rbp),%rsi
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12e2 <main+0x152>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x810,%rsp
lea    0x410(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1050 <memset@plt>
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11eb <main+0x5b>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xde1(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,(%rsp)
lea    0xddc(%rip),%rsi
lea    0xdd9(%rip),%rdx
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0xf8>
test   %eax,%eax
jle    1294 <main+0x104>
lea    0x10(%rsp),%rdi
jmp    128f <main+0xff>
lea    0xd8c(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x810,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [14 x i8] c"socket error\0A\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [13 x i8] c"Hello World!\00"
@.str.4 = [17 x i8] c"recvfrom error!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [1024 x i8]* %4 to i8*
%8 = bitcast [1024 x i8]* %5 to i8*
store i32 0, i32* %6
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 8888)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %19, i32* %20
%21 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%25 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* %2
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @sendto(i32 %26, i8* %27, i32 1024, i32 0, %struct.sockaddr* %28, i32 12)
%30 = load i32, i32* %2
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%32 = call i32 @recvfrom(i32 %30, i8* %31, i32 1024, i32 0, i32* null, i32* null)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %15
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %44
37: ; preds = %15
%38 = load i32, i32* %6
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %40, %37
br label %44
44: ; preds = %43, %35
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [13 x i8] c"Hello World!\00"
@str = [16 x i8] c"recvfrom error!\00"
@str.5 = [13 x i8] c"socket error\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 8888)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %17, i32* %18
%19 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%22 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%23 = call i32 @sendto(i32 %9, i8* %5, i32 1024, i32 0, %struct.sockaddr* %22, i32 12)
%24 = call i32 @recvfrom(i32 %9, i8* %6, i32 1024, i32 0, i32* null, i32* null)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %32
28: ; preds = %13
%29 = icmp sgt i32 %24, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = call i32 @puts(i8* %6)
br label %32
32: ; preds = %28, %30, %26
%33 = call i32 @close(i32 %9)
ret i32 0
}