forkpty(amaster, name, termp, winp) int *amaster;
char *name;
struct termios *termp;
struct winsize *winp;
{
  int master, slave, pid;
  if (openpty(&master, &slave, name, termp, winp) == -1)
    return (-1);
  switch (pid = fork()) {
  case -1:
    return (-1);
  case 0:
    (void)close(master);
    login_tty(slave);
    return (0);
  }
  *amaster = master;
  (void)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   1050 <openpty@plt>
cmp    $0xffffffff,%eax
jne    1188 <forkpty+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <forkpty+0xba>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,-0x34(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x38(%rbp)
sub    $0xffffffff,%eax
je     11b5 <forkpty+0x75>
jmp    11a5 <forkpty+0x65>
mov    -0x38(%rbp),%eax
test   %eax,%eax
je     11c1 <forkpty+0x81>
jmp    11e1 <forkpty+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <forkpty+0xba>
mov    -0x2c(%rbp),%edi
mov    $0x0,%al
call   1030 <close@plt>
mov    -0x30(%rbp),%edi
mov    $0x0,%al
call   1040 <login_tty@plt>
movl   $0x0,-0x4(%rbp)
jmp    11fa <forkpty+0xba>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%edi
mov    $0x0,%al
call   1030 <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
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <openpty@plt>
cmp    $0xffffffff,%eax
je     118f <forkpty+0x4f>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11a7 <forkpty+0x67>
test   %ebx,%ebx
jne    1196 <forkpty+0x56>
mov    0x4(%rsp),%edi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <close@plt>
mov    (%rsp),%edi
xor    %eax,%eax
call   1040 <login_tty@plt>
jmp    11a7 <forkpty+0x67>
mov    $0xffffffff,%ebx
jmp    11a7 <forkpty+0x67>
mov    0x4(%rsp),%eax
mov    %eax,(%r14)
mov    (%rsp),%edi
xor    %eax,%eax
call   1030 <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 %33
19: ; preds = %4
%20 = call i32 (...) @fork()
store i32 %20, i32* %12
switch i32 %20, label %27 [
i32 -1, label %21
i32 0, label %22
]
21: ; preds = %19
store i32 -1, i32* %5
br label %33
22: ; preds = %19
%23 = load i32, i32* %10
%24 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %23)
%25 = load i32, i32* %11
%26 = call i32 (i32, ...) bitcast (i32 (...)* @login_tty to i32 (i32, ...)*)(i32 %25)
store i32 0, i32* %5
br label %33
27: ; preds = %19
%28 = load i32, i32* %10
%29 = load i32*, i32** %6
store i32 %28, i32* %29
%30 = load i32, i32* %11
%31 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %30)
%32 = load i32, i32* %12
store i32 %32, i32* %5
br label %33
33: ; preds = %27, %22, %21, %18
%34 = load i32, i32* %5
ret i32 %34
}

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 %22, label %11
11: ; preds = %4
%12 = call i32 (...) @fork()
switch i32 %12, label %18 [
i32 -1, label %22
i32 0, label %13
]
13: ; preds = %11
%14 = load i32, i32* %5
%15 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %14)
%16 = load i32, i32* %6
%17 = call i32 (i32, ...) bitcast (i32 (...)* @login_tty to i32 (i32, ...)*)(i32 %16)
br label %22
18: ; preds = %11
%19 = load i32, i32* %5
store i32 %19, i32* %0
%20 = load i32, i32* %6
%21 = call i32 (i32, ...) bitcast (i32 (...)* @close to i32 (i32, ...)*)(i32 %20)
br label %22
22: ; preds = %11, %4, %18, %13
%23 = phi i32 [ %12, %18 ], [ 0, %13 ], [ -1, %4 ], [ %12, %11 ]
ret i32 %23
}