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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  FILE *stream;
  FILE *wstream;
  char buf[1024];
  memset(buf, '\0', sizeof(buf));
  printf("sizeof(buf)---->>>>  %d\n", sizeof(buf));
  printf("MAX_PATH---->>>>  %d\n", sizeof(256));
  printf("MAX_PATH---->>>>  %d\n", sizeof(char[1000]));
  stream = popen("ls -l", "r");
  wstream = fopen("test_popen.txt", "w+");
  fread(buf, sizeof(char), sizeof(buf), stream);
  fwrite(buf, 1, sizeof(buf), wstream);
  pclose(stream);
  fclose(wstream);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    0xe54(%rip),%rdi
mov    $0x400,%esi
call   1050 <printf@plt>
lea    0xe5c(%rip),%rdi
mov    $0x4,%esi
call   1050 <printf@plt>
lea    0xe4b(%rip),%rdi
mov    $0x3e8,%esi
call   1050 <printf@plt>
lea    0xe50(%rip),%rdi
lea    0xe4f(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe41(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   10a0 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1060 <pclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    0xe5b(%rip),%rdi
mov    $0x400,%esi
call   1050 <printf@plt>
lea    0xe63(%rip),%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   1050 <printf@plt>
lea    0xe58(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,%r15
lea    0xe4a(%rip),%rdi
lea    0xe52(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    %r15,%rdi
call   1060 <pclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [25 x i8] c"sizeof(buf)---->>>> %d\0A\00"
@.str.1 = [22 x i8] c"MAX_PATH---->>>> %d\0A\00"
@.str.2 = [6 x i8] c"ls -l\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [15 x i8] c"test_popen.txt\00"
@.str.5 = [3 x i8] c"w+\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i8 signext 0, i32 1024)
%7 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 1024)
%8 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 4)
%9 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 1000)
%10 = call i32* @popen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %10, i32** %2
%11 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %11, i32** %3
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i32 @fread(i8* %12, i32 1, i32 1024, i32* %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = load i32*, i32** %3
%17 = call i32 @fwrite(i8* %15, i32 1, i32 1024, i32* %16)
%18 = load i32*, i32** %2
%19 = call i32 @pclose(i32* %18)
%20 = load i32*, i32** %3
%21 = call i32 @fclose(i32* %20)
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"sizeof(buf)---->>>> %d\0A\00"
@.str.1 = [22 x i8] c"MAX_PATH---->>>> %d\0A\00"
@.str.2 = [6 x i8] c"ls -l\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [15 x i8] c"test_popen.txt\00"
@.str.5 = [3 x i8] c"w+\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i8 signext 0, i32 1024)
%4 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 1024)
%5 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 4)
%6 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 1000)
%7 = call i32* @popen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%8 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%9 = call i32 @fread(i8* %2, i32 1, i32 1024, i32* %7)
%10 = call i32 @fwrite(i8* %2, i32 1, i32 1024, i32* %8)
%11 = call i32 @pclose(i32* %7)
%12 = call i32 @fclose(i32* %8)
ret i32 0
}