#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int check_credentials(int, int, int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpgrp();
int getpid();
int getppid();
int getsid(int /*<<< orphan*/);
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ parse_credentials(int, char *, int, int, int, int);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ sleep(int);
void process(char *name, pid_t *cids, int cnum) {
  pid_t pid = getpid();
  pid_t ppid = getppid();
  pid_t pgrp = getpgrp();
  pid_t sid = getsid(0);
  FILE *fp;
  fp = fopen(name, "w");
  fprintf(fp, "pid/spid\tppid/sppid\tsid/ssid\n");
  fclose(fp);
  while (1) {
    int ret = 0, i;
    fp = fopen(name, "a");
    fprintf(fp, "%d/%d\t%d/%d\t%d/%d\n", getpid(), pid, getppid(), ppid,
            getsid(0), sid);
    fclose(fp);
    ret = check_credentials(pid, ppid, pgrp, sid);
    if (ret) {
      parse_credentials(ret, name, pid, ppid, pgrp, sid);
    }
    for (i = 0; i < cnum; i++) {
      if (kill(cids[i], 0) < 0) {
        printf("%s: no child #%d\n", name, i);
      }
    }
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10d0 <getppid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10b0 <getpgrp@plt>
mov    %eax,-0x20(%rbp)
xor    %edi,%edi
call   1070 <getsid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   10d0 <getppid@plt>
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
xor    %edi,%edi
call   1070 <getsid@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%edx
mov    -0x44(%rbp),%ecx
mov    -0x40(%rbp),%r8d
mov    -0x3c(%rbp),%r9d
mov    %eax,%r10d
mov    -0x24(%rbp),%eax
lea    0xd8f(%rip),%rsi
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
call   1030 <check_credentials@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12e3 <process+0x123>
mov    -0x34(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x24(%rbp),%r9d
call   10a0 <parse_credentials@plt>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1338 <process+0x178>
mov    -0x10(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %esi,%esi
call   1090 <kill@plt>
cmp    $0x0,%rax
jge    1325 <process+0x165>
mov    -0x8(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    0xd15(%rip),%rdi
call   1060 <printf@plt>
jmp    132a <process+0x16a>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12ea <process+0x12a>
mov    $0x1,%edi
call   10e0 <sleep@plt>
jmp    122a <process+0x6a>

x86 assembly (clang, -O2):
<process>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x8(%rsp)
xor    %eax,%eax
call   10d0 <getppid@plt>
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   10b0 <getpgrp@plt>
mov    %eax,0xc(%rsp)
xor    %edi,%edi
call   1070 <getsid@plt>
mov    %rax,0x18(%rsp)
lea    0xde6(%rip),%rsi
mov    %r15,%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbp
lea    0xdd6(%rip),%rdi
mov    $0x1d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %ebx,0x14(%rsp)
mov    %ebx,%r12d
mov    %r15,0x20(%rsp)
jmp    126a <process+0x9a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    %r15,%rdi
lea    0xdac(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r13d
xor    %eax,%eax
call   10d0 <getppid@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1070 <getsid@plt>
mov    %eax,%r10d
mov    %r15,%rdi
lea    0xd7f(%rip),%rsi
mov    %r13d,%edx
mov    0x8(%rsp),%ebp
mov    %ebp,%ecx
mov    %ebx,%r8d
mov    0x10(%rsp),%ebx
mov    %ebx,%r9d
xor    %eax,%eax
mov    0x18(%rsp),%r13
push   %r13
push   %r10
call   1080 <fprintf@plt>
add    $0x10,%rsp
mov    %r15,%rdi
mov    0x20(%rsp),%r15
call   1050 <fclose@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    0xc(%rsp),%ebp
mov    %ebp,%edx
mov    %r13d,%ecx
call   1030 <check_credentials@plt>
test   %eax,%eax
je     1303 <process+0x133>
mov    %eax,%edi
mov    %r15,%rsi
mov    0x8(%rsp),%edx
mov    %ebx,%ecx
mov    %ebp,%r8d
mov    %r13d,%r9d
call   10a0 <parse_credentials@plt>
cmpl   $0x0,0x14(%rsp)
lea    0xd26(%rip),%rbx
jle    1260 <process+0x90>
xor    %ebp,%ebp
jmp    132d <process+0x15d>
nopl   0x0(%rax)
add    $0x1,%rbp
cmp    %rbp,%r12
je     1260 <process+0x90>
mov    (%r14,%rbp,4),%edi
xor    %esi,%esi
call   1090 <kill@plt>
test   %rax,%rax
jns    1320 <process+0x150>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <printf@plt>
jmp    1320 <process+0x150>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [30 x i8] c"pid/spid\09ppid/sppid\09sid/ssid\0A\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [19 x i8] c"%d/%d\09%d/%d\09%d/%d\0A\00"
@.str.4 = [18 x i8] c"%s: no child #%d\0A\00"
define void @process(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%14 = call i32 (...) @getpid()
store i32 %14, i32* %7
%15 = call i32 (...) @getppid()
store i32 %15, i32* %8
%16 = call i32 (...) @getpgrp()
store i32 %16, i32* %9
%17 = call i32 @getsid(i32 0)
store i32 %17, i32* %10
%18 = load i8*, i8** %4
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %11
%20 = load i32*, i32** %11
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32*, i32** %11
%23 = call i32 @fclose(i32* %22)
br label %24
24: ; preds = %3, %73
store i32 0, i32* %12
%25 = load i8*, i8** %4
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %11
%27 = load i32*, i32** %11
%28 = call i32 (...) @getpid()
%29 = load i32, i32* %7
%30 = call i32 (...) @getppid()
%31 = load i32, i32* %8
%32 = call i32 @getsid(i32 0)
%33 = load i32, i32* %10
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29, i32 %30, i32 %31, i32 %32, i32 %33)
%35 = load i32*, i32** %11
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %7
%38 = load i32, i32* %8
%39 = load i32, i32* %9
%40 = load i32, i32* %10
%41 = call i32 @check_credentials(i32 %37, i32 %38, i32 %39, i32 %40)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %24
%45 = load i32, i32* %12
%46 = load i8*, i8** %4
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = load i32, i32* %9
%50 = load i32, i32* %10
%51 = call i32 @parse_credentials(i32 %45, i8* %46, i32 %47, i32 %48, i32 %49, i32 %50)
br label %52
52: ; preds = %44, %24
store i32 0, i32* %13
br label %53
53: ; preds = %70, %52
%54 = load i32, i32* %13
%55 = load i32, i32* %6
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %73
57: ; preds = %53
%58 = load i32*, i32** %5
%59 = load i32, i32* %13
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %58, i64 %60
%62 = load i32, i32* %61
%63 = call i64 @kill(i32 %62, i32 0)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %57
%66 = load i8*, i8** %4
%67 = load i32, i32* %13
%68 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %66, i32 %67)
br label %69
69: ; preds = %65, %57
br label %70
70: ; preds = %69
%71 = load i32, i32* %13
%72 = add nsw i32 %71, 1
store i32 %72, i32* %13
br label %53
73: ; preds = %53
%74 = call i32 @sleep(i32 1)
br label %24
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [30 x i8] c"pid/spid\09ppid/sppid\09sid/ssid\0A\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [19 x i8] c"%d/%d\09%d/%d\09%d/%d\0A\00"
@.str.4 = [18 x i8] c"%s: no child #%d\0A\00"
define void @process(i8* %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (...) @getppid()
%6 = tail call i32 (...) @getpgrp()
%7 = tail call i32 @getsid(i32 0)
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i64 29, i64 1, i32* %8)
%10 = tail call i32 @fclose(i32* %8)
%11 = icmp sgt i32 %2, 0
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %37, %3
%14 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (...) @getppid()
%17 = tail call i32 @getsid(i32 0)
%18 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %4, i32 %16, i32 %5, i32 %17, i32 %7)
%19 = tail call i32 @fclose(i32* %14)
%20 = tail call i32 @check_credentials(i32 %4, i32 %5, i32 %6, i32 %7)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %13
%23 = tail call i32 @parse_credentials(i32 %20, i8* %0, i32 %4, i32 %5, i32 %6, i32 %7)
br label %24
24: ; preds = %22, %13
br i1 %11, label %25, label %37
25: ; preds = %24, %34
%26 = phi i64 [ %35, %34 ], [ 0, %24 ]
%27 = getelementptr inbounds i32, i32* %1, i64 %26
%28 = load i32, i32* %27
%29 = tail call i64 @kill(i32 %28, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = trunc i64 %26 to i32
%33 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %32)
br label %34
34: ; preds = %25, %31
%35 = add nuw nsw i64 %26, 1
%36 = icmp eq i64 %35, %12
br i1 %36, label %37, label %25
37: ; preds = %34, %24
%38 = tail call i32 @sleep(i32 1)
br label %13
}