#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ LOG_ERR;
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
scalar_t__ close(int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ errno;
scalar_t__ execl(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int accept_client(int clientfd) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    return -1;
  }
  if (pid != 0) {
    close(clientfd);
    waitpid(pid, NULL, 0);
    return 0;
  }
  if ((pid = fork()) < 0) {
    syslog(LOG_ERR, "fork(2) failed on 1st child in accept_client(): %s\n",
           strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (pid != 0)
    exit(EXIT_SUCCESS);
  if (dup2(clientfd, STDIN_FILENO) < 0) {
    syslog(LOG_ERR,
           "dup2(2) failed when attempting to duplicate onto stdin: %s\n",
           strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (dup2(clientfd, STDOUT_FILENO) < 0) {
    syslog(LOG_ERR,
           "dup2(2) failed when attempting to duplicate onto stdout: %s\n",
           strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (clientfd != STDIN_FILENO && clientfd != STDOUT_FILENO) {
    if (close(clientfd) < 0) {
      syslog(LOG_ERR,
             "Warning: close(2) error after successfully duplicating it to "
             "stdin and stdout: %s\n",
             strerror(errno));
    }
  }
  if (execl("/home/filipe/dev/interview-questions/knowledge_based/"
            "linux_internals/pseudo_terminals/pty",
            "/home/filipe/dev/interview-questions/knowledge_based/"
            "linux_internals/pseudo_terminals/pty",
            "/bin/login", NULL) < 0) {
    syslog(LOG_ERR, "execl(2) error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  assert(0);
}


x86 assembly (clang, -O0):
<accept_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11bc <accept_client+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c3 <accept_client+0x233>
cmpq   $0x0,-0x10(%rbp)
je     11ea <accept_client+0x5a>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
movl   $0x0,-0x4(%rbp)
jmp    13c3 <accept_client+0x233>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1238 <accept_client+0xa8>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
lea    0xddb(%rip),%rsi
call   1060 <syslog@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
je     1251 <accept_client+0xc1>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    12a5 <accept_client+0x115>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xda2(%rip),%rsi
call   1060 <syslog@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    12f9 <accept_client+0x169>
mov    0x2ce9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xd8a(%rip),%rsi
call   1060 <syslog@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
mov    0x2cbd(%rip),%rcx
cmp    (%rcx),%eax
je     135f <accept_client+0x1cf>
mov    -0x8(%rbp),%eax
mov    0x2cdb(%rip),%rcx
cmp    (%rcx),%eax
je     135f <accept_client+0x1cf>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    135a <accept_client+0x1ca>
mov    0x2c7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2c96(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xd58(%rip),%rsi
call   1060 <syslog@plt>
jmp    135f <accept_client+0x1cf>
lea    0xd9a(%rip),%rsi
lea    0xded(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10a0 <execl@plt>
cmp    $0x0,%rax
jge    13bc <accept_client+0x22c>
mov    0x2c26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xdbc(%rip),%rsi
call   1060 <syslog@plt>
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1050 <assert@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_client>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11c1 <accept_client+0x31>
je     11cb <accept_client+0x3b>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    1274 <accept_client+0xe4>
mov    $0xffffffff,%ebp
jmp    1274 <accept_client+0xe4>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     127b <accept_client+0xeb>
jne    129b <accept_client+0x10b>
mov    0x2dd8(%rip),%r14
mov    (%r14),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     12a9 <accept_client+0x119>
mov    0x2dee(%rip),%rbx
mov    (%rbx),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     12c9 <accept_client+0x139>
cmp    %ebp,(%r14)
je     1250 <accept_client+0xc0>
cmp    %ebp,(%rbx)
je     1250 <accept_client+0xc0>
mov    %ebp,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    1250 <accept_client+0xc0>
mov    0x2d80(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe66(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1060 <syslog@plt>
lea    0xea9(%rip),%rdi
lea    0xefc(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   10a0 <execl@plt>
test   %rax,%rax
js     12e9 <accept_client+0x159>
xor    %edi,%edi
call   1050 <assert@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d2e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd67(%rip),%rsi
jmp    1307 <accept_client+0x177>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%ebx
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd6d(%rip),%rsi
jmp    1307 <accept_client+0x177>
mov    0x2ce0(%rip),%rax
mov    (%rax),%ebx
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd89(%rip),%rsi
jmp    1307 <accept_client+0x177>
mov    0x2cc0(%rip),%rax
mov    (%rax),%ebx
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe5e(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1060 <syslog@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [52 x i8] c"fork(2) failed on 1st child in accept_client(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [60 x i8] c"dup2(2) failed when attempting to duplicate onto stdin: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [61 x i8] c"dup2(2) failed when attempting to duplicate onto stdout: %s\0A\00"
@.str.3 = [83 x i8] c"Warning: close(2) error after successfully duplicating it to stdin and stdout: %s\0A\00"
@.str.4 = [90 x i8] c"/home/filipe/dev/interview-questions/knowledge_based/linux_internals/pseudo_terminals/pty\00"
@.str.5 = [11 x i8] c"/bin/login\00"
@.str.6 = [20 x i8] c"execl(2) error: %s\0A\00"
define i32 @accept_client(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
store i32 %0, i32* %3
%5 = call i64 (...) @fork()
store i64 %5, i64* %4
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %1
store i32 -1, i32* %2
br label %86
8: ; preds = %1
%9 = load i64, i64* %4
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = load i32, i32* %3
%13 = call i64 @close(i32 %12)
%14 = load i64, i64* %4
%15 = call i32 @waitpid(i64 %14, i32* null, i32 0)
store i32 0, i32* %2
br label %86
16: ; preds = %8
%17 = call i64 (...) @fork()
store i64 %17, i64* %4
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i32, i32* @LOG_ERR
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @syslog(i32 %20, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
%27 = load i64, i64* %4
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @EXIT_SUCCESS
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %26
%33 = load i32, i32* %3
%34 = load i32, i32* @STDIN_FILENO
%35 = call i64 @dup2(i32 %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i32, i32* @LOG_ERR
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 @syslog(i32 %38, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %32
%45 = load i32, i32* %3
%46 = load i32, i32* @STDOUT_FILENO
%47 = call i64 @dup2(i32 %45, i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %44
%50 = load i32, i32* @LOG_ERR
%51 = load i32, i32* @errno
%52 = call i32 @strerror(i32 %51)
%53 = call i32 @syslog(i32 %50, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %52)
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %44
%57 = load i32, i32* %3
%58 = load i32, i32* @STDIN_FILENO
%59 = icmp ne i32 %57, %58
br i1 %59, label %60, label %74
60: ; preds = %56
%61 = load i32, i32* %3
%62 = load i32, i32* @STDOUT_FILENO
%63 = icmp ne i32 %61, %62
br i1 %63, label %64, label %74
64: ; preds = %60
%65 = load i32, i32* %3
%66 = call i64 @close(i32 %65)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %64
%69 = load i32, i32* @LOG_ERR
%70 = load i32, i32* @errno
%71 = call i32 @strerror(i32 %70)
%72 = call i32 @syslog(i32 %69, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.3, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %68, %64
br label %74
74: ; preds = %73, %60, %56
%75 = call i64 @execl(i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32* null)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %84
77: ; preds = %74
%78 = load i32, i32* @LOG_ERR
%79 = load i32, i32* @errno
%80 = call i32 @strerror(i32 %79)
%81 = call i32 @syslog(i32 %78, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %80)
%82 = load i32, i32* @EXIT_FAILURE
%83 = call i32 @exit(i32 %82)
unreachable
84: ; preds = %74
%85 = call i32 @assert(i32 0)
br label %86
86: ; preds = %84, %11, %7
%87 = load i32, i32* %2
ret i32 %87
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [52 x i8] c"fork(2) failed on 1st child in accept_client(): %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [60 x i8] c"dup2(2) failed when attempting to duplicate onto stdin: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [61 x i8] c"dup2(2) failed when attempting to duplicate onto stdout: %s\0A\00"
@.str.3 = [83 x i8] c"Warning: close(2) error after successfully duplicating it to stdin and stdout: %s\0A\00"
@.str.4 = [90 x i8] c"/home/filipe/dev/interview-questions/knowledge_based/linux_internals/pseudo_terminals/pty\00"
@.str.5 = [11 x i8] c"/bin/login\00"
@.str.6 = [20 x i8] c"execl(2) error: %s\0A\00"
define i32 @accept_client(i32 %0) {
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %72, label %4
4: ; preds = %1
%5 = icmp eq i64 %2, 0
br i1 %5, label %9, label %6
6: ; preds = %4
%7 = tail call i64 @close(i32 %0)
%8 = tail call i32 @waitpid(i64 %2, i32* null, i32 0)
br label %72
9: ; preds = %4
%10 = tail call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %9
%13 = load i32, i32* @LOG_ERR
%14 = load i32, i32* @errno
%15 = tail call i32 @strerror(i32 %14)
%16 = tail call i32 @syslog(i32 %13, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %9
%20 = icmp eq i64 %10, 0
br i1 %20, label %24, label %21
21: ; preds = %19
%22 = load i32, i32* @EXIT_SUCCESS
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %19
%25 = load i32, i32* @STDIN_FILENO
%26 = tail call i64 @dup2(i32 %0, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = load i32, i32* @LOG_ERR
%30 = load i32, i32* @errno
%31 = tail call i32 @strerror(i32 %30)
%32 = tail call i32 @syslog(i32 %29, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %24
%36 = load i32, i32* @STDOUT_FILENO
%37 = tail call i64 @dup2(i32 %0, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i32, i32* @LOG_ERR
%41 = load i32, i32* @errno
%42 = tail call i32 @strerror(i32 %41)
%43 = tail call i32 @syslog(i32 %40, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = load i32, i32* @EXIT_FAILURE
%45 = tail call i32 @exit(i32 %44)
unreachable
46: ; preds = %35
%47 = load i32, i32* @STDIN_FILENO
%48 = icmp eq i32 %47, %0
%49 = load i32, i32* @STDOUT_FILENO
%50 = icmp eq i32 %49, %0
%51 = select i1 %48, i1 true, i1 %50
br i1 %51, label %60, label %52
52: ; preds = %46
%53 = tail call i64 @close(i32 %0)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i32, i32* @LOG_ERR
%57 = load i32, i32* @errno
%58 = tail call i32 @strerror(i32 %57)
%59 = tail call i32 @syslog(i32 %56, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.3, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %52, %55, %46
%61 = tail call i64 @execl(i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i32* null)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %60
%64 = load i32, i32* @LOG_ERR
%65 = load i32, i32* @errno
%66 = tail call i32 @strerror(i32 %65)
%67 = tail call i32 @syslog(i32 %64, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %66)
%68 = load i32, i32* @EXIT_FAILURE
%69 = tail call i32 @exit(i32 %68)
unreachable
70: ; preds = %60
%71 = tail call i32 @assert(i32 0)
br label %72
72: ; preds = %1, %70, %6
%73 = phi i32 [ 0, %6 ], [ undef, %70 ], [ -1, %1 ]
ret i32 %73
}