#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpgid(int);
int getpgrp();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int setpgid(int, int);
int main(int argc, char *argv[]) {
  pid_t mypid, pid;
  int rc;
  mypid = getpid();
  printf("pgroup from getpgid(pid) = %d\n", getpgid(mypid));
  printf("pgroup from getpgrp() = %d\n", getpgrp());
  if (argc < 2)
    exit(EXIT_FAILURE);
  if ((pid = atoi(argv[1])) < 0) {
    printf(" give a running pid as an argument\n");
    exit(EXIT_FAILURE);
  }
  printf("pgroup for pid=%d = %d\n", pid, getpgid(pid));
  rc = setpgid(pid, mypid);
  printf("rc from setpgid(pid,mypid)= %d\n", rc);
  printf("pgroup for pid=%d = %d\n", pid, getpgid(pid));
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <getpgid@plt>
mov    %eax,%esi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getpgrp@plt>
mov    %eax,%esi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11d7 <main+0x67>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    120c <main+0x9c>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <getpgid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1080 <setpgid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <getpgid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1080 <getpgid@plt>
lea    0xe60(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getpgrp@plt>
lea    0xe68(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x1,%ebp
jle    11de <main+0x5e>
mov    0x8(%r14),%rdi
call   1070 <atoi@plt>
test   %eax,%eax
jns    11ec <main+0x6c>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <getpgid@plt>
lea    0xe3f(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1090 <setpgid@plt>
lea    0xe39(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1080 <getpgid@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"pgroup from getpgid(pid) = %d\0A\00"
@.str.1 = [28 x i8] c"pgroup from getpgrp() = %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [36 x i8] c" give a running pid as an argument\0A\00"
@.str.3 = [24 x i8] c"pgroup for pid=%d = %d\0A\00"
@.str.4 = [32 x i8] c"rc from setpgid(pid,mypid)= %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @getpid()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = call i32 @getpgid(i32 %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 (...) @getpgrp()
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = load i32, i32* %7
%32 = load i32, i32* %7
%33 = call i32 @getpgid(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %33)
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = call i32 @setpgid(i32 %35, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %7
%41 = load i32, i32* %7
%42 = call i32 @getpgid(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %40, i32 %42)
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @exit(i32 %44)
unreachable
}

LLVM IR (-O2):
@.str = [31 x i8] c"pgroup from getpgid(pid) = %d\0A\00"
@.str.1 = [28 x i8] c"pgroup from getpgrp() = %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [24 x i8] c"pgroup for pid=%d = %d\0A\00"
@.str.4 = [32 x i8] c"rc from setpgid(pid,mypid)= %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [35 x i8] c" give a running pid as an argument\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 @getpgid(i32 %3)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 (...) @getpgrp()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %0, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = tail call i32 @getpgid(i32 %15)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %22)
%24 = tail call i32 @setpgid(i32 %15, i32 %3)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = tail call i32 @getpgid(i32 %15)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %26)
%28 = load i32, i32* @EXIT_SUCCESS
%29 = tail call i32 @exit(i32 %28)
unreachable
}