#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int testarExistenciaProcesso(int pid) {
  int retorno = 0;
  FILE *stat;
  char nomeArquivo[50];
  sprintf(nomeArquivo, "/proc/%d/stat", pid);
  if ((stat = fopen(nomeArquivo, "r")) != NULL) {
    fclose(stat);
    retorno = 1;
  }
  return retorno;
}


x86 assembly (clang, -O0):
<testarExistenciaProcesso>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xeb0(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xeae(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1183 <testarExistenciaProcesso+0x53>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testarExistenciaProcesso>:
push   %rbx
sub    $0x40,%rsp
mov    %edi,%edx
lea    0xec2(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <sprintf@plt>
lea    0xebe(%rip),%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     116c <testarExistenciaProcesso+0x3c>
mov    %rax,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
jmp    116e <testarExistenciaProcesso+0x3e>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @testarExistenciaProcesso(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [50 x i8]
store i32 %0, i32* %2
store i32 0, i32* %3
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* %2
%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 [50 x i8], [50 x i8]* %5, i64 0, i64 0
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32*, i32** %4
%14 = call i32 @fclose(i32* %13)
store i32 1, i32* %3
br label %15
15: ; preds = %12, %1
%16 = load i32, i32* %3
ret i32 %16
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @testarExistenciaProcesso(i32 %0) {
%2 = alloca [50 x i8]
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = tail call i32 @fclose(i32* %5)
br label %9
9: ; preds = %7, %1
%10 = phi i32 [ 1, %7 ], [ 0, %1 ]
ret i32 %10
}