#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 */
typedef int uint8_t;
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ peer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ htons(int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr_in *, int *);
int /*<<< orphan*/ sendto(int, char *, scalar_t__, int /*<<< orphan*/,
                          struct sockaddr_in *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int loop(void) {
  int socket_desc, client_sock, c, read_size;
  struct sockaddr_in server, client;
  char client_message[2000];
  printf("Press any key to continue...");
  getchar();
  printf("\n");
  socket_desc = socket(AF_INET, SOCK_DGRAM, 0);
  if (socket_desc == -1) {
    perror("Could not create socket");
  }
  puts("Socket created");
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(8888);
  if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) != 0) {
    perror("bind failed.");
    close(socket_desc);
    return 1;
  }
  puts("bind done");
  while (1) {
    struct sockaddr_in peer;
    socklen_t address_len = sizeof(peer);
    while ((read_size = recvfrom(socket_desc, client_message, 2000, 0, &peer,
                                 &address_len)) > 0) {
      uint8_t *ipaddr = &peer.sin_addr.s_addr;
      printf("recv(%d.%d.%d.%d:%d): %s\n", ipaddr[0], ipaddr[1], ipaddr[2],
             ipaddr[3], ntohs(peer.sin_port), client_message);
      sendto(socket_desc, client_message, strlen(client_message) + 1, 0, &peer,
             address_len);
    }
    if (read_size == -1) {
      perror("recv failed");
      close(socket_desc);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1090 <getchar@plt>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1220 <loop+0x60>
lea    0xe02(%rip),%rdi
call   10d0 <perror@plt>
lea    0xe0e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
je     128e <loop+0xce>
lea    0xdd1(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%eax
add    $0x850,%rsp
pop    %rbp
ret
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
movl   $0xc,-0x814(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x800(%rbp),%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
lea    -0x810(%rbp),%r8
lea    -0x814(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    13b6 <loop+0x1f6>
lea    -0x810(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x820(%rbp)
mov    -0x820(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x83c(%rbp)
mov    -0x820(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x838(%rbp)
mov    -0x820(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x834(%rbp)
mov    -0x820(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x830(%rbp)
mov    -0x810(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x83c(%rbp),%esi
mov    -0x838(%rbp),%edx
mov    -0x834(%rbp),%ecx
mov    -0x830(%rbp),%r8d
mov    %eax,%r9d
lea    -0x800(%rbp),%rax
lea    0xd01(%rip),%rdi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x82c(%rbp)
lea    -0x800(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x800(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
mov    -0x814(%rbp),%r9d
xor    %ecx,%ecx
lea    -0x810(%rbp),%r8
call   1070 <sendto@plt>
jmp    12a4 <loop+0xe4>
cmpl   $0xffffffff,-0x10(%rbp)
jne    13d4 <loop+0x214>
lea    0xcae(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
jmp    129a <loop+0xda>

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
lea    0xe18(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2de1(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1373 <loop+0x1a3>
lea    0xe09(%rip),%rdi
call   1040 <puts@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    $0x22b8,%edi
call   1060 <htons@plt>
mov    %eax,0x20(%rsp)
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10d0 <bind@plt>
test   %rax,%rax
jne    1384 <loop+0x1b4>
lea    0xddd(%rip),%rdi
call   1040 <puts@plt>
lea    0x30(%rsp),%rsi
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%r15
jmp    129e <loop+0xce>
nopw   0x0(%rax,%rax,1)
cmp    $0xffffffff,%eax
je     135b <loop+0x18b>
lea    0x30(%rsp),%rsi
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    $0x7d0,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r15,%r9
call   10b0 <recvfrom@plt>
test   %eax,%eax
jle    1290 <loop+0xc0>
lea    0x30(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x10(%rsp),%edi
mov    0x14(%rsp),%r13d
mov    0x18(%rsp),%r12d
mov    0x1c(%rsp),%r15d
mov    0x20(%rsp),%ebp
call   10c0 <ntohs@plt>
mov    %r14,(%rsp)
lea    0xd64(%rip),%rdi
mov    %r13d,%esi
mov    %r12d,%edx
mov    %r15d,%ecx
lea    0xc(%rsp),%r15
mov    %ebp,%r8d
lea    0x10(%rsp),%rbp
mov    %eax,%r9d
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    0xc(%rsp),%r9d
mov    %ebx,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
mov    %rbp,%r8
call   1080 <sendto@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r15,%r9
call   10b0 <recvfrom@plt>
test   %eax,%eax
jg     12d0 <loop+0x100>
jmp    1290 <loop+0xc0>
lea    0xd13(%rip),%rdi
call   10e0 <perror@plt>
mov    %ebx,%edi
call   1090 <close@plt>
jmp    1299 <loop+0xc9>
lea    0xca3(%rip),%rdi
call   10e0 <perror@plt>
jmp    1225 <loop+0x55>
lea    0xcb9(%rip),%rdi
call   10e0 <perror@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    $0x1,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"Press any key to continue...\00"
@.str.1 = [2 x i8] c"\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [24 x i8] c"Could not create socket\00"
@.str.3 = [15 x i8] c"Socket created\00"
@INADDR_ANY = global i32 0
@.str.4 = [13 x i8] c"bind failed.\00"
@.str.5 = [10 x i8] c"bind done\00"
@.str.6 = [26 x i8] c"recv(%d.%d.%d.%d:%d): %s\0A\00"
@.str.7 = [12 x i8] c"recv failed\00"
define i32 @loop() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca [2000 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32*
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (...) @getchar()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %1
%17 = load i32, i32* %1
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %0
%22 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = call i32 @htons(i32 8888)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %1
%31 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %21
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* %1
%37 = call i32 @close(i32 %36)
ret i32 1
38: ; preds = %21
%39 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %38, %80
store i32 12, i32* %9
br label %41
41: ; preds = %46, %40
%42 = load i32, i32* %1
%43 = getelementptr inbounds [2000 x i8], [2000 x i8]* %7, i64 0, i64 0
%44 = call i32 @recvfrom(i32 %42, i8* %43, i32 2000, i32 0, %struct.sockaddr_in* %8, i32* %9)
store i32 %44, i32* %4
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %73
46: ; preds = %41
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
store i32* %48, i32** %10
%49 = load i32*, i32** %10
%50 = getelementptr inbounds i32, i32* %49, i64 0
%51 = load i32, i32* %50
%52 = load i32*, i32** %10
%53 = getelementptr inbounds i32, i32* %52, i64 1
%54 = load i32, i32* %53
%55 = load i32*, i32** %10
%56 = getelementptr inbounds i32, i32* %55, i64 2
%57 = load i32, i32* %56
%58 = load i32*, i32** %10
%59 = getelementptr inbounds i32, i32* %58, i64 3
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @ntohs(i32 %62)
%64 = getelementptr inbounds [2000 x i8], [2000 x i8]* %7, i64 0, i64 0
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %51, i32 %54, i32 %57, i32 %60, i32 %63, i8* %64)
%66 = load i32, i32* %1
%67 = getelementptr inbounds [2000 x i8], [2000 x i8]* %7, i64 0, i64 0
%68 = getelementptr inbounds [2000 x i8], [2000 x i8]* %7, i64 0, i64 0
%69 = call i64 @strlen(i8* %68)
%70 = add nsw i64 %69, 1
%71 = load i32, i32* %9
%72 = call i32 @sendto(i32 %66, i8* %67, i64 %70, i32 0, %struct.sockaddr_in* %8, i32 %71)
br label %41
73: ; preds = %41
%74 = load i32, i32* %4
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %80
76: ; preds = %73
%77 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%78 = load i32, i32* %1
%79 = call i32 @close(i32 %78)
br label %80
80: ; preds = %76, %73
br label %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"Press any key to continue...\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.2 = [24 x i8] c"Could not create socket\00"
@.str.3 = [15 x i8] c"Socket created\00"
@INADDR_ANY = global i32 0
@.str.4 = [13 x i8] c"bind failed.\00"
@.str.5 = [10 x i8] c"bind done\00"
@.str.6 = [26 x i8] c"recv(%d.%d.%d.%d:%d): %s\0A\00"
@.str.7 = [12 x i8] c"recv failed\00"
define i32 @loop() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [2000 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = getelementptr inbounds [2000 x i8], [2000 x i8]* %2, i64 0, i64 0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 (...) @getchar()
%9 = tail call i32 @putchar(i32 10)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, 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 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 8888)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i64 @bind(i32 %12, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 @close(i32 %12)
ret i32 1
30: ; preds = %16
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%32 = bitcast %struct.sockaddr_in* %3 to i8*
%33 = bitcast i32* %4 to i8*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%35 = getelementptr inbounds i32, i32* %34, i64 1
%36 = getelementptr inbounds i32, i32* %34, i64 2
%37 = getelementptr inbounds i32, i32* %34, i64 3
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %39
39: ; preds = %62, %30
store i32 12, i32* %4
%40 = call i32 @recvfrom(i32 %12, i8* %6, i32 2000, i32 0, %struct.sockaddr_in* %3, i32* %4)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %56
42: ; preds = %39, %42
%43 = load i32, i32* %34
%44 = load i32, i32* %35
%45 = load i32, i32* %36
%46 = load i32, i32* %37
%47 = load i32, i32* %38
%48 = call i32 @ntohs(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i32 %43, i32 %44, i32 %45, i32 %46, i32 %48, i8* %6)
%50 = call i64 @strlen(i8* %6)
%51 = add nsw i64 %50, 1
%52 = load i32, i32* %4
%53 = call i32 @sendto(i32 %12, i8* %6, i64 %51, i32 0, %struct.sockaddr_in* %3, i32 %52)
%54 = call i32 @recvfrom(i32 %12, i8* %6, i32 2000, i32 0, %struct.sockaddr_in* %3, i32* %4)
%55 = icmp sgt i32 %54, 0
br i1 %55, label %42, label %56
56: ; preds = %42, %39
%57 = phi i32 [ %40, %39 ], [ %54, %42 ]
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%61 = call i32 @close(i32 %12)
br label %62
62: ; preds = %59, %56
br label %39
}