#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 */
char *bfile(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpgrp();
int setsid();
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *);
int /*<<< orphan*/ warn(char *, ...);
void write_gpidfile(const char *name) {
  pid_t gpid;
  FILE *fp;
  char path[2048];
  if ((gpid = setsid()) == -1) {
    if ((gpid = getpgrp()) == -1) {
      warn("getpgrp()");
      return;
    }
  }
  sprintf(path, "%s/%s.gpid", bfile("var/"), name);
  fp = fopen(path, "w");
  if (fp == NULL) {
    warn("fopen: %s", path);
    return;
  }
  fprintf(fp, "%d", gpid);
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_gpidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <setsid@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11cd <write_gpidfile+0x4d>
mov    $0x0,%al
call   1080 <getpgrp@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11c8 <write_gpidfile+0x48>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
jmp    1259 <write_gpidfile+0xd9>
jmp    11cd <write_gpidfile+0x4d>
lea    -0x820(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    0xe33(%rip),%rdi
call   1070 <bfile@plt>
mov    -0x828(%rbp),%rdi
mov    %rax,%rdx
mov    -0x8(%rbp),%rcx
lea    0xe0e(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x820(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123d <write_gpidfile+0xbd>
lea    -0x820(%rbp),%rsi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
jmp    1259 <write_gpidfile+0xd9>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xddb(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_gpidfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <setsid@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11aa <write_gpidfile+0x2a>
xor    %eax,%eax
call   1080 <getpgrp@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1226 <write_gpidfile+0xa6>
lea    0xe64(%rip),%rdi
call   1070 <bfile@plt>
lea    0xe4d(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
mov    %r14,%rcx
call   10a0 <sprintf@plt>
lea    0xe45(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1209 <write_gpidfile+0x89>
mov    %rax,%rbx
lea    0xe3a(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe0c(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd3(%rip),%rdi
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <warn@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"getpgrp()\00"
@.str.1 = [11 x i8] c"%s/%s.gpid\00"
@.str.2 = [5 x i8] c"var/\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [10 x i8] c"fopen: %s\00"
@.str.5 = [3 x i8] c"%d\00"
define void @write_gpidfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [2048 x i8]
store i8* %0, i8** %2
%6 = call i32 (...) @setsid()
store i32 %6, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = call i32 (...) @getpgrp()
store i32 %9, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %32
13: ; preds = %8
br label %14
14: ; preds = %13, %1
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%16 = call i8* @bfile(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%17 = load i8*, i8** %2
%18 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %17)
%19 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %20, i32** %4
%21 = load i32*, i32** %4
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%25 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %24)
br label %32
26: ; preds = %14
%27 = load i32*, i32** %4
%28 = load i32, i32* %3
%29 = call i32 @fprintf(i32* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
br label %32
32: ; preds = %26, %23, %11
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"getpgrp()\00"
@.str.1 = [11 x i8] c"%s/%s.gpid\00"
@.str.2 = [5 x i8] c"var/\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [10 x i8] c"fopen: %s\00"
@.str.5 = [3 x i8] c"%d\00"
define void @write_gpidfile(i8* %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @setsid()
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = tail call i32 (...) @getpgrp()
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %22
11: ; preds = %6, %1
%12 = phi i32 [ %7, %6 ], [ %4, %1 ]
%13 = tail call i8* @bfile(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %0)
%15 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %3)
br label %22
19: ; preds = %11
%20 = tail call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %12)
%21 = tail call i32 @fclose(i32* %15)
br label %22
22: ; preds = %19, %17, %9
ret void
}