#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*/ bzero(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
int spaghetti_get_prid(char *fullpath_or_null, char *r_pridbuf,
                       unsigned int buflen) {
  char buf[128 + 1];
  bzero(buf, 128 + 1);
  FILE *f;
  if (fullpath_or_null) {
    f = fopen(fullpath_or_null, "rb");
  } else {
    f = fopen("./c4a-prof", "rb");
  }
  if (!f) {
    return (-1);
  }
  if (!fgets(buf, 128, f)) {
    fclose(f);
    return (-1);
  }
  fclose(f);
  if (strchr(buf, '\n')) {
    *strchr(buf, '\n') = '\0';
  }
  strncpy(r_pridbuf, buf, buflen);
  return (0);
}


x86 assembly (clang, -O0):
<spaghetti_get_prid>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0xa0(%rbp),%rdi
mov    $0x81,%esi
call   1080 <bzero@plt>
cmpq   $0x0,-0x10(%rbp)
je     11ae <spaghetti_get_prid+0x4e>
mov    -0x10(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0xa8(%rbp)
jmp    11c8 <spaghetti_get_prid+0x68>
lea    0xe4e(%rip),%rdi
lea    0xe44(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jne    11e2 <spaghetti_get_prid+0x82>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <spaghetti_get_prid+0x110>
lea    -0xa0(%rbp),%rdi
mov    -0xa8(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    121b <spaghetti_get_prid+0xbb>
mov    -0xa8(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <spaghetti_get_prid+0x110>
mov    -0xa8(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0xa0(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     1256 <spaghetti_get_prid+0xf6>
lea    -0xa0(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spaghetti_get_prid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x81,%esi
call   1080 <bzero@plt>
test   %rbx,%rbx
je     1196 <spaghetti_get_prid+0x36>
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdi
jmp    11a4 <spaghetti_get_prid+0x44>
lea    0xe66(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11fb <spaghetti_get_prid+0x9b>
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r12d,%r12d
je     11fb <spaghetti_get_prid+0x9b>
mov    %rsp,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11eb <spaghetti_get_prid+0x8b>
movb   $0x0,(%rax)
mov    %rsp,%rsi
mov    %r15,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [11 x i8] c"./c4a-prof\00"
define i32 @spaghetti_get_prid(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [129 x i8]
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = getelementptr inbounds [129 x i8], [129 x i8]* %8, i64 0, i64 0
%11 = call i32 @bzero(i8* %10, i32 129)
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
br label %19
17: ; preds = %3
%18 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %9
br label %19
19: ; preds = %17, %14
%20 = load i32*, i32** %9
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %19
store i32 -1, i32* %4
br label %45
23: ; preds = %19
%24 = getelementptr inbounds [129 x i8], [129 x i8]* %8, i64 0, i64 0
%25 = load i32*, i32** %9
%26 = call i32 @fgets(i8* %24, i32 128, i32* %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %23
%29 = load i32*, i32** %9
%30 = call i32 @fclose(i32* %29)
store i32 -1, i32* %4
br label %45
31: ; preds = %23
%32 = load i32*, i32** %9
%33 = call i32 @fclose(i32* %32)
%34 = getelementptr inbounds [129 x i8], [129 x i8]* %8, i64 0, i64 0
%35 = call i8* @strchr(i8* %34, i8 signext 10)
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = getelementptr inbounds [129 x i8], [129 x i8]* %8, i64 0, i64 0
%39 = call i8* @strchr(i8* %38, i8 signext 10)
store i8 0, i8* %39
br label %40
40: ; preds = %37, %31
%41 = load i8*, i8** %6
%42 = getelementptr inbounds [129 x i8], [129 x i8]* %8, i64 0, i64 0
%43 = load i32, i32* %7
%44 = call i32 @strncpy(i8* %41, i8* %42, i32 %43)
store i32 0, i32* %4
br label %45
45: ; preds = %40, %28, %22
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [11 x i8] c"./c4a-prof\00"
define i32 @spaghetti_get_prid(i8* %0, i8* %1, i32 %2) {
%4 = alloca [129 x i8]
%5 = getelementptr inbounds [129 x i8], [129 x i8]* %4, i64 0, i64 0
%6 = call i32 @bzero(i8* %5, i32 129)
%7 = icmp eq i8* %0, null
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %12
10: ; preds = %3
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %8
%13 = phi i32* [ %9, %8 ], [ %11, %10 ]
%14 = icmp eq i32* %13, null
br i1 %14, label %25, label %15
15: ; preds = %12
%16 = call i32 @fgets(i8* %5, i32 128, i32* %13)
%17 = icmp eq i32 %16, 0
%18 = call i32 @fclose(i32* %13)
br i1 %17, label %25, label %19
19: ; preds = %15
%20 = call i8* @strchr(i8* %5, i8 signext 10)
%21 = icmp eq i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %19
store i8 0, i8* %20
br label %23
23: ; preds = %22, %19
%24 = call i32 @strncpy(i8* %1, i8* %5, i32 %2)
br label %25
25: ; preds = %15, %12, %23
%26 = phi i32 [ 0, %23 ], [ -1, %12 ], [ -1, %15 ]
ret i32 %26
}