#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 WEXITSTATUS(int);
int fgetc(int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int RunProcess(char *cmd, char *buf, int limit, int *len, int *err) {
  int success = 0;
  *err = 0;
  FILE *F;
  F = popen(cmd, "r");
  if (F != NULL) {
    int n, i = 0;
    n = fgetc(F);
    while ((n >= 0) && (i < limit)) {
      buf[i++] = (char)n;
      n = fgetc(F);
    }
    *len = i;
    int ex_code = pclose(F);
    *err = WEXITSTATUS(ex_code);
    if (!*err)
      success = 1;
  }
  return success;
}


x86 assembly (clang, -O0):
<RunProcess>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1232 <RunProcess+0xf2>
movl   $0x0,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x3c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x3c(%rbp)
mov    %al,-0x45(%rbp)
jl     11b9 <RunProcess+0x79>
mov    -0x40(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    11c9 <RunProcess+0x89>
jmp    11f4 <RunProcess+0xb4>
mov    -0x3c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x40(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x3c(%rbp)
jmp    119e <RunProcess+0x5e>
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <pclose@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
jne    122d <RunProcess+0xed>
movl   $0x1,-0x2c(%rbp)
jmp    1232 <RunProcess+0xf2>
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RunProcess>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r15
mov    %rcx,%rbx
mov    %edx,%r13d
mov    %rsi,%r12
movl   $0x0,(%r8)
lea    0xe9b(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     11cc <RunProcess+0x8c>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <fgetc@plt>
xor    %ebp,%ebp
test   %eax,%eax
js     11ad <RunProcess+0x6d>
test   %r13d,%r13d
jle    11ad <RunProcess+0x6d>
mov    %rbx,(%rsp)
mov    %r13d,%ebx
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %al,(%r12,%rbp,1)
add    $0x1,%rbp
mov    %r14,%rdi
call   1040 <fgetc@plt>
test   %eax,%eax
js     11a9 <RunProcess+0x69>
cmp    %rbx,%rbp
jb     1190 <RunProcess+0x50>
mov    (%rsp),%rbx
mov    %ebp,(%rbx)
mov    %r14,%rdi
call   1030 <pclose@plt>
mov    %eax,%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ecx
mov    %eax,(%r15)
xor    %eax,%eax
test   %ecx,%ecx
sete   %al
jmp    11ce <RunProcess+0x8e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @RunProcess(i8* %0, i8* %1, i32 %2, i32* %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
store i32* %4, i32** %10
store i32 0, i32* %11
%16 = load i32*, i32** %10
store i32 0, i32* %16
%17 = load i8*, i8** %6
%18 = call i32* @popen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %12
%19 = load i32*, i32** %12
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %56
21: ; preds = %5
store i32 0, i32* %14
%22 = load i32*, i32** %12
%23 = call i32 @fgetc(i32* %22)
store i32 %23, i32* %13
br label %24
24: ; preds = %33, %21
%25 = load i32, i32* %13
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i32, i32* %14
%29 = load i32, i32* %8
%30 = icmp slt i32 %28, %29
br label %31
31: ; preds = %27, %24
%32 = phi i1 [ false, %24 ], [ %30, %27 ]
br i1 %32, label %33, label %43
33: ; preds = %31
%34 = load i32, i32* %13
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %7
%37 = load i32, i32* %14
%38 = add nsw i32 %37, 1
store i32 %38, i32* %14
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 %35, i8* %40
%41 = load i32*, i32** %12
%42 = call i32 @fgetc(i32* %41)
store i32 %42, i32* %13
br label %24
43: ; preds = %31
%44 = load i32, i32* %14
%45 = load i32*, i32** %9
store i32 %44, i32* %45
%46 = load i32*, i32** %12
%47 = call i32 @pclose(i32* %46)
store i32 %47, i32* %15
%48 = load i32, i32* %15
%49 = call i32 @WEXITSTATUS(i32 %48)
%50 = load i32*, i32** %10
store i32 %49, i32* %50
%51 = load i32*, i32** %10
%52 = load i32, i32* %51
%53 = icmp ne i32 %52, 0
br i1 %53, label %55, label %54
54: ; preds = %43
store i32 1, i32* %11
br label %55
55: ; preds = %54, %43
br label %56
56: ; preds = %55, %5
%57 = load i32, i32* %11
ret i32 %57
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @RunProcess(i8* nocapture %0, i8* nocapture writeonly %1, i32 %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4) {
store i32 0, i32* %4
%6 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %33, label %8
8: ; preds = %5
%9 = tail call i32 @fgetc(i32* %6)
%10 = icmp sgt i32 %9, -1
%11 = icmp sgt i32 %2, 0
%12 = and i1 %10, %11
br i1 %12, label %13, label %27
13: ; preds = %8
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %19, %15 ]
%17 = phi i32 [ %9, %13 ], [ %21, %15 ]
%18 = trunc i32 %17 to i8
%19 = add nuw nsw i64 %16, 1
%20 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 %18, i8* %20
%21 = tail call i32 @fgetc(i32* %6)
%22 = icmp sgt i32 %21, -1
%23 = icmp ult i64 %19, %14
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %15, label %25
25: ; preds = %15
%26 = trunc i64 %19 to i32
br label %27
27: ; preds = %25, %8
%28 = phi i32 [ 0, %8 ], [ %26, %25 ]
store i32 %28, i32* %3
%29 = tail call i32 @pclose(i32* %6)
%30 = tail call i32 @WEXITSTATUS(i32 %29)
store i32 %30, i32* %4
%31 = icmp eq i32 %30, 0
%32 = zext i1 %31 to i32
br label %33
33: ; preds = %27, %5
%34 = phi i32 [ %32, %27 ], [ 0, %5 ]
ret i32 %34
}