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

/* Variables and functions */
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
char *strchr(char *, char);
char *strdup(char *);
char *secure_config_get_passphrase_from_command(const char *command) {
  FILE *file;
  char *passphrase, *pos, buffer[4096 + 1];
  size_t num_read;
  passphrase = NULL;
  file = popen(command, "r");
  if (!file)
    return NULL;
  num_read = fread(buffer, 1, sizeof(buffer) - 1, file);
  if (num_read > 0) {
    buffer[num_read] = '\0';
    pos = strchr(buffer, '\r');
    if (pos)
      pos[0] = '\0';
    pos = strchr(buffer, '\n');
    if (pos)
      pos[0] = '\0';
    if (buffer[0])
      passphrase = strdup(buffer);
  }
  pclose(file);
  return passphrase;
}


x86 assembly (clang, -O0):
<secure_config_get_passphrase_from_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1193 <secure_config_get_passphrase_from_command+0x43>
movq   $0x0,-0x8(%rbp)
jmp    1252 <secure_config_get_passphrase_from_command+0x102>
lea    -0x1030(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jbe    1241 <secure_config_get_passphrase_from_command+0xf1>
mov    -0x1038(%rbp),%rax
movb   $0x0,-0x1030(%rbp,%rax,1)
lea    -0x1030(%rbp),%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f8 <secure_config_get_passphrase_from_command+0xa8>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1030(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     121f <secure_config_get_passphrase_from_command+0xcf>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
cmpb   $0x0,-0x1030(%rbp)
je     123c <secure_config_get_passphrase_from_command+0xec>
lea    -0x1030(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
jmp    1241 <secure_config_get_passphrase_from_command+0xf1>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secure_config_get_passphrase_from_command>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
lea    0xe9f(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     11c9 <secure_config_get_passphrase_from_command+0x79>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11cd <secure_config_get_passphrase_from_command+0x7d>
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11a1 <secure_config_get_passphrase_from_command+0x51>
movb   $0x0,(%rax)
mov    %rsp,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11b6 <secure_config_get_passphrase_from_command+0x66>
movb   $0x0,(%rax)
cmpb   $0x0,(%rsp)
je     11cd <secure_config_get_passphrase_from_command+0x7d>
mov    %rsp,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
jmp    11cf <secure_config_get_passphrase_from_command+0x7f>
xor    %ebx,%ebx
jmp    11d7 <secure_config_get_passphrase_from_command+0x87>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <pclose@plt>
mov    %rbx,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @secure_config_get_passphrase_from_command(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [4097 x i8]
%8 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %5
%9 = load i8*, i8** %3
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i8* null, i8** %2
br label %50
14: ; preds = %1
%15 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%16 = load i32*, i32** %4
%17 = call i64 @fread(i8* %15, i32 1, i32 4096, i32* %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp ugt i64 %18, 0
br i1 %19, label %20, label %46
20: ; preds = %14
%21 = load i64, i64* %8
%22 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%24 = call i8* @strchr(i8* %23, i8 signext 13)
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i64 0
store i8 0, i8* %29
br label %30
30: ; preds = %27, %20
%31 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%32 = call i8* @strchr(i8* %31, i8 signext 10)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i8*, i8** %6
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 0, i8* %37
br label %38
38: ; preds = %35, %30
%39 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%40 = load i8, i8* %39
%41 = icmp ne i8 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = getelementptr inbounds [4097 x i8], [4097 x i8]* %7, i64 0, i64 0
%44 = call i8* @strdup(i8* %43)
store i8* %44, i8** %5
br label %45
45: ; preds = %42, %38
br label %46
46: ; preds = %45, %14
%47 = load i32*, i32** %4
%48 = call i32 @pclose(i32* %47)
%49 = load i8*, i8** %5
store i8* %49, i8** %2
br label %50
50: ; preds = %46, %13
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define noalias i8* @secure_config_get_passphrase_from_command(i8* nocapture %0) {
%2 = alloca [4097 x i8]
%3 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %26, label %6
6: ; preds = %1
%7 = call i64 @fread(i8* %3, i32 1, i32 4096, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %6
%10 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 %7
store i8 0, i8* %10
%11 = call i8* @strchr(i8* %3, i8 signext 13)
%12 = icmp eq i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %9
store i8 0, i8* %11
br label %14
14: ; preds = %13, %9
%15 = call i8* @strchr(i8* %3, i8 signext 10)
%16 = icmp eq i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %14
store i8 0, i8* %15
br label %18
18: ; preds = %17, %14
%19 = load i8, i8* %3
%20 = icmp eq i8 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = call i8* @strdup(i8* %3)
br label %23
23: ; preds = %18, %21, %6
%24 = phi i8* [ %22, %21 ], [ null, %18 ], [ null, %6 ]
%25 = call i32 @pclose(i32* %4)
br label %26
26: ; preds = %1, %23
%27 = phi i8* [ %24, %23 ], [ null, %1 ]
ret i8* %27
}