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

/* Variables and functions */
int /*<<< orphan*/ ECONNABORTED;
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ cleanup_login_records(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int destroy_socket(int);
int /*<<< orphan*/ errno;
int fork();
scalar_t__ openpty(int *, int *, char *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ shell_loop(int, int);
int /*<<< orphan*/ start_interactive_shell(int, int *, int *);
int drop_pty_connection(int sock) {
  ((void)0);
  int pty, tty;
  char pty_name[5 + 255 + 1];
  if (openpty(&pty, &tty, pty_name, NULL, NULL) < 0) {
    ((void)0);
    return destroy_socket(sock);
  }
  ((void)0);
  cleanup_login_records(basename(pty_name));
  switch (fork()) {
  case 0:
    start_interactive_shell(sock, &pty, &tty);
  case -1:
    ((void)0);
    destroy_socket(sock);
    close(tty);
    return -1;
  default:
    close(tty);
  }
  switch (fork()) {
  case 0:
    shell_loop(sock, pty);
  case -1:
    ((void)0);
  default:
    close(sock);
    close(pty);
    errno = ECONNABORTED;
    return -1;
  }
}


x86 assembly (clang, -O0):
<drop_pty_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x120(%rbp),%rdx
lea    -0xc(%rbp),%rdi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1080 <openpty@plt>
cmp    $0x0,%rax
jge    11c4 <drop_pty_connection+0x44>
mov    -0x8(%rbp),%edi
call   1030 <destroy_socket@plt>
mov    %eax,-0x4(%rbp)
jmp    12a4 <drop_pty_connection+0x124>
lea    -0x120(%rbp),%rdi
call   1060 <basename@plt>
mov    %eax,%edi
call   1090 <cleanup_login_records@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x124(%rbp)
sub    $0xffffffff,%eax
je     1217 <drop_pty_connection+0x97>
jmp    11f4 <drop_pty_connection+0x74>
mov    -0x124(%rbp),%eax
test   %eax,%eax
jne    1233 <drop_pty_connection+0xb3>
jmp    1207 <drop_pty_connection+0x87>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1070 <start_interactive_shell@plt>
mov    -0x8(%rbp),%edi
call   1030 <destroy_socket@plt>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <drop_pty_connection+0x124>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x128(%rbp)
sub    $0xffffffff,%eax
je     1276 <drop_pty_connection+0xf6>
jmp    1258 <drop_pty_connection+0xd8>
mov    -0x128(%rbp),%eax
test   %eax,%eax
jne    127b <drop_pty_connection+0xfb>
jmp    126b <drop_pty_connection+0xeb>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <shell_loop@plt>
jmp    127b <drop_pty_connection+0xfb>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%ecx
mov    0x2d45(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_pty_connection>:
push   %rbx
sub    $0x120,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <openpty@plt>
test   %rax,%rax
js     11ec <drop_pty_connection+0x6c>
lea    0x10(%rsp),%rdi
call   1060 <basename@plt>
mov    %eax,%edi
call   1090 <cleanup_login_records@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11da <drop_pty_connection+0x5a>
test   %eax,%eax
jne    11f5 <drop_pty_connection+0x75>
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1070 <start_interactive_shell@plt>
mov    %ebx,%edi
call   1030 <destroy_socket@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
jmp    1236 <drop_pty_connection+0xb6>
mov    %ebx,%edi
call   1030 <destroy_socket@plt>
jmp    123b <drop_pty_connection+0xbb>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1214 <drop_pty_connection+0x94>
mov    0xc(%rsp),%esi
mov    %ebx,%edi
call   1050 <shell_loop@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    0x2dac(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x120,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@ECONNABORTED = global i32 0
@errno = global i32 0
define i32 @drop_pty_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [261 x i8]
store i32 %0, i32* %3
%7 = getelementptr inbounds [261 x i8], [261 x i8]* %6, i64 0, i64 0
%8 = call i64 @openpty(i32* %4, i32* %5, i8* %7, i32* null, i32* null)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i32 @destroy_socket(i32 %11)
store i32 %12, i32* %2
br label %42
13: ; preds = %1
%14 = getelementptr inbounds [261 x i8], [261 x i8]* %6, i64 0, i64 0
%15 = call i32 @basename(i8* %14)
%16 = call i32 @cleanup_login_records(i32 %15)
%17 = call i32 (...) @fork()
switch i32 %17, label %26 [
i32 0, label %18
i32 -1, label %21
]
18: ; preds = %13
%19 = load i32, i32* %3
%20 = call i32 @start_interactive_shell(i32 %19, i32* %4, i32* %5)
br label %21
21: ; preds = %13, %18
%22 = load i32, i32* %3
%23 = call i32 @destroy_socket(i32 %22)
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %2
br label %42
26: ; preds = %13
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
br label %29
29: ; preds = %26
%30 = call i32 (...) @fork()
switch i32 %30, label %36 [
i32 0, label %31
i32 -1, label %35
]
31: ; preds = %29
%32 = load i32, i32* %3
%33 = load i32, i32* %4
%34 = call i32 @shell_loop(i32 %32, i32 %33)
br label %35
35: ; preds = %29, %31
br label %36
36: ; preds = %29, %35
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @ECONNABORTED
store i32 %41, i32* @errno
store i32 -1, i32* %2
br label %42
42: ; preds = %36, %21, %10
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@ECONNABORTED = global i32 0
@errno = global i32 0
define i32 @drop_pty_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [261 x i8]
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [261 x i8], [261 x i8]* %4, i64 0, i64 0
%8 = call i64 @openpty(i32* %2, i32* %3, i8* %7, i32* null, i32* null)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @destroy_socket(i32 %0)
br label %35
12: ; preds = %1
%13 = call i32 @basename(i8* %7)
%14 = call i32 @cleanup_login_records(i32 %13)
%15 = call i32 (...) @fork()
switch i32 %15, label %22 [
i32 0, label %16
i32 -1, label %18
]
16: ; preds = %12
%17 = call i32 @start_interactive_shell(i32 %0, i32* %2, i32* %3)
br label %18
18: ; preds = %12, %16
%19 = call i32 @destroy_socket(i32 %0)
%20 = load i32, i32* %3
%21 = call i32 @close(i32 %20)
br label %35
22: ; preds = %12
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
%25 = call i32 (...) @fork()
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* %2
%29 = call i32 @shell_loop(i32 %0, i32 %28)
br label %30
30: ; preds = %22, %27
%31 = call i32 @close(i32 %0)
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* @ECONNABORTED
store i32 %34, i32* @errno
br label %35
35: ; preds = %30, %18, %10
%36 = phi i32 [ %11, %10 ], [ -1, %30 ], [ -1, %18 ]
ret i32 %36
}