#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCNOTTY;
int /*<<< orphan*/ alarm(int);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int setsid();
int setupslave(char *slavedevice, int orig_stdout) {
  int fd;
  alarm(150);
  if (setsid() == -1) {
    ;
    return -1;
  }
  fd = open("/dev/tty", O_RDWR);
  if (fd != -1) {
    ioctl(fd, TIOCNOTTY);
  }
  close(0);
  close(1);
  close(2);
  fd = open(slavedevice, O_RDWR);
  if (dup(fd) == -1) {
    return 1;
  }
  if (dup(fd) == -1) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setupslave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x96,%edi
call   1050 <alarm@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
jne    1195 <setupslave+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    1239 <setupslave+0xd9>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     11c8 <setupslave+0x68>
mov    -0x18(%rbp),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
cmp    $0xffffffff,%eax
jne    1215 <setupslave+0xb5>
movl   $0x1,-0x4(%rbp)
jmp    1239 <setupslave+0xd9>
mov    -0x18(%rbp),%edi
call   1030 <dup@plt>
cmp    $0xffffffff,%eax
jne    1232 <setupslave+0xd2>
movl   $0x1,-0x4(%rbp)
jmp    1239 <setupslave+0xd9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setupslave>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x96,%edi
call   1050 <alarm@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
cmp    $0xffffffff,%eax
je     11f3 <setupslave+0x93>
mov    0x2e5c(%rip),%r14
mov    (%r14),%esi
lea    0xe72(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11a8 <setupslave+0x48>
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1040 <ioctl@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%ebp
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <dup@plt>
cmp    $0xffffffff,%eax
je     11f8 <setupslave+0x98>
mov    %ebx,%edi
call   1030 <dup@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
jmp    11f8 <setupslave+0x98>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@TIOCNOTTY = global i32 0
define i32 @setupslave(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = call i32 @alarm(i32 150)
%8 = call i32 (...) @setsid()
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %37
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* %6
%18 = load i32, i32* @TIOCNOTTY
%19 = call i32 @ioctl(i32 %17, i32 %18)
br label %20
20: ; preds = %16, %11
%21 = call i32 @close(i32 0)
%22 = call i32 @close(i32 1)
%23 = call i32 @close(i32 2)
%24 = load i8*, i8** %4
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = call i32 @dup(i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %20
store i32 1, i32* %3
br label %37
31: ; preds = %20
%32 = load i32, i32* %6
%33 = call i32 @dup(i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 1, i32* %3
br label %37
36: ; preds = %31
store i32 0, i32* %3
br label %37
37: ; preds = %36, %35, %30, %10
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@TIOCNOTTY = global i32 0
define i32 @setupslave(i8* nocapture %0, i32 %1) {
%3 = tail call i32 @alarm(i32 150)
%4 = tail call i32 (...) @setsid()
%5 = icmp eq i32 %4, -1
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = load i32, i32* @TIOCNOTTY
%12 = tail call i32 @ioctl(i32 %8, i32 %11)
br label %13
13: ; preds = %10, %6
%14 = tail call i32 @close(i32 0)
%15 = tail call i32 @close(i32 1)
%16 = tail call i32 @close(i32 2)
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i8* %0, i32 %17)
%19 = tail call i32 @dup(i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %25, label %21
21: ; preds = %13
%22 = tail call i32 @dup(i32 %18)
%23 = icmp eq i32 %22, -1
%24 = zext i1 %23 to i32
br label %25
25: ; preds = %21, %13, %2
%26 = phi i32 [ -1, %2 ], [ 1, %13 ], [ %24, %21 ]
ret i32 %26
}