#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGTERM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int errno;
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
void tcp_server(pid_t partner, int listen_fd) {
  int error, accept_fd;
  accept_fd = accept(listen_fd, NULL, NULL);
  if (accept_fd < 0) {
    error = errno;
    (void)kill(partner, SIGTERM);
    errno = error;
    err(-1, "tcp_server: accept");
  }
  close(accept_fd);
  close(listen_fd);
}


x86 assembly (clang, -O0):
<tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11a4 <tcp_server+0x64>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
mov    -0xc(%rbp),%ecx
mov    0x2e47(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffff,%edi
lea    0xe61(%rip),%rsi
call   1050 <err@plt>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebx
mov    %edi,%r14d
mov    %esi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    118d <tcp_server+0x4d>
mov    0x2e73(%rip),%r15
mov    (%r15),%r12d
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <kill@plt>
mov    %r12d,(%r15)
lea    0xe7d(%rip),%rsi
mov    $0xffffffff,%edi
call   1050 <err@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>

LLVM IR (-O0):
@errno = global i32 0
@SIGTERM = global i32 0
@.str = [19 x i8] c"tcp_server: accept\00"
define void @tcp_server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = call i32 @accept(i32 %7, i32* null, i32* null)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @errno
store i32 %12, i32* %5
%13 = load i32, i32* %3
%14 = load i32, i32* @SIGTERM
%15 = call i32 @kill(i32 %13, i32 %14)
%16 = load i32, i32* %5
store i32 %16, i32* @errno
%17 = call i32 @err(i32 -1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %11, %2
%19 = load i32, i32* %6
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@SIGTERM = global i32 0
@.str = [19 x i8] c"tcp_server: accept\00"
define void @tcp_server(i32 %0, i32 %1) {
%3 = tail call i32 @accept(i32 %1, i32* null, i32* null)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = load i32, i32* @SIGTERM
%8 = tail call i32 @kill(i32 %0, i32 %7)
store i32 %6, i32* @errno
%9 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %5, %2
%11 = tail call i32 @close(i32 %3)
%12 = tail call i32 @close(i32 %1)
ret void
}