#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 */

/* Type definitions */
typedef int ssize_t;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ ch;

/* Variables and functions */
int EPIPE;
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ SIGTERM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int errno;
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
void tcp_server(pid_t partner, int listen_fd) {
  int error, accept_fd;
  ssize_t len;
  char ch;
  accept_fd = accept(listen_fd, NULL, NULL);
  if (accept_fd < 0) {
    error = errno;
    (void)kill(partner, SIGTERM);
    errno = error;
    err(-1, "tcp_server: accept");
  }
  ch = 'A';
  len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL);
  if (len < 0) {
    error = errno;
    (void)kill(partner, SIGTERM);
    errno = error;
    err(-1, "tcp_server: send (1)");
  }
  if (len != sizeof(ch)) {
    (void)kill(partner, SIGTERM);
    errx(-1, "tcp_server: send (1) len");
  }
  sleep(10);
  ch = 'A';
  len = send(accept_fd, &ch, sizeof(ch), MSG_NOSIGNAL);
  if (len >= 0) {
    (void)kill(partner, SIGTERM);
    errx(-1, "tcp_server: send (2): success");
  } else if (errno != EPIPE) {
    error = errno;
    (void)kill(partner, SIGTERM);
    errno = error;
    err(-1, "tcp_server: send (2)");
  }
  close(accept_fd);
  close(listen_fd);
}


