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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
void write_track_xid(int type, unsigned short id, char *stats_path) {
  FILE *fp;
  switch (type) {
  case 0:
    strcat(stats_path, "/stats_track_gid");
    break;
  case 1:
    strcat(stats_path, "/stats_track_ppid");
    break;
  case 2:
    strcat(stats_path, "/stats_track_pid");
    break;
  }
  fp = fopen(stats_path, "w+");
  if (!fp) {
    fprintf(stderr, "Error: Couldn't open /proc entry file: %s\n", stats_path);
    exit(1);
  }
  if (fprintf(fp, "%d", id) < 0) {
    fprintf(stderr,
            "Error: Couldn't write id to tracking /proc"
            "entry file: %s\n",
            stats_path);
    exit(1);
  }
  if (fclose(fp)) {
    fprintf(stderr,
            "Error: Couldn't close tracking /proc entry"
            " file: %s\n",
            stats_path);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<write_track_xid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
test   %eax,%eax
je     119b <write_track_xid+0x4b>
jmp    1179 <write_track_xid+0x29>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
je     11b0 <write_track_xid+0x60>
jmp    118a <write_track_xid+0x3a>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     11c5 <write_track_xid+0x75>
jmp    11d5 <write_track_xid+0x85>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1060 <strcat@plt>
jmp    11d5 <write_track_xid+0x85>
mov    -0x10(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1060 <strcat@plt>
jmp    11d5 <write_track_xid+0x85>
mov    -0x10(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1219 <write_track_xid+0xc9>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
movzwl -0x6(%rbp),%edx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmp    $0x0,%rax
jge    1260 <write_track_xid+0x110>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1298 <write_track_xid+0x148>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_track_xid>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
cmp    $0x2,%edi
ja     1177 <write_track_xid+0x27>
movslq %edi,%rax
lea    0xf6c(%rip),%rcx
movslq (%rcx,%rax,4),%rsi
add    %rcx,%rsi
mov    %r14,%rdi
call   1060 <strcat@plt>
lea    0xeb6(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11b8 <write_track_xid+0x68>
mov    %rax,%rbx
movzwl %bp,%edx
lea    0xeca(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %rax,%rax
js     11ca <write_track_xid+0x7a>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jne    11dc <write_track_xid+0x8c>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
jmp    11ec <write_track_xid+0x9c>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
jmp    11ec <write_track_xid+0x9c>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xeb3(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"/stats_track_gid\00"
@.str.1 = [18 x i8] c"/stats_track_ppid\00"
@.str.2 = [17 x i8] c"/stats_track_pid\00"
@.str.3 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.4 = [43 x i8] c"Error: Couldn't open /proc entry file: %s\0A\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [58 x i8] c"Error: Couldn't write id to tracking /procentry file: %s\0A\00"
@.str.7 = [53 x i8] c"Error: Couldn't close tracking /proc entry file: %s\0A\00"
define void @write_track_xid(i32 %0, i16 zeroext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i16
%6 = alloca i8*
%7 = alloca i32*
store i32 %0, i32* %4
store i16 %1, i16* %5
store i8* %2, i8** %6
%8 = load i32, i32* %4
switch i32 %8, label %18 [
i32 0, label %9
i32 1, label %12
i32 2, label %15
]
9: ; preds = %3
%10 = load i8*, i8** %6
%11 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %18
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = call i32 @strcat(i8* %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %18
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i32 @strcat(i8* %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %3, %15, %12, %9
%19 = load i8*, i8** %6
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %20, i32** %7
%21 = load i32*, i32** %7
%22 = icmp ne i32* %21, null
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %6
%26 = call i64 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = load i32*, i32** %7
%30 = ptrtoint i32* %29 to i32
%31 = load i16, i16* %5
%32 = zext i16 %31 to i32
%33 = call i64 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %6
%38 = call i64 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.6, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %28
%41 = load i32*, i32** %7
%42 = call i64 @fclose(i32* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %6
%47 = call i64 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %40
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/stats_track_gid\00"
@.str.1 = [18 x i8] c"/stats_track_ppid\00"
@.str.2 = [17 x i8] c"/stats_track_pid\00"
@.str.3 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.4 = [43 x i8] c"Error: Couldn't open /proc entry file: %s\0A\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [58 x i8] c"Error: Couldn't write id to tracking /procentry file: %s\0A\00"
@.str.7 = [53 x i8] c"Error: Couldn't close tracking /proc entry file: %s\0A\00"
@reltable.write_track_xid = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([17 x i8]* @.str to i64), i64 ptrtoint ([3 x i32]* @reltable.write_track_xid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([18 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.write_track_xid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([17 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.write_track_xid to i64)) to i32)]
define void @write_track_xid(i32 %0, i16 zeroext %1, i8* %2) {
%4 = icmp ult i32 %0, 3
br i1 %4, label %5, label %10
5: ; preds = %3
%6 = sext i32 %0 to i64
%7 = shl i64 %6, 2
%8 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.write_track_xid to i8*), i64 %7)
%9 = tail call i32 @strcat(i8* %2, i8* %8)
br label %10
10: ; preds = %3, %5
%11 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i64 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = ptrtoint i32* %11 to i64
%19 = trunc i64 %18 to i32
%20 = zext i16 %1 to i32
%21 = tail call i64 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = tail call i64 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = tail call i64 @fclose(i32* %11)
%29 = icmp eq i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i64 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %27
ret void
}