#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ENOMEM;
int errno;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
scalar_t__ stat(char const *, struct stat *);
int sigar_sudo_file2str(const char *fname, char *buffer, int buflen) {
  FILE *fp;
  struct stat sb;
  if (stat(fname, &sb) < 0) {
    return errno;
  }
  if (sb.st_size > buflen) {
    return ENOMEM;
  }
  snprintf(buffer, buflen, "sudo cat %s", fname);
  if (!(fp = popen(buffer, "r"))) {
    return errno;
  }
  (void)fgets(buffer, buflen, fp);
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<sigar_sudo_file2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    118b <sigar_sudo_file2str+0x3b>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <sigar_sudo_file2str+0xbe>
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    11a8 <sigar_sudo_file2str+0x58>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <sigar_sudo_file2str+0xbe>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xe46(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ee <sigar_sudo_file2str+0x9e>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <sigar_sudo_file2str+0xbe>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
mov    -0x28(%rbp),%rdi
call   1030 <pclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigar_sudo_file2str>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11c2 <sigar_sudo_file2str+0x72>
cmp    %ebp,0x8(%rsp)
jle    117e <sigar_sudo_file2str+0x2e>
mov    0x2e64(%rip),%rax
jmp    11c9 <sigar_sudo_file2str+0x79>
lea    0xe7b(%rip),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rcx
call   1040 <snprintf@plt>
lea    0xe73(%rip),%rsi
mov    %r14,%rdi
call   1070 <popen@plt>
test   %rax,%rax
je     11c2 <sigar_sudo_file2str+0x72>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1030 <pclose@plt>
xor    %eax,%eax
jmp    11cb <sigar_sudo_file2str+0x7b>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@ENOMEM = global i32 0
@.str = [12 x i8] c"sudo cat %s\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @sigar_sudo_file2str(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i64 @stat(i8* %10, %struct.stat* %9)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32, i32* @errno
store i32 %14, i32* %4
br label %39
15: ; preds = %3
%16 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* %7
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = load i32, i32* @ENOMEM
store i32 %21, i32* %4
br label %39
22: ; preds = %15
%23 = load i8*, i8** %6
%24 = load i32, i32* %7
%25 = load i8*, i8** %5
%26 = call i32 @snprintf(i8* %23, i32 %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %6
%28 = call i32* @popen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %8
%29 = icmp ne i32* %28, null
br i1 %29, label %32, label %30
30: ; preds = %22
%31 = load i32, i32* @errno
store i32 %31, i32* %4
br label %39
32: ; preds = %22
%33 = load i8*, i8** %6
%34 = load i32, i32* %7
%35 = load i32*, i32** %8
%36 = call i32 @fgets(i8* %33, i32 %34, i32* %35)
%37 = load i32*, i32** %8
%38 = call i32 @pclose(i32* %37)
store i32 0, i32* %4
br label %39
39: ; preds = %32, %30, %20, %13
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@ENOMEM = global i32 0
@.str = [12 x i8] c"sudo cat %s\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @sigar_sudo_file2str(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = load i32, i32* @errno
br label %25
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = icmp sgt i32 %12, %2
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = load i32, i32* @ENOMEM
br label %25
16: ; preds = %10
%17 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0)
%18 = call i32* @popen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i32, i32* @errno
br label %25
22: ; preds = %16
%23 = call i32 @fgets(i8* %1, i32 %2, i32* %18)
%24 = call i32 @pclose(i32* %18)
br label %25
25: ; preds = %22, %20, %14, %8
%26 = phi i32 [ %9, %8 ], [ %15, %14 ], [ 0, %22 ], [ %21, %20 ]
ret i32 %26
}