#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TIOCGPGRP;
int getpgrp();
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int *);
int foreground_proc(void) {
  int ctty_pgrp;
  static pid_t pgrp = -1;
  if (pgrp == -1)
    pgrp = getpgrp();
  return (
      (ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 && ctty_pgrp == pgrp));
}


x86 assembly (clang, -O0):
<foreground_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0xffffffff,0x2f01(%rip)
jne    1142 <foreground_proc+0x22>
mov    $0x0,%al
call   1040 <getpgrp@plt>
mov    %eax,0x2eee(%rip)
mov    0x2ea7(%rip),%rax
mov    (%rax),%edi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    -0x4(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x5(%rbp)
je     117c <foreground_proc+0x5c>
mov    -0x4(%rbp),%eax
cmp    0x2eba(%rip),%eax
sete   %al
mov    %al,-0x5(%rbp)
mov    -0x5(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foreground_proc>:
push   %rax
cmpl   $0xffffffff,0x2f08(%rip)
jne    1137 <foreground_proc+0x17>
xor    %eax,%eax
call   1040 <getpgrp@plt>
mov    %eax,0x2ef9(%rip)
mov    0x2eb2(%rip),%rax
mov    (%rax),%edi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    0x4(%rsp),%ecx
cmp    0x2ecd(%rip),%ecx
sete   %cl
and    %al,%cl
movzbl %cl,%eax
pop    %rcx
ret

LLVM IR (-O0):
@foreground_proc.pgrp = internal global i32 -1
@STDOUT_FILENO = global i32 0
@TIOCGPGRP = global i32 0
define i32 @foreground_proc() {
%1 = alloca i32
%2 = load i32, i32* @foreground_proc.pgrp
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = call i32 (...) @getpgrp()
store i32 %5, i32* @foreground_proc.pgrp
br label %6
6: ; preds = %4, %0
%7 = load i32, i32* @STDOUT_FILENO
%8 = load i32, i32* @TIOCGPGRP
%9 = call i32 @ioctl(i32 %7, i32 %8, i32* %1)
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = load i32, i32* %1
%13 = load i32, i32* @foreground_proc.pgrp
%14 = icmp eq i32 %12, %13
br label %15
15: ; preds = %11, %6
%16 = phi i1 [ false, %6 ], [ %14, %11 ]
%17 = zext i1 %16 to i32
ret i32 %17
}

LLVM IR (-O2):
@foreground_proc.pgrp = internal unnamed_addr global i32 -1
@STDOUT_FILENO = global i32 0
@TIOCGPGRP = global i32 0
define i32 @foreground_proc() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @foreground_proc.pgrp
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 (...) @getpgrp()
store i32 %6, i32* @foreground_proc.pgrp
br label %7
7: ; preds = %5, %0
%8 = load i32, i32* @STDOUT_FILENO
%9 = load i32, i32* @TIOCGPGRP
%10 = call i32 @ioctl(i32 %8, i32 %9, i32* %1)
%11 = icmp ne i32 %10, -1
%12 = load i32, i32* %1
%13 = load i32, i32* @foreground_proc.pgrp
%14 = icmp eq i32 %12, %13
%15 = select i1 %11, i1 %14, i1 false
%16 = zext i1 %15 to i32
ret i32 %16
}