#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*/ feof(int /*<<< orphan*/ *);
unsigned char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int main(int args, char *argv[]) {
  FILE *fp;
  char path[80];
  unsigned char ch;
  if (args != 2) {
    printf("format: readproc pid\n");
    return -1;
  }
  sprintf(path, "/proc/%s/cmdline", argv[1]);
  printf("%s\n", path);
  if ((fp = fopen(path, "r")) == NULL) {
    perror("fopen");
    return 0;
  }
  while (!feof(fp)) {
    ch = fgetc(fp);
    printf("%c %d\n", ch, ch);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ad <main+0x3d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <main+0xf7>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe56(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x70(%rbp),%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    120d <main+0x9d>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1267 <main+0xf7>
jmp    1212 <main+0xa2>
mov    -0x18(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1230 <main+0xc0>
jmp    1257 <main+0xe7>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x71(%rbp)
movzbl -0x71(%rbp),%esi
movzbl -0x71(%rbp),%edx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1212 <main+0xa2>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
cmp    $0x2,%edi
jne    120f <main+0x8f>
mov    0x8(%rsi),%rdx
lea    0xe65(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
lea    0xe5c(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1228 <main+0xa8>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1203 <main+0x83>
lea    0xe41(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
movzbl %al,%edx
mov    %r14,%rdi
mov    %edx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11e0 <main+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1220 <main+0xa0>
lea    0xe0a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde4(%rip),%rdi
call   1090 <perror@plt>
jmp    120b <main+0x8b>

LLVM IR (-O0):
@.str = [22 x i8] c"format: readproc pid\0A\00"
@.str.1 = [17 x i8] c"/proc/%s/cmdline\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"fopen\00"
@.str.5 = [7 x i8] c"%c %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [80 x i8]
%8 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
13: ; preds = %2
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %22, i32** %6
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %43
26: ; preds = %13
br label %27
27: ; preds = %32, %26
%28 = load i32*, i32** %6
%29 = call i32 @feof(i32* %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = load i32*, i32** %6
%34 = call zeroext i8 @fgetc(i32* %33)
store i8 %34, i8* %8
%35 = load i8, i8* %8
%36 = zext i8 %35 to i32
%37 = load i8, i8* %8
%38 = zext i8 %37 to i32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %38)
br label %27
40: ; preds = %27
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
store i32 0, i32* %3
br label %43
43: ; preds = %40, %24, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"/proc/%s/cmdline\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"fopen\00"
@.str.5 = [7 x i8] c"%c %d\0A\00"
@str = [21 x i8] c"format: readproc pid\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %28
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 @puts(i8* %4)
%13 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %8
%16 = tail call i32 @feof(i32* %13)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %26
18: ; preds = %8
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %28
20: ; preds = %15, %20
%21 = tail call zeroext i8 @fgetc(i32* %13)
%22 = zext i8 %21 to i32
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %22, i32 %22)
%24 = tail call i32 @feof(i32* %13)
%25 = icmp eq i32 %24, 0
br i1 %25, label %20, label %26
26: ; preds = %20, %15
%27 = tail call i32 @fclose(i32* %13)
br label %28
28: ; preds = %26, %18, %6
%29 = phi i32 [ -1, %6 ], [ 0, %18 ], [ 0, %26 ]
ret i32 %29
}