int forkpty(amaster, name, termp, winp)
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
mov    $0x0,%al
call   1060 <openpty@plt>
cmp    $0xffffffff,%eax
jne    1198 <forkpty+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <forkpty+0xe1>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x34(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     11c5 <forkpty+0x75>
jmp    11b5 <forkpty+0x65>
mov    -0x38(%rbp),%eax
test   %eax,%eax
je     11e5 <forkpty+0x95>
jmp    1218 <forkpty+0xc8>
mov    -0x2c(%rbp),%edi
mov    $0x0,%al
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
mov    $0x0,%al
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <forkpty+0xe1>
mov    -0x2c(%rbp),%edi
mov    $0x0,%al
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
mov    $0x0,%al
call   1050 <login_tty@plt>
cmp    $0x0,%eax
je     120c <forkpty+0xbc>
mov    $0x1,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x4(%rbp)
jmp    1231 <forkpty+0xe1>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%edi
mov    $0x0,%al
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
xor    %eax,%eax
call   1060 <openpty@plt>
cmp    $0xffffffff,%eax
je     1197 <forkpty+0x47>
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
je     119e <forkpty+0x4e>
cmp    $0xffffffff,%eax
jne    11c1 <forkpty+0x71>
mov    (%rsp),%edi
xor    %eax,%eax
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11c9 <forkpty+0x79>
mov    $0xffffffff,%ebx
jmp    11d4 <forkpty+0x84>
mov    (%rsp),%edi
xor    %eax,%eax
call   1040 <close@plt>
mov    0x4(%rsp),%edi
xor    %eax,%eax
call   1050 <login_tty@plt>
test   %eax,%eax
je     11d4 <forkpty+0x84>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    (%rsp),%ecx
mov    %ecx,(%r14)
mov    %eax,%ebx
mov    0x4(%rsp),%edi
xor    %eax,%eax
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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