#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 socklen_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ ad_snd;
typedef int /*<<< orphan*/ ad;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int 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);
int /*<<< orphan*/ strlen(char *);
int main() {
  int fd;
  struct sockaddr_in ad;
  char buf[102];
  struct sockaddr_in ad_snd;
  socklen_t len;
  int r;
  fd = socket(AF_INET, SOCK_DGRAM, 17);
  if (fd == -1) {
    printf("socket : %m\n");
    exit(-1);
  }
  printf("create socket success....\n");
  ad.sin_family = AF_INET;
  ad.sin_port = htons(8888);
  inet_aton("192.168.137.3", &ad.sin_addr);
  r = bind(fd, (struct sockaddr *)&ad, sizeof(ad));
  if (r == -1) {
    printf("socket bind : %m\n");
    exit(-1);
  }
  printf("socket bind success....\n");
  while (1) {
    len = sizeof(ad_snd);
    r = recvfrom(fd, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&ad_snd, &len);
    if (r > 0) {
      buf[r] = 0;
      printf("send:%s,port:%hu,data : %s\n", inet_ntoa(ad_snd.sin_addr),
             ntohs(ad_snd.sin_port), buf);
      sendto(fd, "Hello!", strlen("Hello!"), 0, (struct sockaddr *)&ad_snd,
             sizeof(ad_snd));
    }
    if (r == 0) {
      printf("send close.....\n");
      close(fd);
      break;
    }
    if (r == -1) {
      printf("socket connect error....\n");
      close(fd);
      break;
    }
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    $0x11,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1213 <main+0x53>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1090 <inet_aton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0xffffffff,-0x98(%rbp)
jne    1286 <main+0xc6>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xc,-0x94(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
lea    -0x90(%rbp),%r8
mov    $0x65,%edx
xor    %ecx,%ecx
lea    -0x94(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x98(%rbp)
cmpl   $0x0,-0x98(%rbp)
jle    134e <main+0x18e>
movslq -0x98(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
mov    -0x90(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0xa8(%rbp)
mov    -0x8c(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
lea    -0x80(%rbp),%rcx
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x9c(%rbp)
lea    0xd57(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x9c(%rbp),%edi
mov    %eax,%edx
lea    -0x90(%rbp),%r8
lea    0xd3c(%rip),%rsi
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
cmpl   $0x0,-0x98(%rbp)
jne    1376 <main+0x1b6>
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    13a3 <main+0x1e3>
cmpl   $0xffffffff,-0x98(%rbp)
jne    139e <main+0x1de>
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    13a3 <main+0x1e3>
jmp    1294 <main+0xd4>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2e00(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    $0x11,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1340 <main+0x170>
mov    %eax,%ebx
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    $0x22b8,%edi
call   1060 <htons@plt>
mov    %eax,0x24(%rsp)
lea    0xddc(%rip),%rdi
lea    0x20(%rsp),%rbp
mov    %rbp,%rsi
call   10a0 <inet_aton@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
je     1349 <main+0x179>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
lea    0x30(%rsp),%r14
lea    0x10(%rsp),%r15
lea    0xdd6(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x65,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    0xc(%rsp),%r9
call   10b0 <recvfrom@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    12fa <main+0x12a>
mov    %r13d,%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r12
mov    0x14(%rsp),%edi
call   10c0 <ntohs@plt>
lea    0xd61(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1080 <sendto@plt>
test   %r13d,%r13d
je     1312 <main+0x142>
cmp    $0xffffffff,%r13d
jne    1280 <main+0xb0>
lea    0xd72(%rip),%rdi
jmp    1319 <main+0x149>
lea    0xd82(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb9(%rip),%rdi
jmp    1350 <main+0x180>
lea    0xccb(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%edi
call   10e0 <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
@.str = [13 x i8] c"socket : %m\0A\00"
@.str.1 = [27 x i8] c"create socket success....\0A\00"
@.str.2 = [14 x i8] c"192.168.137.3\00"
@.str.3 = [18 x i8] c"socket bind : %m\0A\00"
@.str.4 = [25 x i8] c"socket bind success....\0A\00"
@.str.5 = [28 x i8] c"send:%s,port:%hu,data : %s\0A\00"
@.str.6 = [7 x i8] c"Hello!\00"
@.str.7 = [17 x i8] c"send close.....\0A\00"
@.str.8 = [26 x i8] c"socket connect error....\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [102 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 17)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 8888)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32* %22)
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %16
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %16
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %71
store i32 12, i32* %6
%35 = load i32, i32* %2
%36 = getelementptr inbounds [102 x i8], [102 x i8]* %4, i64 0, i64 0
%37 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%38 = call i32 @recvfrom(i32 %35, i8* %36, i32 101, i32 0, %struct.sockaddr* %37, i32* %6)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %57
41: ; preds = %34
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [102 x i8], [102 x i8]* %4, i64 0, i64 %43
store i8 0, i8* %44
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i8* @inet_ntoa(i32 %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%49 = load i32, i32* %48
%50 = call i32 @ntohs(i32 %49)
%51 = getelementptr inbounds [102 x i8], [102 x i8]* %4, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %47, i32 %50, i8* %51)
%53 = load i32, i32* %2
%54 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%55 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%56 = call i32 @sendto(i32 %53, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 0, %struct.sockaddr* %55, i32 12)
br label %57
57: ; preds = %41, %34
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%62 = load i32, i32* %2
%63 = call i32 @close(i32 %62)
br label %72
64: ; preds = %57
%65 = load i32, i32* %7
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %71
67: ; preds = %64
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
%69 = load i32, i32* %2
%70 = call i32 @close(i32 %69)
br label %72
71: ; preds = %64
br label %34
72: ; preds = %67, %60
%73 = load i32, i32* %2
%74 = call i32 @close(i32 %73)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [13 x i8] c"socket : %m\0A\00"
@.str.2 = [14 x i8] c"192.168.137.3\00"
@.str.3 = [18 x i8] c"socket bind : %m\0A\00"
@.str.5 = [28 x i8] c"send:%s,port:%hu,data : %s\0A\00"
@.str.6 = [7 x i8] c"Hello!\00"
@str = [26 x i8] c"create socket success....\00"
@str.9 = [24 x i8] c"socket bind success....\00"
@str.10 = [25 x i8] c"socket connect error....\00"
@str.11 = [16 x i8] c"send close.....\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [102 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = getelementptr inbounds [102 x i8], [102 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 17)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 8888)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%23 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32* %22)
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i32 @bind(i32 %11, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %16
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.9, i64 0, i64 0))
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
br label %35
35: ; preds = %48, %30
store i32 12, i32* %4
%36 = call i32 @recvfrom(i32 %11, i8* %6, i32 101, i32 0, %struct.sockaddr* %32, i32* %4)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %35
%39 = zext i32 %36 to i64
%40 = getelementptr inbounds [102 x i8], [102 x i8]* %2, i64 0, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %33
%42 = call i8* @inet_ntoa(i32 %41)
%43 = load i32, i32* %34
%44 = call i32 @ntohs(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %42, i32 %44, i8* %6)
%46 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%47 = call i32 @sendto(i32 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 %46, i32 0, %struct.sockaddr* %32, i32 12)
br label %48
48: ; preds = %38, %35
switch i32 %36, label %35 [
i32 0, label %50
i32 -1, label %49
]
49: ; preds = %48
br label %50
50: ; preds = %48, %49
%51 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @str.10, i64 0, i64 0), %49 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.11, i64 0, i64 0), %48 ]
%52 = call i32 @puts(i8* dereferenceable(1) %51)
%53 = call i32 @close(i32 %11)
%54 = call i32 @close(i32 %11)
ret i32 0
}