#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int parse_typtop_output(const char *cmd) {
  setuid(0);
  char buf[128];
  FILE *fp;
  if ((fp = popen(cmd, "r")) == NULL) {
    printf("Error opening pipe!\n");
    return -1;
  }
  int failed = 1;
  fscanf(fp, "%d\n", &failed);
  printf("ret = %d\n", failed);
  while (fgets(buf, 128, fp) != NULL) {
    printf("OUTPUT: %s", buf);
  }
  if (pclose(fp)) {
    printf("Command not found or exited with error status\n");
    return -1;
  }
  return failed;
}


x86 assembly (clang, -O0):
<parse_typtop_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
xor    %edi,%edi
call   1080 <setuid@plt>
mov    -0x10(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x98(%rbp)
cmp    $0x0,%rax
jne    11b1 <parse_typtop_output+0x51>
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <parse_typtop_output+0xfe>
movl   $0x1,-0x9c(%rbp)
mov    -0x98(%rbp),%rdi
lea    0xe54(%rip),%rsi
lea    -0x9c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x9c(%rbp),%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1225 <parse_typtop_output+0xc5>
lea    -0x90(%rbp),%rsi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e9 <parse_typtop_output+0x89>
mov    -0x98(%rbp),%rdi
call   1040 <pclose@plt>
cmp    $0x0,%rax
je     1255 <parse_typtop_output+0xf5>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <parse_typtop_output+0xfe>
mov    -0x9c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_typtop_output>:
push   %r15
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %rdi,%rbx
xor    %edi,%edi
call   1090 <setuid@plt>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
call   1080 <popen@plt>
test   %rax,%rax
je     1238 <parse_typtop_output+0xc8>
mov    %rax,%r15
movl   $0x1,0xc(%rsp)
lea    0xe58(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    0xc(%rsp),%esi
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1222 <parse_typtop_output+0xb2>
lea    0xe1f(%rip),%r14
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1200 <parse_typtop_output+0x90>
mov    %r15,%rdi
call   1050 <pclose@plt>
test   %rax,%rax
je     124b <parse_typtop_output+0xdb>
lea    0xde1(%rip),%rdi
jmp    123f <parse_typtop_output+0xcf>
lea    0xe06(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    124f <parse_typtop_output+0xdf>
mov    0xc(%rsp),%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [21 x i8] c"Error opening pipe!\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [10 x i8] c"ret = %d\0A\00"
@.str.4 = [11 x i8] c"OUTPUT: %s\00"
@.str.5 = [47 x i8] c"Command not found or exited with error status\0A\00"
define i32 @parse_typtop_output(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [128 x i8]
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i32 @setuid(i32 0)
%8 = load i8*, i8** %3
%9 = call i32* @popen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %34
13: ; preds = %1
store i32 1, i32* %6
%14 = load i32*, i32** %5
%15 = call i32 @fscanf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%16 = load i32, i32* %6
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %23, %13
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%20 = load i32*, i32** %5
%21 = call i32* @fgets(i8* %19, i32 128, i32* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %24)
br label %18
26: ; preds = %18
%27 = load i32*, i32** %5
%28 = call i64 @pclose(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %2
br label %34
32: ; preds = %26
%33 = load i32, i32* %6
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %30, %11
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [10 x i8] c"ret = %d\0A\00"
@.str.4 = [11 x i8] c"OUTPUT: %s\00"
@str = [46 x i8] c"Command not found or exited with error status\00"
@str.6 = [20 x i8] c"Error opening pipe!\00"
define i32 @parse_typtop_output(i8* nocapture %0) {
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = tail call i32 @setuid(i32 0)
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%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 %8, label %10
8: ; preds = %1
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
br label %30
10: ; preds = %1
%11 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%12 = call i32 @fscanf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%13 = load i32, i32* %3
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = call i32* @fgets(i8* %5, i32 128, i32* %6)
%16 = icmp eq i32* %15, null
br i1 %16, label %21, label %17
17: ; preds = %10, %17
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%19 = call i32* @fgets(i8* %5, i32 128, i32* %6)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %17
21: ; preds = %17, %10
%22 = call i64 @pclose(i32* %6)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %28
26: ; preds = %21
%27 = load i32, i32* %3
br label %28
28: ; preds = %26, %24
%29 = phi i32 [ -1, %24 ], [ %27, %26 ]
br label %30
30: ; preds = %28, %8
%31 = phi i32 [ -1, %8 ], [ %29, %28 ]
ret i32 %31
}