#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*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int main(int argc, char *argv[]) {
  int pid;
  char *pid_r;
  char input;
  FILE *fptr;
  printf("\n**Original file**\n\n");
  pid = getpid();
  fptr = fopen("my_pid.txt", "w");
  fprintf(fptr, "/proc/%d/exe", pid);
  fclose(fptr);
  fptr = fopen("my_pid.txt", "r");
  fscanf(fptr, "%s", pid_r);
  printf("the /proc/pid/exe path after reading is: %s\n", pid_r);
  printf("Enter \"e\" and then hit enter to terminate the program.\n");
  while (1) {
    scanf("%c", &input);
    if (input == 'e') {
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
lea    0xe70(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe5f(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe3d(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe3a(%rip),%rsi
call   1060 <fscanf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe76(%rip),%rdi
lea    -0x21(%rbp),%rsi
call   1080 <scanf@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x65,%eax
jne    123e <main+0xce>
jmp    1243 <main+0xd3>
jmp    121c <main+0xac>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xec0(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
lea    0xe5c(%rip),%r14
lea    0xe60(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0xe50(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe44(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
lea    0xe37(%rip),%rsi
mov    %rax,%rdi
call   1070 <fscanf@plt>
lea    0xe2b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3e(%rip),%r14
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <scanf@plt>
cmpb   $0x65,0xf(%rsp)
jne    1220 <main+0xa0>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"\0A**Original file**\0A\0A\00"
@.str.1 = [11 x i8] c"my_pid.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [13 x i8] c"/proc/%d/exe\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [45 x i8] c"the /proc/pid/exe path after reading is: %s\0A\00"
@.str.7 = [56 x i8] c"Enter \22e\22 and then hit enter to terminate the program.\0A\00"
@.str.8 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (...) @getpid()
store i32 %11, i32* %6
%12 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %12, i32** %9
%13 = load i32*, i32** %9
%14 = load i32, i32* %6
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = load i32*, i32** %9
%17 = call i32 @fclose(i32* %16)
%18 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %18, i32** %9
%19 = load i32*, i32** %9
%20 = load i8*, i8** %7
%21 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %20)
%22 = load i8*, i8** %7
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.7, i64 0, i64 0))
br label %25
25: ; preds = %2, %31
%26 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %8)
%27 = load i8, i8* %8
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 101
br i1 %29, label %30, label %31
30: ; preds = %25
br label %32
31: ; preds = %25
br label %25
32: ; preds = %30
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"my_pid.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [13 x i8] c"/proc/%d/exe\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [45 x i8] c"the /proc/pid/exe path after reading is: %s\0A\00"
@.str.8 = [3 x i8] c"%c\00"
@str = [20 x i8] c"\0A**Original file**\0A\00"
@str.9 = [55 x i8] c"Enter \22e\22 and then hit enter to terminate the program.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 (...) @getpid()
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%8 = tail call i32 @fclose(i32* %6)
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%10 = tail call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* undef)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i8* undef)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.9, i64 0, i64 0))
br label %13
13: ; preds = %13, %2
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %3)
%15 = load i8, i8* %3
%16 = icmp eq i8 %15, 101
br i1 %16, label %17, label %13
17: ; preds = %13
ret i32 0
}