#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 /*<<< orphan*/ free(char *);
scalar_t__ getpid();
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strtok_r(char *, char *, char **);
int get_mycpu() {
  int mypid, mycpu;
  char *mypath, *mystr, *token;
  char *work, *ptrptr;
  int myfd;
  int i;
  mypath = malloc(256);
  mystr = malloc(256);
  mypid = (int)getpid();
  sprintf(mypath, "/proc/%d/stat", mypid);
  myfd = open(mypath, O_RDONLY);
  (void)read(myfd, mystr, 256);
  token = strtok_r(mystr, " ", &ptrptr);
  for (i = 2; i < 40; i++) {
    token = strtok_r(NULL, " ", &ptrptr);
  }
  mycpu = atoi(token);
  close(myfd);
  free(mystr);
  free(mypath);
  return mycpu;
}


x86 assembly (clang, -O0):
<get_mycpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe34(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    -0x18(%rbp),%rdi
lea    0xe0c(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
movl   $0x2,-0x38(%rbp)
cmpl   $0x28,-0x38(%rbp)
jge    1246 <get_mycpu+0xb6>
xor    %eax,%eax
mov    %eax,%edi
lea    0xde3(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1060 <strtok_r@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1216 <get_mycpu+0x86>
mov    -0x20(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x8(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_mycpu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe3d(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
mov    %eax,%r12d
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
lea    0xe17(%rip),%r13
mov    %rsp,%rbp
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1060 <strtok_r@plt>
mov    $0x26,%ebx
nopl   (%rax)
xor    %edi,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1060 <strtok_r@plt>
add    $0xffffffff,%ebx
jne    1210 <get_mycpu+0x80>
mov    %rax,%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c" \00"
define i32 @get_mycpu() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = call i8* @malloc(i32 256)
store i8* %10, i8** %3
%11 = call i8* @malloc(i32 256)
store i8* %11, i8** %4
%12 = call i64 (...) @getpid()
%13 = trunc i64 %12 to i32
store i32 %13, i32* %1
%14 = load i8*, i8** %3
%15 = load i32, i32* %1
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i8*, i8** %3
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = load i8*, i8** %4
%22 = call i32 @read(i32 %20, i8* %21, i32 256)
%23 = load i8*, i8** %4
%24 = call i8* @strtok_r(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7)
store i8* %24, i8** %5
store i32 2, i32* %9
br label %25
25: ; preds = %30, %0
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 40
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7)
store i8* %29, i8** %5
br label %30
30: ; preds = %28
%31 = load i32, i32* %9
%32 = add nsw i32 %31, 1
store i32 %32, i32* %9
br label %25
33: ; preds = %25
%34 = load i8*, i8** %5
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %2
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
%38 = load i8*, i8** %4
%39 = call i32 @free(i8* %38)
%40 = load i8*, i8** %3
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c" \00"
define i32 @get_mycpu() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%4 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%5 = tail call i64 (...) @getpid()
%6 = trunc i64 %5 to i32
%7 = tail call i32 @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %3, i32 %8)
%10 = tail call i32 @read(i32 %9, i8* %4, i32 256)
%11 = call i8* @strtok_r(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %1)
br label %12
12: ; preds = %0, %12
%13 = phi i32 [ 2, %0 ], [ %15, %12 ]
%14 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %1)
%15 = add nuw nsw i32 %13, 1
%16 = icmp eq i32 %15, 40
br i1 %16, label %17, label %12
17: ; preds = %12
%18 = call i32 @atoi(i8* %14)
%19 = call i32 @close(i32 %9)
%20 = call i32 @free(i8* %4)
%21 = call i32 @free(i8* %3)
ret i32 %18
}