#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 /*<<< orphan*/ member_0;
};
struct termios {
  int /*<<< orphan*/ member_0;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int fork();
int forkpty(int *, int /*<<< orphan*/ *, struct termios *, struct winsize *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int openpty(int *, int *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int tcgetattr(int, struct termios *);
pid_t _fork(int *master_fd, int *slave_fd) {
  pid_t pid;
  struct termios ts = {0};
  struct winsize win = {0};
  if (-1 == tcgetattr(1, &ts))
    fprintf(stderr, "bicon: tcgetaddr() failed.\n");
  if (-1 == ioctl(0, TIOCGWINSZ, (char *)&win))
    fprintf(stderr, "bicon: ioctl(0, TIOCGWINSZ) failed.\n");
  pid = forkpty(master_fd, NULL, &ts, &win);
  if (pid != -1) {
    if (pid == 0)
      setsid();
    return pid;
  }
  if (openpty(master_fd, slave_fd, NULL, NULL, NULL) == -1)
    return -1;
  pid = fork();
  if (pid == -1)
    return -1;
  if (pid == 0) {
    setsid();
    close(*master_fd);
    dup2(*slave_fd, 0);
    dup2(*slave_fd, 1);
    dup2(*slave_fd, 2);
    if (*slave_fd > 2)
      close(*slave_fd);
  }
  return pid;
}


x86 assembly (clang, -O0):
<_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    $0x1,%edi
lea    -0x20(%rbp),%rsi
call   10a0 <tcgetattr@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1202 <_fork+0x62>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
xor    %edi,%edi
call   1050 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    123a <_fork+0x9a>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   10b0 <forkpty@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1278 <_fork+0xd8>
cmpl   $0x0,-0x1c(%rbp)
jne    126d <_fork+0xcd>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1331 <_fork+0x191>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <openpty@plt>
cmp    $0xffffffff,%eax
jne    12a5 <_fork+0x105>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <_fork+0x191>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12c5 <_fork+0x125>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <_fork+0x191>
cmpl   $0x0,-0x1c(%rbp)
jne    132b <_fork+0x18b>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x2,(%rax)
jle    1326 <_fork+0x186>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <close@plt>
jmp    132b <_fork+0x18b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_fork>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movl   $0x0,0x8(%rsp)
movl   $0x0,(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x1,%edi
call   1090 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    11d6 <_fork+0x46>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1203 <_fork+0x73>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1070 <fprintf@plt>
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <forkpty@plt>
cmp    $0xffffffff,%eax
je     122b <_fork+0x9b>
mov    %eax,%ebp
test   %eax,%eax
jne    1295 <_fork+0x105>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <setsid@plt>
jmp    1295 <_fork+0x105>
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <openpty@plt>
cmp    $0xffffffff,%eax
je     1290 <_fork+0x100>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1295 <_fork+0x105>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    (%rbx),%edi
call   1050 <close@plt>
mov    (%r14),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    (%r14),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    (%r14),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    (%r14),%edi
cmp    $0x3,%edi
jl     1295 <_fork+0x105>
call   1050 <close@plt>
jmp    1295 <_fork+0x105>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"bicon: tcgetaddr() failed.\0A\00"
@TIOCGWINSZ = global i32 0
@.str.1 = [37 x i8] c"bicon: ioctl(0, TIOCGWINSZ) failed.\0A\00"
define i32 @_fork(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca %struct.winsize
store i32* %0, i32** %4
store i32* %1, i32** %5
%9 = bitcast %struct.termios* %7 to i8*
%10 = bitcast %struct.winsize* %8 to i8*
%11 = call i32 @tcgetattr(i32 1, %struct.termios* %7)
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %2
%17 = load i32, i32* @TIOCGWINSZ
%18 = bitcast %struct.winsize* %8 to i8*
%19 = call i32 @ioctl(i32 0, i32 %17, i8* %18)
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %16
%25 = load i32*, i32** %4
%26 = call i32 @forkpty(i32* %25, i32* null, %struct.termios* %7, %struct.winsize* %8)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 (...) @setsid()
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* %6
store i32 %35, i32* %3
br label %74
36: ; preds = %24
%37 = load i32*, i32** %4
%38 = load i32*, i32** %5
%39 = call i32 @openpty(i32* %37, i32* %38, i32* null, i32* null, i32* null)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %36
store i32 -1, i32* %3
br label %74
42: ; preds = %36
%43 = call i32 (...) @fork()
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %3
br label %74
47: ; preds = %42
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %72
50: ; preds = %47
%51 = call i32 (...) @setsid()
%52 = load i32*, i32** %4
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = load i32*, i32** %5
%56 = load i32, i32* %55
%57 = call i32 @dup2(i32 %56, i32 0)
%58 = load i32*, i32** %5
%59 = load i32, i32* %58
%60 = call i32 @dup2(i32 %59, i32 1)
%61 = load i32*, i32** %5
%62 = load i32, i32* %61
%63 = call i32 @dup2(i32 %62, i32 2)
%64 = load i32*, i32** %5
%65 = load i32, i32* %64
%66 = icmp sgt i32 %65, 2
br i1 %66, label %67, label %71
67: ; preds = %50
%68 = load i32*, i32** %5
%69 = load i32, i32* %68
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %67, %50
br label %72
72: ; preds = %71, %47
%73 = load i32, i32* %6
store i32 %73, i32* %3
br label %74
74: ; preds = %72, %46, %41, %34
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@stderr = global i32 0
@.str = [28 x i8] c"bicon: tcgetaddr() failed.\0A\00"
@TIOCGWINSZ = global i32 0
@.str.1 = [37 x i8] c"bicon: ioctl(0, TIOCGWINSZ) failed.\0A\00"
define i32 @_fork(i32* %0, i32* %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.winsize
%5 = bitcast %struct.termios* %3 to i8*
%6 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 0, i32* %6
%7 = bitcast %struct.winsize* %4 to i8*
%8 = getelementptr inbounds %struct.winsize, %struct.winsize* %4, i64 0, i32 0
store i32 0, i32* %8
%9 = call i32 @tcgetattr(i32 1, %struct.termios* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %2
%15 = load i32, i32* @TIOCGWINSZ
%16 = call i32 @ioctl(i32 0, i32 %15, i8* %7)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %18, %14
%22 = call i32 @forkpty(i32* %0, i32* null, %struct.termios* %3, %struct.winsize* %4)
switch i32 %22, label %45 [
i32 -1, label %25
i32 0, label %23
]
23: ; preds = %21
%24 = call i32 (...) @setsid()
br label %45
25: ; preds = %21
%26 = call i32 @openpty(i32* %0, i32* %1, i32* null, i32* null, i32* null)
%27 = icmp eq i32 %26, -1
br i1 %27, label %45, label %28
28: ; preds = %25
%29 = call i32 (...) @fork()
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %45
31: ; preds = %28
%32 = call i32 (...) @setsid()
%33 = load i32, i32* %0
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %1
%36 = call i32 @dup2(i32 %35, i32 0)
%37 = load i32, i32* %1
%38 = call i32 @dup2(i32 %37, i32 1)
%39 = load i32, i32* %1
%40 = call i32 @dup2(i32 %39, i32 2)
%41 = load i32, i32* %1
%42 = icmp sgt i32 %41, 2
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = call i32 @close(i32 %41)
br label %45
45: ; preds = %28, %43, %31, %25, %23, %21
%46 = phi i32 [ %22, %21 ], [ 0, %23 ], [ -1, %25 ], [ %29, %28 ], [ %29, %31 ], [ %29, %43 ]
ret i32 %46
}