#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;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ htons(int);
int inet_addr(char *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
scalar_t__ recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr_in *,
                    int *);
int /*<<< orphan*/ scanf(char *, char *);
scalar_t__ 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 sock;
  struct sockaddr_in server;
  char message[1000], server_reply[2000];
  printf("Press any key to continue...");
  getchar();
  printf("\n");
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock == -1) {
    perror("socket creation failed");
    return 1;
  }
  puts("Socket created\n");
  server.sin_addr.s_addr = inet_addr("192.168.1.173");
  server.sin_family = AF_INET;
  server.sin_port = htons(8888);
  while (1) {
    struct sockaddr_in peer;
    socklen_t address_len = sizeof(server);
    printf("Enter message : ");
    scanf("%s", message);
    if (sendto(sock, message, strlen(message) + 1, 0, &server, address_len) <
        0) {
      perror("Send failed");
      close(sock);
      return 1;
    }
    if (recvfrom(sock, server_reply, 2000, 0, &peer, &address_len) < 0) {
      perror("recv failed");
      break;
    }
    puts("Server reply :");
    uint8_t *ipaddr = &peer.sin_addr.s_addr;
    printf("Server (%d.%d.%d.%d:%d) reply: %s\n", ipaddr[0], ipaddr[1],
           ipaddr[2], ipaddr[3], ntohs(peer.sin_port), server_reply);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc20,%rsp
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10a0 <getchar@plt>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    123c <loop+0x6c>
lea    0xdf2(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13fe <loop+0x22e>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf5(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
movl   $0xc,-0xbe4(%rbp)
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x400(%rbp),%rsi
lea    0xdcd(%rip),%rdi
call   10d0 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xbfc(%rbp)
lea    -0x400(%rbp),%rax
mov    %rax,-0xbf8(%rbp)
lea    -0x400(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xbfc(%rbp),%edi
mov    -0xbf8(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
mov    -0xbe4(%rbp),%r9d
xor    %ecx,%ecx
lea    -0x18(%rbp),%r8
call   1070 <sendto@plt>
cmp    $0x0,%rax
jge    130e <loop+0x13e>
lea    0xd71(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13fe <loop+0x22e>
mov    -0x8(%rbp),%edi
lea    -0xbd0(%rbp),%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
lea    -0xbe0(%rbp),%r8
lea    -0xbe4(%rbp),%r9
call   10b0 <recvfrom@plt>
cmp    $0x0,%rax
jge    134d <loop+0x17d>
lea    0xd2f(%rip),%rdi
call   10e0 <perror@plt>
jmp    13ef <loop+0x21f>
lea    0xd2a(%rip),%rdi
call   1030 <puts@plt>
lea    -0xbe0(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0xbf0(%rbp)
mov    -0xbf0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc0c(%rbp)
mov    -0xbf0(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0xc08(%rbp)
mov    -0xbf0(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0xc04(%rbp)
mov    -0xbf0(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0xc00(%rbp)
mov    -0xbe0(%rbp),%edi
call   10c0 <ntohs@plt>
mov    -0xc0c(%rbp),%esi
mov    -0xc08(%rbp),%edx
mov    -0xc04(%rbp),%ecx
mov    -0xc00(%rbp),%r8d
mov    %eax,%r9d
lea    -0xbd0(%rbp),%rax
lea    0xcae(%rip),%rdi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1270 <loop+0xa0>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xbf8,%rsp
lea    0xe08(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   10b0 <getchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2dd1(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
jne    1245 <loop+0x65>
lea    0xde7(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%eax
jmp    1377 <loop+0x197>
mov    %eax,%ebx
lea    0xde6(%rip),%rdi
call   1040 <puts@plt>
lea    0xdea(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x24(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    $0x22b8,%edi
call   1060 <htons@plt>
mov    %eax,0x20(%rsp)
nopl   0x0(%rax,%rax,1)
movl   $0xc,0xc(%rsp)
lea    0xdc3(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdc6(%rip),%rdi
lea    0x30(%rsp),%rbp
mov    %rbp,%rsi
call   10e0 <scanf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    0xc(%rsp),%r9d
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
lea    0x20(%rsp),%r8
call   1080 <sendto@plt>
test   %rax,%rax
js     1348 <loop+0x168>
mov    %ebx,%edi
lea    0x420(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
call   10c0 <recvfrom@plt>
test   %rax,%rax
js     1362 <loop+0x182>
lea    0xd7e(%rip),%rdi
call   1040 <puts@plt>
mov    0x10(%rsp),%edi
mov    0x14(%rsp),%r15d
mov    0x18(%rsp),%r12d
mov    0x1c(%rsp),%r14d
mov    0x20(%rsp),%r13d
call   10d0 <ntohs@plt>
mov    %rbp,(%rsp)
lea    0xd60(%rip),%rdi
mov    %r15d,%esi
mov    %r12d,%edx
mov    %r14d,%ecx
mov    %r13d,%r8d
mov    %eax,%r9d
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1280 <loop+0xa0>
lea    0xd17(%rip),%rdi
call   10f0 <perror@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    $0x1,%eax
jmp    1377 <loop+0x197>
lea    0xd09(%rip),%rdi
call   10f0 <perror@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0xbf8,%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 }
@.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 = [23 x i8] c"socket creation failed\00"
@.str.3 = [16 x i8] c"Socket created\0A\00"
@.str.4 = [14 x i8] c"192.168.1.173\00"
@.str.5 = [17 x i8] c"Enter message : \00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [12 x i8] c"Send failed\00"
@.str.8 = [12 x i8] c"recv failed\00"
@.str.9 = [15 x i8] c"Server reply :\00"
@.str.10 = [35 x i8] c"Server (%d.%d.%d.%d:%d) reply: %s\0A\00"
define i32 @loop() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [1000 x i8]
%5 = alloca [2000 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32*
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @getchar()
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %75
19: ; preds = %0
%20 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @htons(i32 8888)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %26, i32* %27
br label %28
28: ; preds = %19, %51
store i32 12, i32* %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %30)
%32 = load i32, i32* %2
%33 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%34 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%35 = call i64 @strlen(i8* %34)
%36 = add nsw i64 %35, 1
%37 = load i32, i32* %7
%38 = call i64 @sendto(i32 %32, i8* %33, i64 %36, i32 0, %struct.sockaddr_in* %3, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %28
%41 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%42 = load i32, i32* %2
%43 = call i32 @close(i32 %42)
store i32 1, i32* %1
br label %75
44: ; preds = %28
%45 = load i32, i32* %2
%46 = getelementptr inbounds [2000 x i8], [2000 x i8]* %5, i64 0, i64 0
%47 = call i64 @recvfrom(i32 %45, i8* %46, i32 2000, i32 0, %struct.sockaddr_in* %6, i32* %7)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
br label %72
51: ; preds = %44
%52 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
store i32* %54, i32** %8
%55 = load i32*, i32** %8
%56 = getelementptr inbounds i32, i32* %55, i64 0
%57 = load i32, i32* %56
%58 = load i32*, i32** %8
%59 = getelementptr inbounds i32, i32* %58, i64 1
%60 = load i32, i32* %59
%61 = load i32*, i32** %8
%62 = getelementptr inbounds i32, i32* %61, i64 2
%63 = load i32, i32* %62
%64 = load i32*, i32** %8
%65 = getelementptr inbounds i32, i32* %64, i64 3
%66 = load i32, i32* %65
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @ntohs(i32 %68)
%70 = getelementptr inbounds [2000 x i8], [2000 x i8]* %5, i64 0, i64 0
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %57, i32 %60, i32 %63, i32 %66, i32 %69, i8* %70)
br label %28
72: ; preds = %49
%73 = load i32, i32* %2
%74 = call i32 @close(i32 %73)
store i32 0, i32* %1
br label %75
75: ; preds = %72, %40, %17
%76 = load i32, i32* %1
ret i32 %76
}

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