#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 const *,
            struct winsize const *);
int forkpty(int *amaster, char *name, const struct termios *termp,
            const struct winsize *winp) {
  int master, slave, pid;
  if (openpty(&master, &slave, name, termp, winp) == -1)
    return -1;
  switch (pid = fork()) {
  case -1:
    close(master);
    close(slave);
    return -1;
  case 0:
    close(master);
    if (login_tty(slave))
      _exit(1);
    return 0;
  default:
    *amaster = master;
    close(slave);
    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    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    -0x2c(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <openpty@plt>
cmp    $0xffffffff,%eax
jne    1196 <forkpty+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <forkpty+0xd5>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x34(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     11c3 <forkpty+0x73>
jmp    11b3 <forkpty+0x63>
mov    -0x38(%rbp),%eax
test   %eax,%eax
je     11df <forkpty+0x8f>
jmp    120e <forkpty+0xbe>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <forkpty+0xd5>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
call   1050 <login_tty@plt>
cmp    $0x0,%eax
je     1202 <forkpty+0xb2>
mov    $0x1,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x4(%rbp)
jmp    1225 <forkpty+0xd5>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkpty>:
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r8
mov    %rdx,%rcx
mov    %rsi,%rdx
mov    %rdi,%r14
mov    %rsp,%rdi
lea    0x4(%rsp),%rsi
call   1060 <openpty@plt>
cmp    $0xffffffff,%eax
je     1193 <forkpty+0x43>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     119a <forkpty+0x4a>
cmp    $0xffffffff,%eax
jne    11b9 <forkpty+0x69>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11c1 <forkpty+0x71>
mov    $0xffffffff,%ebx
jmp    11ca <forkpty+0x7a>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <login_tty@plt>
test   %eax,%eax
je     11ca <forkpty+0x7a>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    (%rsp),%ecx
mov    %ecx,(%r14)
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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