#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int getpidproc(void) {
  int pid;
  FILE *f = fopen("/proc/self/stat", "r");
  if (!f) {
    fprintf(stderr, "can't open /proc/self/stat: %s\n", strerror(errno));
    return -1;
  }
  if (fscanf(f, "%d", &pid) != 1) {
    pid = -1;
    goto out;
  }
out:
  fclose(f);
  return pid;
}


x86 assembly (clang, -O0):
<getpidproc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b2 <getpidproc+0x62>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe71(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <getpidproc+0x9f>
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     11db <getpidproc+0x8b>
movl   $0xffffffff,-0x8(%rbp)
jmp    11e0 <getpidproc+0x90>
jmp    11e0 <getpidproc+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpidproc>:
push   %rbx
sub    $0x10,%rsp
lea    0xea4(%rip),%rdi
lea    0xead(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119f <getpidproc+0x4f>
mov    %rax,%rbx
lea    0xebb(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
je     1191 <getpidproc+0x41>
movl   $0xffffffff,0xc(%rsp)
ff
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    11cc <getpidproc+0x7c>
mov    0x2e3a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe55(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [32 x i8] c"can't open /proc/self/stat: %s\0A\00"
@errno = global i32 0
@.str.3 = [3 x i8] c"%d\00"
define i32 @getpidproc() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %12, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %10)
store i32 -1, i32* %1
br label %22
12: ; preds = %0
%13 = load i32*, i32** %3
%14 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%15 = icmp ne i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %2
br label %18
17: ; preds = %12
br label %18
18: ; preds = %17, %16
%19 = load i32*, i32** %3
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* %2
store i32 %21, i32* %1
br label %22
22: ; preds = %18, %7
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [32 x i8] c"can't open /proc/self/stat: %s\0A\00"
@errno = global i32 0
@.str.3 = [3 x i8] c"%d\00"
define i32 @getpidproc() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %17
10: ; preds = %0
%11 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %14, label %13
13: ; preds = %10
store i32 -1, i32* %1
br label %14
14: ; preds = %10, %13
%15 = call i32 @fclose(i32* %3)
%16 = load i32, i32* %1
br label %17
17: ; preds = %14, %5
%18 = phi i32 [ %16, %14 ], [ -1, %5 ]
ret i32 %18
}