#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 */
struct sigaction {
  int dummy;
};

/* Variables and functions */
int EINTR;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ WNOHANG;
int errno;
int fork();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int receive_frk(int);
int send_frk(int);
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int talk(int sock) {
  int ret;
  int pid;
  struct sigaction sa;
  pid = fork();
  if (pid == -1)
    return 0;
  if (pid == 0) {
    ret = receive_frk(sock);
    if (ret && errno != EINTR)
      printf("El interlocutor se desconectó\n");
  } else {
    ret = send_frk(sock);
    int saved_errno = errno;
    if (waitpid(-1, NULL, WNOHANG) == 0)
      kill(pid, SIGTERM);
    errno = saved_errno;
  }
  return ret;
}


x86 assembly (clang, -O0):
<talk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    118b <talk+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    122e <talk+0xce>
cmpl   $0x0,-0x10(%rbp)
jne    11d3 <talk+0x73>
mov    -0x8(%rbp),%edi
call   1070 <receive_frk@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11ce <talk+0x6e>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%eax
je     11ce <talk+0x6e>
lea    0xe37(%rip),%rdi
call   1030 <printf@plt>
jmp    1228 <talk+0xc8>
mov    -0x8(%rbp),%edi
call   1050 <send_frk@plt>
mov    %eax,-0xc(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jne    121c <talk+0xbc>
mov    -0x10(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
call   1040 <kill@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2daa(%rip),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<talk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11ef <talk+0x8f>
mov    %eax,%ebp
mov    %ebx,%edi
test   %eax,%eax
jne    11b0 <talk+0x50>
call   1070 <receive_frk@plt>
test   %eax,%eax
je     11ef <talk+0x8f>
mov    %eax,%ebp
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%eax
je     11ab <talk+0x4b>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%r14d
jmp    11ef <talk+0x8f>
call   1050 <send_frk@plt>
mov    %eax,%r14d
mov    0x2e11(%rip),%r15
mov    (%r15),%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1060 <waitpid@plt>
test   %rax,%rax
jne    11ec <talk+0x8c>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <kill@plt>
mov    %ebx,(%r15)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sigaction = type { i32 }
@errno = global i32 0
@EINTR = global i32 0
@.str = [32 x i8] c"El interlocutor se desconect\C3\B3\0A\00"
@WNOHANG = global i32 0
@SIGTERM = global i32 0
define i32 @talk(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sigaction
%7 = alloca i32
store i32 %0, i32* %3
%8 = call i32 (...) @fork()
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* %2
br label %42
12: ; preds = %1
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = load i32, i32* %3
%17 = call i32 @receive_frk(i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i32, i32* @errno
%22 = load i32, i32* @EINTR
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %20, %15
br label %40
27: ; preds = %12
%28 = load i32, i32* %3
%29 = call i32 @send_frk(i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* @errno
store i32 %30, i32* %7
%31 = load i32, i32* @WNOHANG
%32 = call i64 @waitpid(i32 -1, i32* null, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = load i32, i32* %5
%36 = load i32, i32* @SIGTERM
%37 = call i32 @kill(i32 %35, i32 %36)
br label %38
38: ; preds = %34, %27
%39 = load i32, i32* %7
store i32 %39, i32* @errno
br label %40
40: ; preds = %38, %26
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %11
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@WNOHANG = global i32 0
@SIGTERM = global i32 0
@str = [31 x i8] c"El interlocutor se desconect\C3\B3\00"
define i32 @talk(i32 %0) {
%2 = tail call i32 (...) @fork()
switch i32 %2, label %12 [
i32 -1, label %22
i32 0, label %3
]
3: ; preds = %1
%4 = tail call i32 @receive_frk(i32 %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = load i32, i32* @EINTR
%9 = icmp eq i32 %7, %8
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %22
12: ; preds = %1
%13 = tail call i32 @send_frk(i32 %0)
%14 = load i32, i32* @errno
%15 = load i32, i32* @WNOHANG
%16 = tail call i64 @waitpid(i32 -1, i32* null, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i32, i32* @SIGTERM
%20 = tail call i32 @kill(i32 %2, i32 %19)
br label %21
21: ; preds = %18, %12
store i32 %14, i32* @errno
br label %22
22: ; preds = %21, %10, %6, %3, %1
%23 = phi i32 [ 0, %1 ], [ %4, %10 ], [ %4, %6 ], [ 0, %3 ], [ %13, %21 ]
ret i32 %23
}