x86 assembly (clang, -O0):
<tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1080 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11d4 <tcp_server+0x64>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0xc(%rbp),%ecx
mov    0x2e0f(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffff,%edi
lea    0xe31(%rip),%rsi
call   1070 <err@plt>
movb   $0x41,-0x15(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%ecx
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <send@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1239 <tcp_server+0xc9>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0xc(%rbp),%ecx
mov    0x2daa(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffff,%edi
lea    0xddf(%rip),%rsi
call   1070 <err@plt>
movslq -0x14(%rbp),%rax
cmp    $0x1,%rax
je     1269 <tcp_server+0xf9>
mov    -0x4(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    $0xffffffff,%edi
lea    0xdc4(%rip),%rsi
call   1030 <errx@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
movb   $0x41,-0x15(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%ecx
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <send@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     12c5 <tcp_server+0x155>
mov    -0x4(%rbp),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    $0xffffffff,%edi
lea    0xd86(%rip),%rsi
call   1030 <errx@plt>
jmp    131c <tcp_server+0x1ac>
mov    0x2d04(%rip),%rax
mov    (%rax),%eax
mov    0x2d03(%rip),%rcx
cmp    (%rcx),%eax
je     1317 <tcp_server+0x1a7>
mov    0x2cec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2cf5(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
mov    -0xc(%rbp),%ecx
mov    0x2ccc(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffff,%edi
lea    0xd4d(%rip),%rsi
call   1070 <err@plt>
jmp    131c <tcp_server+0x1ac>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r12d
mov    %esi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c1 <tcp_server+0x51>
mov    0x2e37(%rip),%r15
mov    (%r15),%ebx
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1060 <kill@plt>
mov    %ebx,(%r15)
lea    0xe49(%rip),%rsi
mov    $0xffffffff,%edi
call   1070 <err@plt>
movb   $0x41,0x7(%rsp)
mov    0x2dfb(%rip),%r15
mov    (%r15),%ecx
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <send@plt>
test   %eax,%eax
js     11ec <tcp_server+0x7c>
cmp    $0x1,%eax
jne    121d <tcp_server+0xad>
jmp    123f <tcp_server+0xcf>
mov    0x2ddd(%rip),%r13
mov    0x0(%r13),%ebx
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1060 <kill@plt>
mov    %ebx,0x0(%r13)
lea    0xe00(%rip),%rsi
mov    $0xffffffff,%edi
call   1070 <err@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1060 <kill@plt>
lea    0xdf3(%rip),%rsi
mov    $0xffffffff,%edi
call   1030 <errx@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
movb   $0x41,0x7(%rsp)
mov    (%r15),%ecx
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <send@plt>
test   %eax,%eax
js     128a <tcp_server+0x11a>
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1060 <kill@plt>
lea    0xdc3(%rip),%rsi
mov    $0xffffffff,%edi
call   1030 <errx@plt>
jmp    12c4 <tcp_server+0x154>
mov    0x2d3f(%rip),%r15
mov    (%r15),%ebx
mov    0x2d3d(%rip),%rax
cmp    (%rax),%ebx
je     12c4 <tcp_server+0x154>
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1060 <kill@plt>
mov    %ebx,(%r15)
lea    0xda5(%rip),%rsi
mov    $0xffffffff,%edi
call   1070 <err@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@SIGTERM = global i32 0
@.str = [19 x i8] c"tcp_server: accept\00"
@MSG_NOSIGNAL = global i32 0
@.str.1 = [21 x i8] c"tcp_server: send (1)\00"
@.str.2 = [25 x i8] c"tcp_server: send (1) len\00"
@.str.3 = [30 x i8] c"tcp_server: send (2): success\00"
@EPIPE = global i32 0
@.str.4 = [21 x i8] c"tcp_server: send (2)\00"
define void @tcp_server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = call i32 @accept(i32 %9, i32* null, i32* null)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @errno
store i32 %14, i32* %5
%15 = load i32, i32* %3
%16 = load i32, i32* @SIGTERM
%17 = call i32 @kill(i32 %15, i32 %16)
%18 = load i32, i32* %5
store i32 %18, i32* @errno
%19 = call i32 @err(i32 -1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %13, %2
store i8 65, i8* %8
%21 = load i32, i32* %6
%22 = load i32, i32* @MSG_NOSIGNAL
%23 = call i32 @send(i32 %21, i8* %8, i32 1, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i32, i32* @errno
store i32 %27, i32* %5
%28 = load i32, i32* %3
%29 = load i32, i32* @SIGTERM
%30 = call i32 @kill(i32 %28, i32 %29)
%31 = load i32, i32* %5
store i32 %31, i32* @errno
%32 = call i32 @err(i32 -1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %26, %20
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = icmp ne i64 %35, 1
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* %3
%39 = load i32, i32* @SIGTERM
%40 = call i32 @kill(i32 %38, i32 %39)
%41 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %37, %33
%43 = call i32 @sleep(i32 10)
store i8 65, i8* %8
%44 = load i32, i32* %6
%45 = load i32, i32* @MSG_NOSIGNAL
%46 = call i32 @send(i32 %44, i8* %8, i32 1, i32 %45)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp sge i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i32, i32* %3
%51 = load i32, i32* @SIGTERM
%52 = call i32 @kill(i32 %50, i32 %51)
%53 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %66
54: ; preds = %42
%55 = load i32, i32* @errno
%56 = load i32, i32* @EPIPE
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %65
58: ; preds = %54
%59 = load i32, i32* @errno
store i32 %59, i32* %5
%60 = load i32, i32* %3
%61 = load i32, i32* @SIGTERM
%62 = call i32 @kill(i32 %60, i32 %61)
%63 = load i32, i32* %5
store i32 %63, i32* @errno
%64 = call i32 @err(i32 -1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %58, %54
br label %66
66: ; preds = %65, %49
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %4
%70 = call i32 @close(i32 %69)
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@SIGTERM = global i32 0
@.str = [19 x i8] c"tcp_server: accept\00"
@MSG_NOSIGNAL = global i32 0
@.str.1 = [21 x i8] c"tcp_server: send (1)\00"
@.str.2 = [25 x i8] c"tcp_server: send (1) len\00"
@.str.3 = [30 x i8] c"tcp_server: send (2): success\00"
@EPIPE = global i32 0
@.str.4 = [21 x i8] c"tcp_server: send (2)\00"
define void @tcp_server(i32 %0, i32 %1) {
%3 = alloca i8
%4 = tail call i32 @accept(i32 %1, i32* null, i32* null)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = load i32, i32* @SIGTERM
%9 = tail call i32 @kill(i32 %0, i32 %8)
store i32 %7, i32* @errno
%10 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %6, %2
store i8 65, i8* %3
%12 = load i32, i32* @MSG_NOSIGNAL
%13 = call i32 @send(i32 %4, i8* %3, i32 1, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = load i32, i32* @SIGTERM
%18 = call i32 @kill(i32 %0, i32 %17)
store i32 %16, i32* @errno
%19 = call i32 @err(i32 -1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %22
20: ; preds = %11
%21 = icmp eq i32 %13, 1
br i1 %21, label %26, label %22
22: ; preds = %15, %20
%23 = load i32, i32* @SIGTERM
%24 = call i32 @kill(i32 %0, i32 %23)
%25 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %22, %20
%27 = call i32 @sleep(i32 10)
store i8 65, i8* %3
%28 = load i32, i32* @MSG_NOSIGNAL
%29 = call i32 @send(i32 %4, i8* %3, i32 1, i32 %28)
%30 = icmp sgt i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* @SIGTERM
%33 = call i32 @kill(i32 %0, i32 %32)
%34 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %43
35: ; preds = %26
%36 = load i32, i32* @errno
%37 = load i32, i32* @EPIPE
%38 = icmp eq i32 %36, %37
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i32, i32* @SIGTERM
%41 = call i32 @kill(i32 %0, i32 %40)
store i32 %36, i32* @errno
%42 = call i32 @err(i32 -1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %35, %39, %31
%44 = call i32 @close(i32 %4)
%45 = call i32 @close(i32 %1)
ret void
}