#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ECONNRESET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int receiver(int port) {
  printf("Receiver\n");
  int connfd, listenfd, n;
  struct sockaddr_in server = {0};
  struct sockaddr_in client = {0};
  socklen_t client_len;
  pid_t childpid;
  char msg[1500];
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(INADDR_ANY);
  server.sin_port = htons(port);
  bind(listenfd, (struct sockaddr *)&server, sizeof(server));
  listen(listenfd, 1024);
  for (;;) {
    client_len = sizeof(client);
    connfd = accept(listenfd, (struct sockaddr *)&client, &client_len);
    if ((childpid = fork()) == 0) {
      for (;;) {
        n = recvfrom(connfd, msg, 1500, 0, (struct sockaddr *)&client,
                     &client_len);
        if (n < 0) {
          if (errno == ECONNRESET) {
            close(connfd);
            return 0;
          }
          perror("Problem receiving data");
          exit(-1);
        }
      }
    }
    close(connfd);
  }
  close(listenfd);
  return 0;
}


x86 assembly (clang, -O0):
<receiver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe1b(%rip),%rdi
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
mov    -0xc(%rbp),%edi
mov    $0x400,%esi
call   1080 <listen@plt>
movl   $0x10,-0x34(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jne    1311 <receiver+0x141>
jmp    129e <receiver+0xce>
mov    -0x8(%rbp),%edi
lea    -0x620(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x5dc,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    130c <receiver+0x13c>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rax
mov    0x2cf6(%rip),%rcx
cmp    (%rcx),%rax
jne    12f6 <receiver+0x126>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x620,%rsp
pop    %rbp
ret
lea    0xd0d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
jmp    129e <receiver+0xce>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    126c <receiver+0x9c>

x86 assembly (clang, -O2):
<receiver>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x618,%rsp
mov    %edi,%r14d
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,0x1c(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
mov    0x2df5(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
mov    %ebx,%edi
mov    $0x400,%esi
call   1070 <listen@plt>
movl   $0x10,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10b0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12a9 <receiver+0xe9>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1060 <close@plt>
movl   $0x10,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
jne    1280 <receiver+0xc0>
lea    0x30(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %rbx,%r9
call   1080 <recvfrom@plt>
test   %eax,%eax
jns    12c0 <receiver+0x100>
mov    0x2cde(%rip),%rax
mov    (%rax),%rax
mov    0x2ce4(%rip),%rcx
cmp    (%rcx),%rax
jne    1308 <receiver+0x148>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x618,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"Receiver\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@errno = global i64 0
@ECONNRESET = global i64 0
@.str.1 = [23 x i8] c"Problem receiving data\00"
define i32 @receiver(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1500 x i8]
store i32 %0, i32* %2
%11 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%12 = bitcast %struct.sockaddr_in* %6 to i8*
%13 = bitcast %struct.sockaddr_in* %7 to i8*
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %4
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
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* %6, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %2
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %4
%27 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 16)
%29 = load i32, i32* %4
%30 = call i32 @listen(i32 %29, i32 1024)
br label %31
31: ; preds = %56, %1
store i32 16, i32* %8
%32 = load i32, i32* %4
%33 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%34 = call i32 @accept(i32 %32, %struct.sockaddr* %33, i32* %8)
store i32 %34, i32* %3
%35 = call i32 (...) @fork()
store i32 %35, i32* %9
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %56
37: ; preds = %31
br label %38
38: ; preds = %55, %37
%39 = load i32, i32* %3
%40 = getelementptr inbounds [1500 x i8], [1500 x i8]* %10, i64 0, i64 0
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i32 @recvfrom(i32 %39, i8* %40, i32 1500, i32 0, %struct.sockaddr* %41, i32* %8)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %38
%46 = load i64, i64* @errno
%47 = load i64, i64* @ECONNRESET
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* %3
%51 = call i32 @close(i32 %50)
ret i32 0
52: ; preds = %45
%53 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %38
br label %38
56: ; preds = %31
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
br label %31
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@errno = global i64 0
@ECONNRESET = global i64 0
@.str.1 = [23 x i8] c"Problem receiving data\00"
@str = [9 x i8] c"Receiver\00"
define i32 @receiver(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [1500 x i8]
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 0, i32* %8
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = getelementptr inbounds [1500 x i8], [1500 x i8]* %5, i64 0, i64 0
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%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 = load i32, i32* @INADDR_ANY
%18 = tail call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 %0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i32 @bind(i32 %14, %struct.sockaddr* %22, i32 16)
%24 = call i32 @listen(i32 %14, i32 1024)
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
store i32 16, i32* %4
%26 = call i32 @accept(i32 %14, %struct.sockaddr* %25, i32* %4)
%27 = call i32 (...) @fork()
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %43, %1
%30 = phi i32 [ %26, %1 ], [ %46, %43 ]
br label %31
31: ; preds = %29, %31
%32 = call i32 @recvfrom(i32 %30, i8* %11, i32 1500, i32 0, %struct.sockaddr* %25, i32* %4)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %31
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @ECONNRESET
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @close(i32 %30)
ret i32 0
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %1, %43
%44 = phi i32 [ %46, %43 ], [ %26, %1 ]
%45 = call i32 @close(i32 %44)
store i32 16, i32* %4
%46 = call i32 @accept(i32 %14, %struct.sockaddr* %25, i32* %4)
%47 = call i32 (...) @fork()
%48 = icmp eq i32 %47, 0
br i1 %48, label %29, label %43
}