#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 winsize {
  int dummy;
};
struct termios {
  int dummy;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ fork();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, char *);
scalar_t__ openpty(int *, int *, char *, struct termios const *,
                   struct winsize const *);
int /*<<< orphan*/ setsid();
int forkpty(int *m, char *name, const struct termios *tio,
            const struct winsize *ws) {
  int s;
  pid_t pid;
  if (openpty(m, &s, name, tio, ws) < 0)
    return -1;
  pid = fork();
  if (!pid) {
    close(*m);
    setsid();
    ioctl(s, TIOCSCTTY, (char *)0);
    dup2(s, 0);
    dup2(s, 1);
    dup2(s, 2);
    if (s > 2)
      close(s);
    return 0;
  }
  close(s);
  if (pid < 0)
    close(*m);
  return pid;
}


x86 assembly (clang, -O0):
<forkpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    -0x2c(%rbp),%rsi
call   1070 <openpty@plt>
cmp    $0x0,%rax
jge    11a7 <forkpty+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <forkpty+0xeb>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1226 <forkpty+0xc6>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    -0x2c(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
cmpl   $0x2,-0x2c(%rbp)
jle    121a <forkpty+0xba>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    124b <forkpty+0xeb>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
cmpq   $0x0,-0x38(%rbp)
jge    1244 <forkpty+0xe4>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkpty>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r8
mov    %rdx,%rcx
mov    %rsi,%rdx
mov    %rdi,%r15
lea    0xc(%rsp),%rsi
call   1070 <openpty@plt>
test   %rax,%rax
js     11b1 <forkpty+0x51>
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11b9 <forkpty+0x59>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
call   1050 <close@plt>
test   %rbx,%rbx
jns    11ac <forkpty+0x4c>
mov    (%r15),%edi
call   1050 <close@plt>
mov    %ebx,%r14d
jmp    1211 <forkpty+0xb1>
mov    $0xffffffff,%r14d
jmp    1211 <forkpty+0xb1>
mov    (%r15),%edi
call   1050 <close@plt>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0xc(%rsp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <ioctl@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
cmp    $0x3,%edi
jl     1211 <forkpty+0xb1>
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@TIOCSCTTY = global i32 0
define i32 @forkpty(i32* %0, i8* %1, %struct.termios* %2, %struct.winsize* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca %struct.termios*
%9 = alloca %struct.winsize*
%10 = alloca i32
%11 = alloca i64
store i32* %0, i32** %6
store i8* %1, i8** %7
store %struct.termios* %2, %struct.termios** %8
store %struct.winsize* %3, %struct.winsize** %9
%12 = load i32*, i32** %6
%13 = load i8*, i8** %7
%14 = load %struct.termios*, %struct.termios** %8
%15 = load %struct.winsize*, %struct.winsize** %9
%16 = call i64 @openpty(i32* %12, i32* %10, i8* %13, %struct.termios* %14, %struct.winsize* %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %55
19: ; preds = %4
%20 = call i64 (...) @fork()
store i64 %20, i64* %11
%21 = load i64, i64* %11
%22 = icmp ne i64 %21, 0
br i1 %22, label %43, label %23
23: ; preds = %19
%24 = load i32*, i32** %6
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = call i32 (...) @setsid()
%28 = load i32, i32* %10
%29 = load i32, i32* @TIOCSCTTY
%30 = call i32 @ioctl(i32 %28, i32 %29, i8* null)
%31 = load i32, i32* %10
%32 = call i32 @dup2(i32 %31, i32 0)
%33 = load i32, i32* %10
%34 = call i32 @dup2(i32 %33, i32 1)
%35 = load i32, i32* %10
%36 = call i32 @dup2(i32 %35, i32 2)
%37 = load i32, i32* %10
%38 = icmp sgt i32 %37, 2
br i1 %38, label %39, label %42
39: ; preds = %23
%40 = load i32, i32* %10
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39, %23
store i32 0, i32* %5
br label %55
43: ; preds = %19
%44 = load i32, i32* %10
%45 = call i32 @close(i32 %44)
%46 = load i64, i64* %11
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i32*, i32** %6
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %48, %43
%53 = load i64, i64* %11
%54 = trunc i64 %53 to i32
store i32 %54, i32* %5
br label %55
55: ; preds = %52, %42, %18
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@TIOCSCTTY = global i32 0
define i32 @forkpty(i32* %0, i8* %1, %struct.termios* %2, %struct.winsize* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i64 @openpty(i32* %0, i32* %5, i8* %1, %struct.termios* %2, %struct.winsize* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %38, label %9
9: ; preds = %4
%10 = call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %9
%13 = load i32, i32* %0
%14 = call i32 @close(i32 %13)
%15 = call i32 (...) @setsid()
%16 = load i32, i32* %5
%17 = load i32, i32* @TIOCSCTTY
%18 = call i32 @ioctl(i32 %16, i32 %17, i8* null)
%19 = load i32, i32* %5
%20 = call i32 @dup2(i32 %19, i32 0)
%21 = load i32, i32* %5
%22 = call i32 @dup2(i32 %21, i32 1)
%23 = load i32, i32* %5
%24 = call i32 @dup2(i32 %23, i32 2)
%25 = load i32, i32* %5
%26 = icmp sgt i32 %25, 2
br i1 %26, label %27, label %38
27: ; preds = %12
%28 = call i32 @close(i32 %25)
br label %38
29: ; preds = %9
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
%32 = icmp slt i64 %10, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* %0
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %29
%37 = trunc i64 %10 to i32
br label %38
38: ; preds = %12, %27, %4, %36
%39 = phi i32 [ %37, %36 ], [ -1, %4 ], [ 0, %27 ], [ 0, %12 ]
ret i32 %39
}