#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 termios {
  scalar_t__ c_iflag;
  scalar_t__ c_oflag;
  scalar_t__ c_cflag;
  scalar_t__ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ getpgid(scalar_t__);
scalar_t__ getpid();
scalar_t__ getsid(scalar_t__);
int open(char *, int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
scalar_t__ tcgetsid(int);
int testslave(char *slavedevice, struct termios *orig_termios_p,
              int orig_stdout) {
  struct termios curr_termios;
  int fd = open(slavedevice, O_RDWR);
  if (fd == -1) {
    exit(1);
  }
  if (getpid() != getsid(getpid()) || getpid() != getpgid(getpid())) {
    exit(1);
  }
  if (getpid() != tcgetsid(fd)) {
    exit(1);
  }
  if (tcgetattr(1, &curr_termios) == -1) {
    exit(1);
  }
  if (curr_termios.c_iflag != orig_termios_p->c_iflag ||
      curr_termios.c_oflag != orig_termios_p->c_oflag ||
      curr_termios.c_cflag != orig_termios_p->c_cflag ||
      curr_termios.c_lflag != orig_termios_p->c_lflag) {
    exit(1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<testslave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11bc <testslave+0x3c>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rdi
call   1050 <getsid@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
jne    1210 <testslave+0x90>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rdi
call   1090 <getpgid@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
cmp    %rcx,%rax
je     121a <testslave+0x9a>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x58(%rbp)
mov    -0x3c(%rbp),%edi
call   1030 <tcgetsid@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
cmp    %rcx,%rax
je     1247 <testslave+0xc7>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x1,%edi
lea    -0x38(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    1268 <testslave+0xe8>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
jne    12af <testslave+0x12f>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
jne    12af <testslave+0x12f>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x10(%rcx),%rax
jne    12af <testslave+0x12f>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x18(%rcx),%rax
je     12b9 <testslave+0x139>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testslave>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124b <testslave+0xcb>
mov    %eax,%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rdi
call   1050 <getsid@plt>
cmp    %rax,%rbp
jne    124b <testslave+0xcb>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rdi
call   1090 <getpgid@plt>
cmp    %rax,%rbp
jne    124b <testslave+0xcb>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rax,%rbp
mov    %r14d,%edi
call   1030 <tcgetsid@plt>
cmp    %rax,%rbp
jne    124b <testslave+0xcb>
mov    %rsp,%rsi
mov    $0x1,%edi
call   1070 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     124b <testslave+0xcb>
mov    (%rsp),%rax
cmp    (%rbx),%rax
jne    124b <testslave+0xcb>
mov    0x8(%rsp),%rax
cmp    0x8(%rbx),%rax
jne    124b <testslave+0xcb>
mov    0x10(%rsp),%rax
cmp    0x10(%rbx),%rax
jne    124b <testslave+0xcb>
mov    0x18(%rsp),%rax
cmp    0x18(%rbx),%rax
jne    124b <testslave+0xcb>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i64, i64, i64, i64 }
@O_RDWR = global i32 0
define i32 @testslave(i8* %0, %struct.termios* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca %struct.termios*
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca i32
store i8* %0, i8** %4
store %struct.termios* %1, %struct.termios** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = call i64 (...) @getpid()
%18 = call i64 (...) @getpid()
%19 = call i64 @getsid(i64 %18)
%20 = icmp ne i64 %17, %19
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = call i64 (...) @getpid()
%23 = call i64 (...) @getpid()
%24 = call i64 @getpgid(i64 %23)
%25 = icmp ne i64 %22, %24
br i1 %25, label %26, label %28
26: ; preds = %21, %16
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %21
%29 = call i64 (...) @getpid()
%30 = load i32, i32* %8
%31 = call i64 @tcgetsid(i32 %30)
%32 = icmp ne i64 %29, %31
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
%36 = call i32 @tcgetattr(i32 1, %struct.termios* %7)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %35
%41 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%42 = load i64, i64* %41
%43 = load %struct.termios*, %struct.termios** %5
%44 = getelementptr inbounds %struct.termios, %struct.termios* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = icmp ne i64 %42, %45
br i1 %46, label %68, label %47
47: ; preds = %40
%48 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
%49 = load i64, i64* %48
%50 = load %struct.termios*, %struct.termios** %5
%51 = getelementptr inbounds %struct.termios, %struct.termios* %50, i32 0, i32 1
%52 = load i64, i64* %51
%53 = icmp ne i64 %49, %52
br i1 %53, label %68, label %54
54: ; preds = %47
%55 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%56 = load i64, i64* %55
%57 = load %struct.termios*, %struct.termios** %5
%58 = getelementptr inbounds %struct.termios, %struct.termios* %57, i32 0, i32 2
%59 = load i64, i64* %58
%60 = icmp ne i64 %56, %59
br i1 %60, label %68, label %61
61: ; preds = %54
%62 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
%63 = load i64, i64* %62
%64 = load %struct.termios*, %struct.termios** %5
%65 = getelementptr inbounds %struct.termios, %struct.termios* %64, i32 0, i32 3
%66 = load i64, i64* %65
%67 = icmp ne i64 %63, %66
br i1 %67, label %68, label %70
68: ; preds = %61, %54, %47, %40
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %61
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i64, i64, i64, i64 }
@O_RDWR = global i32 0
define i32 @testslave(i8* nocapture %0, %struct.termios* nocapture %1, i32 %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = tail call i64 (...) @getpid()
%13 = tail call i64 (...) @getpid()
%14 = tail call i64 @getsid(i64 %13)
%15 = icmp eq i64 %12, %14
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = tail call i64 (...) @getpid()
%18 = tail call i64 (...) @getpid()
%19 = tail call i64 @getpgid(i64 %18)
%20 = icmp eq i64 %17, %19
br i1 %20, label %23, label %21
21: ; preds = %16, %11
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = tail call i64 (...) @getpid()
%25 = tail call i64 @tcgetsid(i32 %7)
%26 = icmp eq i64 %24, %25
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = call i32 @tcgetattr(i32 1, %struct.termios* %4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %29
%35 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%38 = load i64, i64* %37
%39 = icmp eq i64 %36, %38
br i1 %39, label %40, label %58
40: ; preds = %34
%41 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%42 = load i64, i64* %41
%43 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%44 = load i64, i64* %43
%45 = icmp eq i64 %42, %44
br i1 %45, label %46, label %58
46: ; preds = %40
%47 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 2
%48 = load i64, i64* %47
%49 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 2
%50 = load i64, i64* %49
%51 = icmp eq i64 %48, %50
br i1 %51, label %52, label %58
52: ; preds = %46
%53 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 3
%54 = load i64, i64* %53
%55 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 3
%56 = load i64, i64* %55
%57 = icmp eq i64 %54, %56
br i1 %57, label %60, label %58
58: ; preds = %52, %46, %40, %34
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %52
%61 = call i32 @close(i32 %7)
ret i32 0
}