#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;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(void *, int, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(void) {
  int fd, addrlen, newfd;
  int n, nw;
  struct sockaddr_in addr;
  char *ptr, buffer[128];
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    exit(1);
  }
  memset((void *)&addr, (int)'\0', sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(59000);
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    exit(1);
  }
  while (1) {
    addrlen = sizeof(addr);
    if ((newfd = accept(fd, (struct sockaddr *)&addr, &addrlen)) == -1) {
      exit(1);
    }
    while ((n = read(newfd, buffer, 128)) != 0) {
      if (n == 1) {
        exit(1);
      }
      ptr = &buffer[0];
      while (n > 0) {
        if ((nw = write(newfd, ptr, n)) <= 0) {
          exit(1);
        }
        n -= nw;
        ptr += nw;
      }
    }
    close(newfd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11e1 <main+0x41>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0xe678,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    123f <main+0x9f>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1244 <main+0xa4>
movl   $0xc,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1271 <main+0xd1>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1276 <main+0xd6>
mov    -0x10(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x80,%edx
call   1080 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     130a <main+0x16a>
cmpl   $0x1,-0x14(%rbp)
jne    12aa <main+0x10a>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0xb0(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1305 <main+0x165>
mov    -0x10(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     12e4 <main+0x144>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    12b5 <main+0x115>
jmp    1276 <main+0xd6>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
jmp    1244 <main+0xa4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    0x2e30(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xa2>
mov    %eax,%r12d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0xe678,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xa2>
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r12d,%edi
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0xac>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %eax,%ebp
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
jmp    1280 <main+0xe0>
mov    %ebp,%edi
call   1070 <close@plt>
movl   $0xc,0xc(%rsp)
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    %r15,%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1242 <main+0xa2>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x80,%edx
call   1080 <read@plt>
test   %eax,%eax
je     125a <main+0xba>
mov    %eax,%ebx
cmp    $0x1,%eax
je     1242 <main+0xa2>
jl     1280 <main+0xe0>
mov    %r14,%r13
nop
mov    %ebp,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    1242 <main+0xa2>
mov    %eax,%ecx
add    %rcx,%r13
sub    %eax,%ebx
jg     12a0 <main+0x100>
jmp    1280 <main+0xe0>

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
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i8*
%9 = alloca [128 x i8]
store i32 0, i32* %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = bitcast %struct.sockaddr_in* %7 to i8*
%18 = call i32 @memset(i8* %17, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
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* %7, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = call i32 @htons(i32 59000)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %16
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %16
br label %34
34: ; preds = %33, %74
store i32 12, i32* %3
%35 = load i32, i32* %2
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%37 = call i32 @accept(i32 %35, %struct.sockaddr* %36, i32* %3)
store i32 %37, i32* %4
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %34
br label %42
42: ; preds = %73, %41
%43 = load i32, i32* %4
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 128)
store i32 %45, i32* %5
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %74
47: ; preds = %42
%48 = load i32, i32* %5
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %47
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
store i8* %53, i8** %8
br label %54
54: ; preds = %65, %52
%55 = load i32, i32* %5
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %73
57: ; preds = %54
%58 = load i32, i32* %4
%59 = load i8*, i8** %8
%60 = load i32, i32* %5
%61 = call i32 @write(i32 %58, i8* %59, i32 %60)
store i32 %61, i32* %6
%62 = icmp sle i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %57
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %57
%66 = load i32, i32* %6
%67 = load i32, i32* %5
%68 = sub nsw i32 %67, %66
store i32 %68, i32* %5
%69 = load i32, i32* %6
%70 = load i8*, i8** %8
%71 = sext i32 %69 to i64
%72 = getelementptr inbounds i8, i8* %70, i64 %71
store i8* %72, i8** %8
br label %54
73: ; preds = %54
br label %42
74: ; preds = %42
%75 = load i32, i32* %4
%76 = call i32 @close(i32 %75)
br label %34
}

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
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca [128 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 @memset(i8* %5, 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 = load i32, i32* @INADDR_ANY
%18 = 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 = call i32 @htons(i32 59000)
%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 %9, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %28, label %25
25: ; preds = %13
store i32 12, i32* %1
%26 = call i32 @accept(i32 %9, %struct.sockaddr* %22, i32* %1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %30
28: ; preds = %13
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %25, %53
%31 = phi i32 [ %55, %53 ], [ %26, %25 ]
br label %34
32: ; preds = %53, %25
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %38, %30
%35 = call i32 @read(i32 %31, i8* %6, i32 128)
switch i32 %35, label %36 [
i32 0, label %53
i32 1, label %39
]
36: ; preds = %34
%37 = icmp sgt i32 %35, 0
br i1 %37, label %41, label %38
38: ; preds = %48, %36
br label %34
39: ; preds = %34
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %36, %48
%42 = phi i8* [ %51, %48 ], [ %6, %36 ]
%43 = phi i32 [ %49, %48 ], [ %35, %36 ]
%44 = call i32 @write(i32 %31, i8* %42, i32 %43)
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %41
%49 = sub nsw i32 %43, %44
%50 = zext i32 %44 to i64
%51 = getelementptr inbounds i8, i8* %42, i64 %50
%52 = icmp sgt i32 %49, 0
br i1 %52, label %41, label %38
53: ; preds = %34
%54 = call i32 @close(i32 %31)
store i32 12, i32* %1
%55 = call i32 @accept(i32 %9, %struct.sockaddr* %22, i32* %1)
%56 = icmp eq i32 %55, -1
br i1 %56, label %32, label %30
}