#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;
};

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ login_tty(int);
int openpty(int *, int *, char *, struct termios *, struct winsize *);
int restraint_forkpty(int *amaster, char *name, const struct termios *termp,
                      const struct winsize *winp, void (*setup)(void)) {
  int master, slave, pid;
  if (openpty(&master, &slave, name, (struct termios *)termp,
              (struct winsize *)winp) == -1)
    return -1;
  switch (pid = fork()) {
  case -1:
    close(master);
    close(slave);
    return -1;
  case 0:
    close(master);
    if (setup != NULL) {
      setup();
    }
    if (login_tty(slave))
      _exit(1);
    return 0;
  default:
    *amaster = master;
    close(slave);
    return pid;
  }
}


x86 assembly (clang, -O0):
<restraint_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    %r8,-0x30(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    -0x34(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1060 <openpty@plt>
cmp    $0xffffffff,%eax
jne    119a <restraint_forkpty+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <restraint_forkpty+0xe7>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x3c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x40(%rbp)
sub    $0xffffffff,%eax
je     11c7 <restraint_forkpty+0x77>
jmp    11b7 <restraint_forkpty+0x67>
mov    -0x40(%rbp),%eax
test   %eax,%eax
je     11e3 <restraint_forkpty+0x93>
jmp    1220 <restraint_forkpty+0xd0>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <restraint_forkpty+0xe7>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x30(%rbp)
je     11f9 <restraint_forkpty+0xa9>
call   *-0x30(%rbp)
mov    -0x38(%rbp),%edi
call   1050 <login_tty@plt>
cmp    $0x0,%eax
je     1214 <restraint_forkpty+0xc4>
mov    $0x1,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x4(%rbp)
jmp    1237 <restraint_forkpty+0xe7>
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<restraint_forkpty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r14
mov    %rcx,%r8
mov    %rdx,%rcx
mov    %rsi,%rdx
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1060 <openpty@plt>
cmp    $0xffffffff,%eax
je     119d <restraint_forkpty+0x4d>
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     11a4 <restraint_forkpty+0x54>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ce <restraint_forkpty+0x7e>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
jmp    11d4 <restraint_forkpty+0x84>
mov    $0xffffffff,%ebp
jmp    11dd <restraint_forkpty+0x8d>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
test   %r14,%r14
je     11b5 <restraint_forkpty+0x65>
call   *%r14
mov    0xc(%rsp),%edi
call   1050 <login_tty@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     11dd <restraint_forkpty+0x8d>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
define i32 @restraint_forkpty(i32* %0, i8* %1, %struct.termios* %2, %struct.winsize* %3, void ()* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca %struct.termios*
%10 = alloca %struct.winsize*
%11 = alloca void ()*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %7
store i8* %1, i8** %8
store %struct.termios* %2, %struct.termios** %9
store %struct.winsize* %3, %struct.winsize** %10
store void ()* %4, void ()** %11
%15 = load i8*, i8** %8
%16 = load %struct.termios*, %struct.termios** %9
%17 = load %struct.winsize*, %struct.winsize** %10
%18 = call i32 @openpty(i32* %12, i32* %13, i8* %15, %struct.termios* %16, %struct.winsize* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %5
store i32 -1, i32* %6
br label %48
21: ; preds = %5
%22 = call i32 (...) @fork()
store i32 %22, i32* %14
switch i32 %22, label %42 [
i32 -1, label %23
i32 0, label %28
]
23: ; preds = %21
%24 = load i32, i32* %12
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %13
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %6
br label %48
28: ; preds = %21
%29 = load i32, i32* %12
%30 = call i32 @close(i32 %29)
%31 = load void ()*, void ()** %11
%32 = icmp ne void ()* %31, null
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = load void ()*, void ()** %11
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %13
%37 = call i32 @login_tty(i32 %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @_exit(i32 1)
unreachable
41: ; preds = %35
store i32 0, i32* %6
br label %48
42: ; preds = %21
%43 = load i32, i32* %12
%44 = load i32*, i32** %7
store i32 %43, i32* %44
%45 = load i32, i32* %13
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %14
store i32 %47, i32* %6
br label %48
48: ; preds = %42, %41, %23, %20
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
define i32 @restraint_forkpty(i32* nocapture writeonly %0, i8* %1, %struct.termios* %2, %struct.winsize* %3, void ()* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %6 to i8*
%9 = bitcast i32* %7 to i8*
%10 = call i32 @openpty(i32* %6, i32* %7, i8* %1, %struct.termios* %2, %struct.winsize* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %34, label %12
12: ; preds = %5
%13 = call i32 (...) @fork()
switch i32 %13, label %28 [
i32 -1, label %14
i32 0, label %17
]
14: ; preds = %12
%15 = load i32, i32* %6
%16 = call i32 @close(i32 %15)
br label %30
17: ; preds = %12
%18 = load i32, i32* %6
%19 = call i32 @close(i32 %18)
%20 = icmp eq void ()* %4, null
br i1 %20, label %22, label %21
21: ; preds = %17
br label %22
22: ; preds = %21, %17
%23 = load i32, i32* %7
%24 = call i32 @login_tty(i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %22
%27 = call i32 @_exit(i32 1)
unreachable
28: ; preds = %12
%29 = load i32, i32* %6
store i32 %29, i32* %0
br label %30
30: ; preds = %14, %28
%31 = phi i32 [ -1, %14 ], [ %13, %28 ]
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %30, %22, %5
%35 = phi i32 [ -1, %5 ], [ 0, %22 ], [ %31, %30 ]
ret i32 %35
}