#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 pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(void) {
  FILE *fp;
  pid_t pid;
  char pid_str[6];
  char path[128];
  char buf[1024];
  pid = getpid();
  sprintf(pid_str, "%d", pid);
  strcpy(path, "/proc/self/task/");
  strcat(path, pid_str);
  strcat(path, "/status");
  fp = fopen(path, "r");
  if (fp == NULL) {
    perror("fail to open");
    exit(1);
  }
  while (fgets(buf, 1024, fp) != NULL)
    printf("%s", buf);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
lea    -0x1a(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe36(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0xa0(%rbp),%rdi
lea    -0x1a(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe14(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123d <main+0x9d>
lea    0xdf0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1242 <main+0xa2>
lea    -0x4a0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1279 <main+0xd9>
lea    -0x4a0(%rbp),%rsi
lea    0xdbc(%rip),%rdi
call   1060 <printf@plt>
jmp    1242 <main+0xa2>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x490,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe46(%rip),%rsi
lea    0xa(%rsp),%r14
mov    %r14,%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
lea    0xe33(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
lea    0xe25(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
lea    0xe1e(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1277 <main+0xd7>
mov    %rax,%r15
lea    0x90(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1260 <main+0xc0>
lea    0xdfc(%rip),%r14
lea    0x90(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1240 <main+0xa0>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x490,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [17 x i8] c"/proc/self/task/\00"
@.str.2 = [8 x i8] c"/status\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [13 x i8] c"fail to open\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [6 x i8]
%5 = alloca [128 x i8]
%6 = alloca [1024 x i8]
store i32 0, i32* %1
%7 = call i32 (...) @getpid()
store i32 %7, i32* %3
%8 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%14 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%15 = call i32 @strcat(i8* %13, i8* %14)
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%17 = call i32 @strcat(i8* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %19, i32** %2
%20 = load i32*, i32** %2
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %0
%23 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %0
br label %26
26: ; preds = %31, %25
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%28 = load i32*, i32** %2
%29 = call i32* @fgets(i8* %27, i32 1024, i32* %28)
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %32)
br label %26
34: ; preds = %26
%35 = load i32*, i32** %2
%36 = call i32 @fclose(i32* %35)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [17 x i8] c"/proc/self/task/\00"
@.str.2 = [8 x i8] c"/status\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [13 x i8] c"fail to open\00"
@.str.5 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca [6 x i8]
%2 = alloca [128 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = tail call i32 (...) @getpid()
%8 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @strcat(i8* %5, i8* %4)
%11 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %0
%15 = call i32* @fgets(i8* %6, i32 1024, i32* %12)
%16 = icmp eq i32* %15, null
br i1 %16, label %24, label %20
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %14, %20
%21 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%22 = call i32* @fgets(i8* %6, i32 1024, i32* %12)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %20
24: ; preds = %20, %14
%25 = call i32 @fclose(i32* %12)
ret i32 0
}