#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 member_0;
  int member_1;
  int /*<<< orphan*/ member_3;
  int /*<<< orphan*/ member_2;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ openpty(int *, int *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/ *, struct winsize *);
int /*<<< orphan*/ perror(char *);
int start_shell(int);
pid_t fork_shell(int *master_out) {
  int master, slave;
  struct winsize sz = {20, 80, 0, 0};
  pid_t pid;
  openpty(&master, &slave, NULL, NULL, &sz);
  pid = fork();
  if (pid == -1) {
    perror("fork failure");
    exit(-1);
  } else if (pid == 0) {
    close(master);
    int rv;
    if ((rv = start_shell(slave)) != 0) {
      perror("Error starting shell");
      close(master);
      exit(-1);
    }
    return pid;
  } else {
    close(slave);
    if (master_out != NULL) {
      *master_out = master;
    }
    return pid;
  }
}


x86 assembly (clang, -O0):
<fork_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xe8d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1040 <openpty@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11c4 <fork_shell+0x64>
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1213 <fork_shell+0xb3>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%edi
call   1070 <start_shell@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     1208 <fork_shell+0xa8>
lea    0xe2c(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1235 <fork_shell+0xd5>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
cmpq   $0x0,-0x10(%rbp)
je     122f <fork_shell+0xcf>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_shell>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movups 0xe90(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <openpty@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11b5 <fork_shell+0x55>
cmp    $0xffffffff,%ebp
je     11d4 <fork_shell+0x74>
mov    0xc(%rsp),%edi
call   1030 <close@plt>
test   %rbx,%rbx
je     11cb <fork_shell+0x6b>
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
jmp    11cb <fork_shell+0x6b>
mov    0x8(%rsp),%edi
call   1030 <close@plt>
mov    0xc(%rsp),%edi
call   1070 <start_shell@plt>
test   %eax,%eax
jne    11ea <fork_shell+0x8a>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe35(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
lea    0xe2c(%rip),%rdi
call   1050 <perror@plt>
mov    0x8(%rsp),%edi
call   1030 <close@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.winsize = type { i32, i32, i32, i32 }
@__const.fork_shell.sz = %struct.winsize { i32 20, i32 80, i32 0, i32 0 }
@.str = [13 x i8] c"fork failure\00"
@.str.1 = [21 x i8] c"Error starting shell\00"
define i32 @fork_shell(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.winsize
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
%9 = bitcast %struct.winsize* %6 to i8*
%10 = call i32 @openpty(i32* %4, i32* %5, i32* null, i32* null, %struct.winsize* %6)
%11 = call i32 (...) @fork()
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %1
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %17
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
%23 = load i32, i32* %5
%24 = call i32 @start_shell(i32 %23)
store i32 %24, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %20
%32 = load i32, i32* %7
store i32 %32, i32* %2
br label %43
33: ; preds = %17
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
%36 = load i32*, i32** %3
%37 = icmp ne i32* %36, null
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* %4
%40 = load i32*, i32** %3
store i32 %39, i32* %40
br label %41
41: ; preds = %38, %33
%42 = load i32, i32* %7
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %31
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32, i32, i32 }
@__const.fork_shell.sz = %struct.winsize { i32 20, i32 80, i32 0, i32 0 }
@.str = [13 x i8] c"fork failure\00"
@.str.1 = [21 x i8] c"Error starting shell\00"
define i32 @fork_shell(i32* writeonly %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.winsize
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.winsize* %4 to i8*
%8 = call i32 @openpty(i32* %2, i32* %3, i32* null, i32* null, %struct.winsize* %4)
%9 = call i32 (...) @fork()
switch i32 %9, label %24 [
i32 -1, label %10
i32 0, label %13
]
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %1
%14 = load i32, i32* %2
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* %3
%17 = call i32 @start_shell(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %30, label %19
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %1
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
%27 = icmp eq i32* %0, null
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = load i32, i32* %2
store i32 %29, i32* %0
br label %30
30: ; preds = %24, %28, %13
ret i32 %9
}