#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 sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sender(int port) {
  printf("Sender\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 = sendto(connfd, msg, 1500, 0, (struct sockaddr *)&client,
                   client_len);
        if (n < 0) {
          if (errno == ECONNRESET) {
            perror("connection closed normally... probably");
            close(connfd);
            return 0;
          }
          perror("Problem receiving data");
          exit(-1);
        }
      }
    }
    close(connfd);
  }
  close(listenfd);
  return 0;
}


x86 assembly (clang, -O0):
<sender>:
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   1090 <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    131d <sender+0x14d>
jmp    129e <sender+0xce>
mov    -0x8(%rbp),%edi
lea    -0x620(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    -0x34(%rbp),%r9d
mov    $0x5dc,%edx
xor    %ecx,%ecx
call   1070 <sendto@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1318 <sender+0x148>
mov    0x2cf0(%rip),%rax
mov    (%rax),%rax
mov    0x2cf6(%rip),%rcx
cmp    (%rcx),%rax
jne    1302 <sender+0x132>
lea    0xd1e(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x620,%rsp
pop    %rbp
ret
lea    0xd26(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
jmp    129e <sender+0xce>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
jmp    126c <sender+0x9c>

x86 assembly (clang, -O2):
<sender>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x618,%rsp
mov    %edi,%r14d
lea    0xe67(%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   1080 <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 <sender+0xe9>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1070 <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 <sender+0xc0>
lea    0x30(%rsp),%r14
lea    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%r9d
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x5dc,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1060 <sendto@plt>
test   %eax,%eax
jns    12c0 <sender+0x100>
mov    0x2cdc(%rip),%rax
mov    (%rax),%rax
mov    0x2ce2(%rip),%rcx
cmp    (%rcx),%rax
jne    1316 <sender+0x156>
lea    0xd06(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x618,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0a(%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 = [8 x i8] c"Sender\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 = [39 x i8] c"connection closed normally... probably\00"
@.str.2 = [23 x i8] c"Problem receiving data\00"
define i32 @sender(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 ([8 x i8], [8 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 = %58, %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 %58
37: ; preds = %31
br label %38
38: ; preds = %57, %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 = load i32, i32* %8
%43 = call i32 @sendto(i32 %39, i8* %40, i32 1500, i32 0, %struct.sockaddr* %41, i32 %42)
store i32 %43, i32* %5
%44 = load i32, i32* %5
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %38
%47 = load i64, i64* @errno
%48 = load i64, i64* @ECONNRESET
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%52 = load i32, i32* %3
%53 = call i32 @close(i32 %52)
ret i32 0
54: ; preds = %46
%55 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %38
br label %38
58: ; preds = %31
%59 = load i32, i32* %3
%60 = call i32 @close(i32 %59)
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 = [39 x i8] c"connection closed normally... probably\00"
@.str.2 = [23 x i8] c"Problem receiving data\00"
@str = [7 x i8] c"Sender\00"
define i32 @sender(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 ([7 x i8], [7 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 %45
29: ; preds = %45, %1
%30 = phi i32 [ %26, %1 ], [ %48, %45 ]
br label %31
31: ; preds = %29, %31
%32 = load i32, i32* %4
%33 = call i32 @sendto(i32 %30, i8* %11, i32 1500, i32 0, %struct.sockaddr* %25, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %31
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @ECONNRESET
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @close(i32 %30)
ret i32 0
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %1, %45
%46 = phi i32 [ %48, %45 ], [ %26, %1 ]
%47 = call i32 @close(i32 %46)
store i32 16, i32* %4
%48 = call i32 @accept(i32 %14, %struct.sockaddr* %25, i32* %4)
%49 = call i32 (...) @fork()
%50 = icmp eq i32 %49, 0
br i1 %50, label %29, label %45
}