#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*/ STDIN_FILENO;
int getpgrp();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int tcgetpgrp(int /*<<< orphan*/);
void judge(void) {
  pid_t pid;
  pid = tcgetpgrp(STDIN_FILENO);
  if (pid == -1) {
    perror("tcgetpgrp");
    return;
  } else if (pid == getpgrp()) {
    printf("foreground\n");
  } else {
    printf("background\n");
  }
}


x86 assembly (clang, -O0):
<judge>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
call   1040 <tcgetpgrp@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1174 <judge+0x34>
lea    0xe96(%rip),%rdi
call   1060 <perror@plt>
jmp    11b0 <judge+0x70>
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%eax
cmp    %ecx,%eax
jne    119f <judge+0x5f>
lea    0xe75(%rip),%rdi
call   1030 <printf@plt>
jmp    11ab <judge+0x6b>
lea    0xe70(%rip),%rdi
call   1030 <printf@plt>
jmp    11b0 <judge+0x70>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<judge>:
push   %rbx
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
call   1040 <tcgetpgrp@plt>
cmp    $0xffffffff,%eax
je     117b <judge+0x3b>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpgrp@plt>
cmp    %eax,%ebx
jne    116e <judge+0x2e>
lea    0xead(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>
lea    0xe95(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>
lea    0xe7e(%rip),%rdi
pop    %rbx
jmp    1060 <perror@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"tcgetpgrp\00"
@.str.1 = [12 x i8] c"foreground\0A\00"
@.str.2 = [12 x i8] c"background\0A\00"
define void @judge() {
%1 = alloca i32
%2 = load i32, i32* @STDIN_FILENO
%3 = call i32 @tcgetpgrp(i32 %2)
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %17
8: ; preds = %0
%9 = load i32, i32* %1
%10 = call i32 (...) @getpgrp()
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %16
14: ; preds = %8
%15 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %12
br label %17
17: ; preds = %6, %16
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [10 x i8] c"tcgetpgrp\00"
@str = [11 x i8] c"background\00"
@str.3 = [11 x i8] c"foreground\00"
define void @judge() {
%1 = load i32, i32* @STDIN_FILENO
%2 = tail call i32 @tcgetpgrp(i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %13
6: ; preds = %0
%7 = tail call i32 (...) @getpgrp()
%8 = icmp eq i32 %2, %7
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.3, i64 0, i64 0))
br label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %13
13: ; preds = %11, %9, %4
ret void
}