#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, char *, char *, int *, int *);
char *getenv(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char const *);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("usage: %s <process id>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  const char *proc = getenv("PROC_PATH");
  if (!proc)
    proc = "/proc";
  char path[strlen(proc) + 1 + strlen(argv[1]) + 1 + strlen("stat") + 1];
  sprintf(path, "%s/%s/stat", proc, argv[1]);
  FILE *f = fopen(path, "r");
  if (!f) {
    fprintf(stderr, "could not open '%s'\n", path);
    exit(EXIT_FAILURE);
  }
  {
    int pid;
    char comm[32];
    char state;
    int ppid;
    int pgrp;
    int r = fscanf(f, "%d %s %c %d %d", &pid, comm, &state, &ppid, &pgrp);
    if (r != 5) {
      fprintf(stderr, "could not read from '%s'\n", path);
      exit(EXIT_FAILURE);
    }
    printf("got info: %d %s %c %d %d\n", pid, comm, state, ppid, pgrp);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c6 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe4b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ec <main+0x6c>
lea    0xe3a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x88(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x88(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x80(%rbp)
lea    0xe0d(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x80(%rbp),%rdx
mov    %rax,%rcx
lea    0x3(%rcx,%rdx,1),%rax
mov    %rsp,%rsi
mov    %rsi,-0x20(%rbp)
lea    0x12(%rcx,%rdx,1),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x78(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xdc2(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x78(%rbp),%rdi
lea    0xdbd(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12b1 <main+0x131>
mov    -0x78(%rbp),%rdx
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
lea    -0x60(%rbp),%rcx
lea    0xd8a(%rip),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x61(%rbp),%r8
lea    -0x68(%rbp),%r9
lea    -0x6c(%rbp),%rax
sub    $0x10,%rsp
mov    %rax,(%rsp)
call   1060 <fscanf@plt>
add    $0x10,%rsp
mov    %eax,-0x70(%rbp)
cmpl   $0x5,-0x70(%rbp)
je     1315 <main+0x195>
mov    -0x78(%rbp),%rdx
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x34(%rbp),%esi
lea    -0x60(%rbp),%rdx
movsbl -0x61(%rbp),%ecx
mov    -0x68(%rbp),%r8d
mov    -0x6c(%rbp),%r9d
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    126a <main+0xea>
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe71(%rip),%rbx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    0x8(%r14),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
add    %r15,%rax
add    $0x16,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe41(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1090 <sprintf@plt>
lea    0xe37(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     127d <main+0xfd>
sub    $0x8,%rsp
lea    -0x28(%rbp),%rbx
lea    0xe32(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x50(%rbp),%rcx
lea    -0x21(%rbp),%r8
lea    -0x2c(%rbp),%r9
mov    %rax,%rdi
push   %rbx
call   1060 <fscanf@plt>
add    $0x10,%rsp
cmp    $0x5,%eax
jne    128f <main+0x10f>
mov    -0x30(%rbp),%esi
movsbl -0x21(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x28(%rbp),%r9d
lea    0xe23(%rip),%rdi
lea    -0x50(%rbp),%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xd8c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a7 <main+0x127>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
jmp    129f <main+0x11f>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"usage: %s <process id>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"PROC_PATH\00"
@.str.2 = [6 x i8] c"/proc\00"
@.str.3 = [5 x i8] c"stat\00"
@.str.4 = [11 x i8] c"%s/%s/stat\00"
@.str.5 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.6 = [21 x i8] c"could not open '%s'\0A\00"
@.str.7 = [15 x i8] c"%d %s %c %d %d\00"
@.str.8 = [26 x i8] c"could not read from '%s'\0A\00"
@.str.9 = [26 x i8] c"got info: %d %s %c %d %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [32 x i8]
%12 = alloca i8
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %25
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %25
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %6
br label %30
30: ; preds = %29, %25
%31 = load i8*, i8** %6
%32 = call i64 @strlen(i8* %31)
%33 = add nsw i64 %32, 1
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i64 @strlen(i8* %36)
%38 = add nsw i64 %33, %37
%39 = add nsw i64 %38, 1
%40 = call i64 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%41 = add nsw i64 %39, %40
%42 = add nsw i64 %41, 1
%43 = call i8* @llvm.stacksave()
store i8* %43, i8** %7
%44 = alloca i8, i64 %42
store i64 %42, i64* %8
%45 = load i8*, i8** %6
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %45, i8* %48)
%50 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %50, i32** %9
%51 = load i32*, i32** %9
%52 = icmp ne i32* %51, null
br i1 %52, label %58, label %53
53: ; preds = %30
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %44)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %30
%59 = load i32*, i32** %9
%60 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%61 = call i32 @fscanf(i32* %59, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32* %10, i8* %60, i8* %12, i32* %13, i32* %14)
store i32 %61, i32* %15
%62 = load i32, i32* %15
%63 = icmp ne i32 %62, 5
br i1 %63, label %64, label %69
64: ; preds = %58
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i8* %44)
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %58
%70 = load i32, i32* %10
%71 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%72 = load i8, i8* %12
%73 = sext i8 %72 to i32
%74 = load i32, i32* %13
%75 = load i32, i32* %14
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i32 %70, i8* %71, i32 %73, i32 %74, i32 %75)
store i32 0, i32* %3
%77 = load i8*, i8** %7
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@.str = [24 x i8] c"usage: %s <process id>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"PROC_PATH\00"
@.str.2 = [6 x i8] c"/proc\00"
@.str.4 = [11 x i8] c"%s/%s/stat\00"
@.str.5 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.6 = [21 x i8] c"could not open '%s'\0A\00"
@.str.7 = [15 x i8] c"%d %s %c %d %d\00"
@.str.8 = [26 x i8] c"could not read from '%s'\0A\00"
@.str.9 = [26 x i8] c"got info: %d %s %c %d %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = icmp eq i32 %0, 2
br i1 %8, label %14, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i8* %15, null
%17 = select i1 %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %15
%18 = tail call i64 @strlen(i8* dereferenceable(1) %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i64 @strlen(i8* dereferenceable(1) %20)
%22 = add i64 %18, 7
%23 = add i64 %22, %21
%24 = alloca i8, i64 %23
%25 = call i32 @sprintf(i8* %24, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %17, i8* %20)
%26 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %33
28: ; preds = %14
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %24)
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %14
%34 = bitcast i32* %3 to i8*
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%36 = bitcast i32* %6 to i8*
%37 = bitcast i32* %7 to i8*
%38 = call i32 @fscanf(i32* %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32* %3, i8* %35, i8* %5, i32* %6, i32* %7)
%39 = icmp eq i32 %38, 5
br i1 %39, label %45, label %40
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i8* %24)
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %33
%46 = load i32, i32* %3
%47 = load i8, i8* %5
%48 = sext i8 %47 to i32
%49 = load i32, i32* %6
%50 = load i32, i32* %7
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i32 %46, i8* %35, i32 %48, i32 %49, i32 %50)
ret i32 0
}