#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 /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ snprintf(char *, int, char *, int);
int openProcessFiles(int pid, int *fdStatProc, int *fdStatm) {
  char file[256] = "";
  if (snprintf(file, 256 - 1, "/proc/%d/stat", pid) < 0) {
    perror("snprintf failed");
    exit(-1);
  }
  if ((*fdStatProc = open(file, O_RDONLY)) == -1) {
    return -1;
  }
  if (snprintf(file, 256 - 1, "/proc/%d/statm", pid) < 0) {
    perror("snprintf failed");
    exit(-1);
  }
  if ((*fdStatm = open(file, O_RDONLY)) == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<openProcessFiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe71(%rip),%rdx
call   1030 <snprintf@plt>
cmp    $0x0,%rax
jge    11b4 <openProcessFiles+0x64>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    11e4 <openProcessFiles+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <openProcessFiles+0x106>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe24(%rip),%rdx
call   1030 <snprintf@plt>
cmp    $0x0,%rax
jge    121f <openProcessFiles+0xcf>
lea    0xdfe(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    -0x18(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    124f <openProcessFiles+0xff>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <openProcessFiles+0x106>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openProcessFiles>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe3a(%rip),%rdx
mov    %rsp,%rdi
mov    $0xff,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
test   %rax,%rax
js     123d <openProcessFiles+0xfd>
mov    0x2e07(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdi
call   1040 <open@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     122a <openProcessFiles+0xea>
lea    0xe24(%rip),%rdx
mov    %rsp,%rdi
mov    $0xff,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
test   %rax,%rax
js     123d <openProcessFiles+0xfd>
mov    (%r15),%esi
mov    %rsp,%rdi
call   1040 <open@plt>
mov    %eax,%ecx
mov    %eax,(%r14)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
jmp    122f <openProcessFiles+0xef>
mov    $0xffffffff,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdca(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [16 x i8] c"snprintf failed\00"
@O_RDONLY = global i32 0
@.str.2 = [15 x i8] c"/proc/%d/statm\00"
define i32 @openProcessFiles(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [256 x i8]
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%9 = bitcast [256 x i8]* %8 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i64 @snprintf(i8* %10, i32 255, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %3
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
%21 = load i32*, i32** %6
store i32 %20, i32* %21
%22 = icmp eq i32 %20, -1
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 -1, i32* %4
br label %40
24: ; preds = %17
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%26 = load i32, i32* %5
%27 = call i64 @snprintf(i8* %25, i32 255, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
%36 = load i32*, i32** %7
store i32 %35, i32* %36
%37 = icmp eq i32 %35, -1
br i1 %37, label %38, label %39
38: ; preds = %32
store i32 -1, i32* %4
br label %40
39: ; preds = %32
store i32 0, i32* %4
br label %40
40: ; preds = %39, %38, %23
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [16 x i8] c"snprintf failed\00"
@O_RDONLY = global i32 0
@.str.2 = [15 x i8] c"/proc/%d/statm\00"
define i32 @openProcessFiles(i32 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = call i64 @snprintf(i8* %5, i32 255, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %5, i32 %12)
store i32 %13, i32* %1
%14 = icmp eq i32 %13, -1
br i1 %14, label %26, label %15
15: ; preds = %11
%16 = call i64 @snprintf(i8* %5, i32 255, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %15
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %5, i32 %22)
store i32 %23, i32* %2
%24 = icmp eq i32 %23, -1
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %21, %11
%27 = phi i32 [ -1, %11 ], [ %25, %21 ]
ret i32 %27
}