#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
scalar_t__ getpgrp();
int open(char *, int);
scalar_t__ tcgetpgrp(int);
int main(void) {
  int ttyfd = open("/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (ttyfd < 0)
    return EXIT_FAILURE;
  pid_t tpgid = tcgetpgrp(ttyfd);
  if (tpgid < 0)
    return EXIT_FAILURE;
  pid_t pgid = getpgrp();
  return tpgid == pgid ? EXIT_SUCCESS : EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1184 <main+0x54>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e8 <main+0xb8>
mov    -0x8(%rbp),%edi
call   1030 <tcgetpgrp@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11ac <main+0x7c>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e8 <main+0xb8>
mov    $0x0,%al
call   1040 <getpgrp@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11d6 <main+0xa6>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11e2 <main+0xb2>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    0x2e91(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e96(%rip),%rax
or     (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1180 <main+0x50>
mov    %eax,%edi
call   1030 <tcgetpgrp@plt>
test   %rax,%rax
js     1180 <main+0x50>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpgrp@plt>
cmp    %rax,%rbx
jne    1180 <main+0x50>
mov    0x2e42(%rip),%rax
jmp    1187 <main+0x57>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %5, %6
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %1
br label %33
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i64 @tcgetpgrp(i32 %16)
store i64 %17, i64* %3
%18 = load i64, i64* %3
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = load i32, i32* @EXIT_FAILURE
store i32 %21, i32* %1
br label %33
22: ; preds = %15
%23 = call i64 (...) @getpgrp()
store i64 %23, i64* %4
%24 = load i64, i64* %3
%25 = load i64, i64* %4
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i32, i32* @EXIT_SUCCESS
br label %31
29: ; preds = %22
%30 = load i32, i32* @EXIT_FAILURE
br label %31
31: ; preds = %29, %27
%32 = phi i32 [ %28, %27 ], [ %30, %29 ]
store i32 %32, i32* %1
br label %33
33: ; preds = %31, %20, %13
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_NOCTTY
%3 = or i32 %2, %1
%4 = load i32, i32* @O_NONBLOCK
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = load i32, i32* @EXIT_FAILURE
br label %21
10: ; preds = %0
%11 = tail call i64 @tcgetpgrp(i32 %6)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32, i32* @EXIT_FAILURE
br label %21
15: ; preds = %10
%16 = tail call i64 (...) @getpgrp()
%17 = icmp eq i64 %11, %16
%18 = load i32, i32* @EXIT_SUCCESS
%19 = load i32, i32* @EXIT_FAILURE
%20 = select i1 %17, i32 %18, i32 %19
br label %21
21: ; preds = %13, %15, %8
%22 = phi i32 [ %9, %8 ], [ %14, %13 ], [ %20, %15 ]
ret i32 %22
}