#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 /*<<< orphan*/ yes;
struct TYPE_2__ {
  int /*<<< orphan*/ 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*/ message;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  struct sockaddr_in addr, con_addr;
  int rc, sd, cd, yes = 1, fd2, bytes_received;
  socklen_t alen = sizeof(struct sockaddr_in);
  char message[64 * 1024];
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(7177);
  sd = socket(PF_INET, SOCK_STREAM, 0);
  setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  bind(sd, (struct sockaddr *)&addr, sizeof(addr));
  listen(sd, 5);
  for (;;) {
    cd = accept(sd, (struct sockaddr *)&con_addr, &alen);
    bytes_received = 0;
    fd2 = open("outfile", O_RDWR | O_CREAT | O_TRUNC, 0666);
    printf("\nAccepting input on port %d\n", 7177);
    do {
      rc = recv(cd, message, sizeof(message), 0);
      bytes_received += rc;
      write(fd2, message, rc);
    } while (rc > 0);
    printf("Received:  %d bytes\n", bytes_received);
    printf("Received the end of input\n\n");
    close(cd);
    close(fd2);
  }
  close(sd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
movl   $0xc,-0x3c(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    $0x1c09,%edi
call   1060 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    -0x28(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    -0x28(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x38(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    0x2d66(%rip),%rax
or     (%rax),%esi
mov    0x2d55(%rip),%rax
or     (%rax),%esi
lea    0xd5c(%rip),%rdi
mov    $0x1b6,%edx
call   10c0 <open@plt>
mov    %eax,-0x34(%rbp)
lea    0xd50(%rip),%rdi
mov    $0x1c09,%esi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x2c(%rbp),%edi
lea    -0x10040(%rbp),%rsi
mov    $0x10000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x10040(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <write@plt>
cmpl   $0x0,-0x24(%rbp)
jg     12c4 <main+0x104>
mov    -0x38(%rbp),%esi
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
mov    -0x34(%rbp),%edi
call   1090 <close@plt>
jmp    1268 <main+0xa8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10038,%rsp
movl   $0x1,0xc(%rsp)
movl   $0xc,0x8(%rsp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x1c09,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2d57(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
lea    0x30(%rsp),%rbp
nop
mov    %ebx,%edi
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   10e0 <accept@plt>
mov    %eax,%r12d
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    0x2d34(%rip),%rax
or     (%rax),%esi
mov    0x2d43(%rip),%rax
or     (%rax),%esi
lea    0xd4a(%rip),%rdi
mov    $0x1b6,%edx
call   10d0 <open@plt>
mov    %eax,%r13d
lea    0xd3e(%rip),%rdi
mov    $0x1c09,%esi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x10000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r15d
add    %eax,%r14d
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1060 <write@plt>
test   %r15d,%r15d
jg     12e0 <main+0x110>
lea    0xd14(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xd18(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
call   10a0 <close@plt>
mov    %r13d,%edi
call   10a0 <close@plt>
jmp    1280 <main+0xb0>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [8 x i8] c"outfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [29 x i8] c"\0AAccepting input on port %d\0A\00"
@.str.2 = [21 x i8] c"Received: %d bytes\0A\00"
@.str.3 = [28 x i8] c"Received the end of input\0A\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [65536 x i8]
store i32 0, i32* %1
store i32 1, i32* %7
store i32 12, i32* %10
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 @htons(i32 7177)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %7, i32 4)
%27 = load i32, i32* %5
%28 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = load i32, i32* %5
%31 = call i32 @listen(i32 %30, i32 5)
br label %32
32: ; preds = %57, %0
%33 = load i32, i32* %5
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i32 @accept(i32 %33, %struct.sockaddr* %34, i32* %10)
store i32 %35, i32* %6
store i32 0, i32* %9
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %40, i32 438)
store i32 %41, i32* %8
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 7177)
br label %43
43: ; preds = %54, %32
%44 = load i32, i32* %6
%45 = getelementptr inbounds [65536 x i8], [65536 x i8]* %11, i64 0, i64 0
%46 = call i32 @recv(i32 %44, i8* %45, i32 65536, i32 0)
store i32 %46, i32* %4
%47 = load i32, i32* %4
%48 = load i32, i32* %9
%49 = add nsw i32 %48, %47
store i32 %49, i32* %9
%50 = load i32, i32* %8
%51 = getelementptr inbounds [65536 x i8], [65536 x i8]* %11, i64 0, i64 0
%52 = load i32, i32* %4
%53 = call i32 @write(i32 %50, i8* %51, i32 %52)
br label %54
54: ; preds = %43
%55 = load i32, i32* %4
%56 = icmp sgt i32 %55, 0
br i1 %56, label %43, label %57
57: ; preds = %54
%58 = load i32, i32* %9
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
br label %32
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [8 x i8] c"outfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [29 x i8] c"\0AAccepting input on port %d\0A\00"
@.str.2 = [21 x i8] c"Received: %d bytes\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [27 x i8] c"Received the end of input\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca [65536 x i8]
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%9 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%10 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = tail call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 7177)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %3, i32 4)
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i32 @bind(i32 %20, %struct.sockaddr* %24, i32 12)
%26 = call i32 @listen(i32 %20, i32 5)
%27 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %28
28: ; preds = %43, %0
%29 = call i32 @accept(i32 %20, %struct.sockaddr* %27, i32* %4)
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_CREAT
%32 = or i32 %31, %30
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %34, i32 438)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 7177)
br label %37
37: ; preds = %37, %28
%38 = phi i32 [ 0, %28 ], [ %40, %37 ]
%39 = call i32 @recv(i32 %29, i8* %10, i32 65536, i32 0)
%40 = add nsw i32 %39, %38
%41 = call i32 @write(i32 %35, i8* %10, i32 %39)
%42 = icmp sgt i32 %39, 0
br i1 %42, label %37, label %43
43: ; preds = %37
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%46 = call i32 @close(i32 %29)
%47 = call i32 @close(i32 %35)
br label %28
}