#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 EPERM;
int O_CLOEXEC;
int O_NOCTTY;
int O_RDWR;
scalar_t__ STDERR_FILENO;
scalar_t__ STDIN_FILENO;
scalar_t__ STDOUT_FILENO;
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, scalar_t__);
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
char *ptsname(int);
scalar_t__ setsid();
int setup_pty_child(int master_fd) {
  pid_t pid;
  int slave_fd;
  char *slave_name = ptsname(master_fd);
  if (slave_name == NULL)
    return -errno;
  slave_fd = open(slave_name, O_RDWR | O_NOCTTY | O_CLOEXEC);
  if (slave_fd < -1)
    return -errno;
  pid = setsid();
  if (pid < 0 && errno != EPERM)
    return -errno;
  if (ioctl(slave_fd, TIOCSCTTY, 0) < 0)
    return -errno;
  if (dup2(slave_fd, STDIN_FILENO) != STDIN_FILENO ||
      dup2(slave_fd, STDOUT_FILENO) != STDOUT_FILENO ||
      dup2(slave_fd, STDERR_FILENO) != STDERR_FILENO)
    return -errno;
  if (slave_fd > 2)
    close(slave_fd);
  return 0;
}


x86 assembly (clang, -O0):
<setup_pty_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1195 <setup_pty_child+0x35>
xor    %eax,%eax
mov    0x2e3d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12dc <setup_pty_child+0x17c>
mov    -0x20(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jge    11d9 <setup_pty_child+0x79>
xor    %eax,%eax
mov    0x2df9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12dc <setup_pty_child+0x17c>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    121a <setup_pty_child+0xba>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%eax
je     121a <setup_pty_child+0xba>
xor    %eax,%eax
mov    0x2db8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12dc <setup_pty_child+0x17c>
mov    -0x14(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    124a <setup_pty_child+0xea>
xor    %eax,%eax
mov    0x2d88(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12dc <setup_pty_child+0x17c>
mov    -0x14(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
jne    12b0 <setup_pty_child+0x150>
mov    -0x14(%rbp),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%rax
jne    12b0 <setup_pty_child+0x150>
mov    -0x14(%rbp),%edi
mov    0x2d28(%rip),%rax
mov    (%rax),%rsi
call   1040 <dup2@plt>
mov    0x2d19(%rip),%rcx
cmp    (%rcx),%rax
je     12c3 <setup_pty_child+0x163>
xor    %eax,%eax
mov    0x2d0f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12dc <setup_pty_child+0x17c>
cmpl   $0x2,-0x14(%rbp)
jle    12d5 <setup_pty_child+0x175>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_pty_child>:
push   %rbp
push   %rbx
push   %rax
call   1030 <ptsname@plt>
test   %rax,%rax
je     1234 <setup_pty_child+0xd4>
mov    0x2e68(%rip),%rcx
mov    0x2e51(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e16(%rip),%rcx
or     (%rcx),%esi
mov    %rax,%rdi
call   1080 <open@plt>
cmp    $0xfffffffe,%eax
jle    1234 <setup_pty_child+0xd4>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <setsid@plt>
test   %rax,%rax
jns    11c3 <setup_pty_child+0x63>
mov    0x2e16(%rip),%rax
mov    (%rax),%ebp
mov    0x2ddd(%rip),%rax
cmp    (%rax),%ebp
jne    1251 <setup_pty_child+0xf1>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     1248 <setup_pty_child+0xe8>
mov    0x2dcd(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    0x0(%rbp),%rax
jne    1234 <setup_pty_child+0xd4>
mov    0x2df5(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    0x0(%rbp),%rax
jne    1234 <setup_pty_child+0xd4>
mov    0x2dad(%rip),%rbp
mov    0x0(%rbp),%rsi
mov    %ebx,%edi
call   1040 <dup2@plt>
cmp    0x0(%rbp),%rax
jne    1234 <setup_pty_child+0xd4>
xor    %ebp,%ebp
cmp    $0x3,%ebx
jl     123f <setup_pty_child+0xdf>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    123f <setup_pty_child+0xdf>
mov    0x2d8d(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d79(%rip),%rax
jmp    123d <setup_pty_child+0xdd>
neg    %ebp
jmp    123f <setup_pty_child+0xdf>

LLVM IR (-O0):
@errno = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_CLOEXEC = global i32 0
@EPERM = global i32 0
@TIOCSCTTY = global i32 0
@STDIN_FILENO = global i64 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
define i32 @setup_pty_child(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i8* @ptsname(i32 %7)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %74
14: ; preds = %1
%15 = load i8*, i8** %6
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_NOCTTY
%18 = or i32 %16, %17
%19 = load i32, i32* @O_CLOEXEC
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %15, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
store i32 %26, i32* %2
br label %74
27: ; preds = %14
%28 = call i64 (...) @setsid()
store i64 %28, i64* %4
%29 = load i64, i64* %4
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %27
%32 = load i32, i32* @errno
%33 = load i32, i32* @EPERM
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %2
br label %74
38: ; preds = %31, %27
%39 = load i32, i32* %5
%40 = load i32, i32* @TIOCSCTTY
%41 = call i64 @ioctl(i32 %39, i32 %40, i32 0)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* @errno
%45 = sub nsw i32 0, %44
store i32 %45, i32* %2
br label %74
46: ; preds = %38
%47 = load i32, i32* %5
%48 = load i64, i64* @STDIN_FILENO
%49 = call i64 @dup2(i32 %47, i64 %48)
%50 = load i64, i64* @STDIN_FILENO
%51 = icmp ne i64 %49, %50
br i1 %51, label %64, label %52
52: ; preds = %46
%53 = load i32, i32* %5
%54 = load i64, i64* @STDOUT_FILENO
%55 = call i64 @dup2(i32 %53, i64 %54)
%56 = load i64, i64* @STDOUT_FILENO
%57 = icmp ne i64 %55, %56
br i1 %57, label %64, label %58
58: ; preds = %52
%59 = load i32, i32* %5
%60 = load i64, i64* @STDERR_FILENO
%61 = call i64 @dup2(i32 %59, i64 %60)
%62 = load i64, i64* @STDERR_FILENO
%63 = icmp ne i64 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %58, %52, %46
%65 = load i32, i32* @errno
%66 = sub nsw i32 0, %65
store i32 %66, i32* %2
br label %74
67: ; preds = %58
%68 = load i32, i32* %5
%69 = icmp sgt i32 %68, 2
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i32, i32* %5
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %67
store i32 0, i32* %2
br label %74
74: ; preds = %73, %64, %43, %35, %24, %11
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
@errno = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_CLOEXEC = global i32 0
@EPERM = global i32 0
@TIOCSCTTY = global i32 0
@STDIN_FILENO = global i64 0
@STDOUT_FILENO = global i64 0
@STDERR_FILENO = global i64 0
define i32 @setup_pty_child(i32 %0) {
%2 = tail call i8* @ptsname(i32 %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @errno
%6 = sub nsw i32 0, %5
br label %56
7: ; preds = %1
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %9, %8
%11 = load i32, i32* @O_CLOEXEC
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* %2, i32 %12)
%14 = icmp slt i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
br label %56
18: ; preds = %7
%19 = tail call i64 (...) @setsid()
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = load i32, i32* @EPERM
%24 = icmp eq i32 %22, %23
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = sub nsw i32 0, %22
br label %56
27: ; preds = %21, %18
%28 = load i32, i32* @TIOCSCTTY
%29 = tail call i64 @ioctl(i32 %13, i32 %28, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @errno
%33 = sub nsw i32 0, %32
br label %56
34: ; preds = %27
%35 = load i64, i64* @STDIN_FILENO
%36 = tail call i64 @dup2(i32 %13, i64 %35)
%37 = load i64, i64* @STDIN_FILENO
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %49
39: ; preds = %34
%40 = load i64, i64* @STDOUT_FILENO
%41 = tail call i64 @dup2(i32 %13, i64 %40)
%42 = load i64, i64* @STDOUT_FILENO
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i64, i64* @STDERR_FILENO
%46 = tail call i64 @dup2(i32 %13, i64 %45)
%47 = load i64, i64* @STDERR_FILENO
%48 = icmp eq i64 %46, %47
br i1 %48, label %52, label %49
49: ; preds = %44, %39, %34
%50 = load i32, i32* @errno
%51 = sub nsw i32 0, %50
br label %56
52: ; preds = %44
%53 = icmp sgt i32 %13, 2
br i1 %53, label %54, label %56
54: ; preds = %52
%55 = tail call i32 @close(i32 %13)
br label %56
56: ; preds = %52, %54, %49, %31, %25, %15, %4
%57 = phi i32 [ %6, %4 ], [ %17, %15 ], [ %26, %25 ], [ %33, %31 ], [ %51, %49 ], [ 0, %54 ], [ 0, %52 ]
ret i32 %57
}