#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*/ serveraddr;
typedef int /*<<< orphan*/ clientaddr;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void main() {
  int sd = -1, rc;
  char buffer[100];
  struct sockaddr_in serveraddr;
  struct sockaddr_in clientaddr;
  int clientaddrlen = sizeof(clientaddr);
  do {
    sd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sd < 0) {
      perror("socket() failed");
      break;
    }
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(6701);
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    rc = bind(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
    if (rc < 0) {
      perror("bind() failed");
      break;
    }
    rc = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&clientaddr,
                  &clientaddrlen);
    if (rc < 0) {
      perror("recvfrom() failed");
      break;
    }
    printf("server received the following: <%s>\n", buffer);
    printf("from port %d and address %s\n", ntohs(clientaddr.sin_port),
           inet_ntoa(clientaddr.sin_addr));
    rc = sendto(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&clientaddr,
                sizeof(clientaddr));
    if (rc < 0) {
      perror("sendto() failed");
      break;
    }
  } while (0);
  if (sd != -1)
    close(sd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0xffffffff,-0x4(%rbp)
movl   $0xc,-0x94(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1213 <main+0x53>
lea    0xdf7(%rip),%rdi
call   10d0 <perror@plt>
jmp    1343 <main+0x183>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    $0x1a2d,%edi
call   1040 <htons@plt>
mov    %eax,-0x7c(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x80(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    127c <main+0xbc>
lea    0xd9e(%rip),%rdi
call   10d0 <perror@plt>
jmp    1343 <main+0x183>
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
lea    -0x90(%rbp),%r8
mov    $0x64,%edx
xor    %ecx,%ecx
lea    -0x94(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    12bb <main+0xfb>
lea    0xd6d(%rip),%rdi
call   10d0 <perror@plt>
jmp    1343 <main+0x183>
lea    -0x70(%rbp),%rsi
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8c(%rbp),%edi
call   10b0 <ntohs@plt>
mov    %eax,-0x98(%rbp)
mov    -0x90(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    -0x98(%rbp),%esi
mov    %rax,%rdx
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
lea    -0x90(%rbp),%r8
mov    $0x64,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    133e <main+0x17e>
lea    0xd3e(%rip),%rdi
call   10d0 <perror@plt>
jmp    1343 <main+0x183>
jmp    1343 <main+0x183>
cmpl   $0xffffffff,-0x4(%rbp)
je     1355 <main+0x195>
mov    -0x4(%rbp),%edi
call   1090 <close@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2e0e(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12de <main+0x11e>
lea    0x20(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    $0x1a2d,%edi
call   1040 <htons@plt>
mov    %eax,0x24(%rsp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x20(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %eax,%eax
js     12f1 <main+0x131>
lea    0x30(%rsp),%rsi
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   10a0 <recvfrom@plt>
test   %eax,%eax
js     12fa <main+0x13a>
lea    0xdbb(%rip),%rdi
lea    0x30(%rsp),%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x14(%rsp),%edi
call   10b0 <ntohs@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%edi
call   1030 <inet_ntoa@plt>
lea    0xdb6(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
test   %eax,%eax
js     1303 <main+0x143>
mov    %ebx,%edi
call   1090 <close@plt>
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd1b(%rip),%rdi
call   10d0 <perror@plt>
cmp    $0xffffffff,%ebx
jne    12cb <main+0x10b>
jmp    12d2 <main+0x112>
lea    0xd18(%rip),%rdi
jmp    130a <main+0x14a>
lea    0xd1d(%rip),%rdi
jmp    130a <main+0x14a>
lea    0xd68(%rip),%rdi
call   10d0 <perror@plt>
jmp    12cb <main+0x10b>

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 = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"bind() failed\00"
@.str.2 = [18 x i8] c"recvfrom() failed\00"
@.str.3 = [37 x i8] c"server received the following: <%s>\0A\00"
@.str.4 = [29 x i8] c"from port %d and address %s\0A\00"
@.str.5 = [16 x i8] c"sendto() failed\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32 -1, i32* %1
store i32 12, i32* %6
br label %7
7: ; preds = %0
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %1
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %61
15: ; preds = %7
%16 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 6701)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %1
%26 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %15
%31 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %61
32: ; preds = %15
%33 = load i32, i32* %1
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i32 @recvfrom(i32 %33, i8* %34, i32 100, i32 0, %struct.sockaddr* %35, i32* %6)
store i32 %36, i32* %2
%37 = load i32, i32* %2
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %61
41: ; preds = %32
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%45 = load i32, i32* %44
%46 = call i32 @ntohs(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @inet_ntoa(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %46, i8* %50)
%52 = load i32, i32* %1
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%54 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%55 = call i32 @sendto(i32 %52, i8* %53, i32 100, i32 0, %struct.sockaddr* %54, i32 12)
store i32 %55, i32* %2
%56 = load i32, i32* %2
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %41
%59 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %61
60: ; preds = %41
br label %61
61: ; preds = %60, %58, %39, %30, %13
%62 = load i32, i32* %1
%63 = icmp ne i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32, i32* %1
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %64, %61
ret void
}

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 = [16 x i8] c"socket() failed\00"
@INADDR_ANY = global i32 0
@.str.1 = [14 x i8] c"bind() failed\00"
@.str.2 = [18 x i8] c"recvfrom() failed\00"
@.str.3 = [37 x i8] c"server received the following: <%s>\0A\00"
@.str.4 = [29 x i8] c"from port %d and address %s\0A\00"
@.str.5 = [16 x i8] c"sendto() failed\00"
define void @main() {
%1 = alloca [100 x i8]
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %40, label %13
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 6701)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %11, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i32 %23, 0
br i1 %24, label %43, label %25
25: ; preds = %13
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @recvfrom(i32 %11, i8* %5, i32 100, i32 0, %struct.sockaddr* %26, i32* %4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %43, label %29
29: ; preds = %25
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @ntohs(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i8* @inet_ntoa(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %33, i8* %36)
%38 = call i32 @sendto(i32 %11, i8* %5, i32 100, i32 0, %struct.sockaddr* %26, i32 12)
%39 = icmp slt i32 %38, 0
br i1 %39, label %43, label %46
40: ; preds = %0
%41 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%42 = icmp eq i32 %11, -1
br i1 %42, label %48, label %46
43: ; preds = %29, %25, %13
%44 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %25 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), %29 ]
%45 = call i32 @perror(i8* %44)
br label %46
46: ; preds = %43, %29, %40
%47 = call i32 @close(i32 %11)
br label %48
48: ; preds = %46, %40
ret void
}