#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 scalar_t__ ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ local;
typedef int /*<<< orphan*/ client;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                    int *);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  if (argc != 3) {
    print(argv[0]);
    return -1;
  }
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    perror("socket");
    return 1;
  }
  struct sockaddr_in local;
  local.sin_family = AF_INET;
  local.sin_port = htons(atoi(argv[2]));
  local.sin_addr.s_addr = inet_addr(argv[1]);
  socklen_t addrlen = sizeof(local);
  if (bind(sock, (struct sockaddr *)&local, addrlen) < 0) {
    perror("bind");
    return 2;
  }
  struct sockaddr_in client;
  socklen_t len = sizeof(client);
  char buf[1024];
  while (1) {
    memset(buf, '\0', sizeof(buf));
    ssize_t ret = recvfrom(sock, buf, sizeof(buf) - 1, 0,
                           (struct sockaddr *)&client, &len);
    if (ret > 0) {
      printf("client :%s \n", buf);
      fflush(stdout);
      sendto(sock, buf, strlen(buf), 0, (struct sockaddr *)&client, len);
    } else {
      perror("recvfrom");
      return 3;
    }
  }
  close(sock);
  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     120b <main+0x3b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <print@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <main+0x1b5>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1249 <main+0x79>
lea    0xdc8(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <main+0x1b5>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
movl   $0xc,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    12b4 <main+0xe4>
lea    0xd64(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1385 <main+0x1b5>
movl   $0xc,-0x34(%rbp)
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x3ff,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   10b0 <recvfrom@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
jle    1368 <main+0x198>
lea    -0x440(%rbp),%rsi
lea    0xcfd(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x454(%rbp)
lea    -0x440(%rbp),%rax
mov    %rax,-0x450(%rbp)
lea    -0x440(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
lea    -0x30(%rbp),%r8
mov    -0x34(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
jmp    1380 <main+0x1b0>
lea    0xcaa(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1385 <main+0x1b5>
jmp    12bb <main+0xeb>
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
je     1208 <main+0x38>
mov    (%rbx),%rdi
call   1080 <print@plt>
mov    $0xffffffff,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd9(%rip),%r14
mov    (%r14),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
jns    1239 <main+0x69>
lea    0xdd3(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%eax
jmp    11f6 <main+0x26>
mov    %eax,%ebp
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x10(%rbx),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rbx),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jns    128f <main+0xbf>
lea    0xd87(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%eax
jmp    11f6 <main+0x26>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
call   10b0 <recvfrom@plt>
test   %rax,%rax
jle    134d <main+0x17d>
lea    0xd36(%rip),%r14
lea    0x30(%rsp),%rbx
mov    0x2cee(%rip),%r13
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x0(%r13),%edi
call   10a0 <fflush@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    0xc(%rsp),%r9d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1070 <sendto@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   10b0 <recvfrom@plt>
test   %rax,%rax
jg     12f0 <main+0x120>
lea    0xcc5(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x3,%eax
jmp    11f6 <main+0x26>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [13 x i8] c"client :%s \0A\00"
@stdout = global i32 0
@.str.3 = [9 x i8] c"recvfrom\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i64
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, 3
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @print(i8* %18)
store i32 -1, i32* %3
br label %75
20: ; preds = %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 i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %75
28: ; preds = %20
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32 @inet_addr(i8* %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
store i32 %40, i32* %42
store i32 12, i32* %8
%43 = load i32, i32* %6
%44 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%45 = load i32, i32* %8
%46 = call i64 @bind(i32 %43, %struct.sockaddr* %44, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %28
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %75
50: ; preds = %28
store i32 12, i32* %10
br label %51
51: ; preds = %50, %74
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%53 = call i32 @memset(i8* %52, i8 signext 0, i32 1024)
%54 = load i32, i32* %6
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%56 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%57 = call i64 @recvfrom(i32 %54, i8* %55, i32 1023, i32 0, %struct.sockaddr* %56, i32* %10)
store i64 %57, i64* %12
%58 = load i64, i64* %12
%59 = icmp sgt i64 %58, 0
br i1 %59, label %60, label %72
60: ; preds = %51
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%62 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = load i32, i32* @stdout
%64 = call i32 @fflush(i32 %63)
%65 = load i32, i32* %6
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%68 = call i32 @strlen(i8* %67)
%69 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%70 = load i32, i32* %10
%71 = call i32 @sendto(i32 %65, i8* %66, i32 %68, i32 0, %struct.sockaddr* %69, i32 %70)
br label %74
72: ; preds = %51
%73 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 3, i32* %3
br label %75
74: ; preds = %60
br label %51
75: ; preds = %72, %48, %26, %15
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [13 x i8] c"client :%s \0A\00"
@stdout = global i32 0
@.str.3 = [9 x i8] c"recvfrom\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = icmp eq i32 %0, 3
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @print(i8* %9)
br label %58
11: ; preds = %2
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %58
18: ; preds = %11
%19 = bitcast %struct.sockaddr_in* %3 to i8*
%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 i8*, i8** %1, i64 2
%23 = load i8*, i8** %22
%24 = tail call i32 @atoi(i8* %23)
%25 = tail call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds i8*, i8** %1, i64 1
%28 = load i8*, i8** %27
%29 = tail call i32 @inet_addr(i8* %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %29, i32* %30
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @bind(i32 %14, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %18
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %56
36: ; preds = %18
%37 = bitcast %struct.sockaddr_in* %4 to i8*
%38 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = call i32 @memset(i8* %39, i8 signext 0, i32 1024)
%41 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%42 = call i64 @recvfrom(i32 %14, i8* %39, i32 1023, i32 0, %struct.sockaddr* %41, i32* %5)
%43 = icmp sgt i64 %42, 0
br i1 %43, label %44, label %54
44: ; preds = %36, %44
%45 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %39)
%46 = load i32, i32* @stdout
%47 = call i32 @fflush(i32 %46)
%48 = call i32 @strlen(i8* %39)
%49 = load i32, i32* %5
%50 = call i32 @sendto(i32 %14, i8* %39, i32 %48, i32 0, %struct.sockaddr* %41, i32 %49)
%51 = call i32 @memset(i8* %39, i8 signext 0, i32 1024)
%52 = call i64 @recvfrom(i32 %14, i8* %39, i32 1023, i32 0, %struct.sockaddr* %41, i32* %5)
%53 = icmp sgt i64 %52, 0
br i1 %53, label %44, label %54
54: ; preds = %44, %36
%55 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %34
%57 = phi i32 [ 2, %34 ], [ 3, %54 ]
br label %58
58: ; preds = %16, %56, %8
%59 = phi i32 [ -1, %8 ], [ 1, %16 ], [ %57, %56 ]
ret i32 %59
}