#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ sched_yield();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ usleep(int);
int get_proc_num(void) {
  int fd = -1;
  int fsize = -1;
  char filename[256];
  char fbuff[512];
  sprintf(filename, "/proc/%d/stat", getpid());
  if ((fd = open(filename, O_RDONLY)) == -1) {
    perror("get_proc_num(): open()");
    return -1;
  }
  usleep(6);
  sched_yield();
  if ((fsize = read(fd, fbuff, 512)) == -1) {
    perror("main(): read()");
    return -1;
  }
  close(fd);
  return atoi(&fbuff[fsize - 2]);
}


x86 assembly (clang, -O0):
<get_proc_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x320,%rsp
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x318(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x318(%rbp),%rdi
mov    %eax,%edx
lea    0xe32(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    120c <get_proc_num+0x7c>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <get_proc_num+0xe7>
mov    $0x6,%edi
call   10b0 <usleep@plt>
mov    $0x0,%al
call   1050 <sched_yield@plt>
mov    -0x8(%rbp),%edi
lea    -0x310(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1255 <get_proc_num+0xc5>
lea    0xde1(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <get_proc_num+0xe7>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
cltq
lea    -0x310(%rbp),%rdi
add    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x320,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_num>:
push   %rbp
push   %r14
push   %rbx
sub    $0x300,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe57(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1220 <get_proc_num+0x90>
mov    %eax,%ebx
mov    $0x6,%edi
call   10b0 <usleep@plt>
xor    %eax,%eax
call   1050 <sched_yield@plt>
lea    0x100(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1229 <get_proc_num+0x99>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
movslq %ebp,%rax
lea    (%rax,%r14,1),%rdi
add    $0xfffffffffffffffe,%rdi
call   1090 <atoi@plt>
add    $0x300,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde7(%rip),%rdi
jmp    1230 <get_proc_num+0xa0>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1214 <get_proc_num+0x84>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"get_proc_num(): open()\00"
@.str.2 = [15 x i8] c"main(): read()\00"
define i32 @get_proc_num() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca [512 x i8]
store i32 -1, i32* %2
store i32 -1, i32* %3
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i32 (...) @getpid()
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %32
15: ; preds = %0
%16 = call i32 @usleep(i32 6)
%17 = call i32 (...) @sched_yield()
%18 = load i32, i32* %2
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 512)
store i32 %20, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %32
24: ; preds = %15
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %3
%28 = sub nsw i32 %27, 2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %1
br label %32
32: ; preds = %24, %22, %13
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"get_proc_num(): open()\00"
@.str.2 = [15 x i8] c"main(): read()\00"
define i32 @get_proc_num() {
%1 = alloca [256 x i8]
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %3, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %25
12: ; preds = %0
%13 = tail call i32 @usleep(i32 6)
%14 = tail call i32 (...) @sched_yield()
%15 = call i32 @read(i32 %8, i8* %4, i32 512)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %25
19: ; preds = %12
%20 = tail call i32 @close(i32 %8)
%21 = add nsw i32 %15, -2
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %22
%24 = call i32 @atoi(i8* %23)
br label %25
25: ; preds = %19, %17, %10
%26 = phi i32 [ -1, %10 ], [ -1, %17 ], [ %24, %19 ]
ret i32 %26
}