#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*/ memset(char *, int, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
char *strstr(char *, char *);
int main() {
  int count = 0;
  char Buffer[256] = {0x00}, *ptr = NULL;
  char *servicename = "cipherserver";
  FILE *fp;
  fp = popen(" ps -e | grep nginx", "r");
  memset(Buffer, 0x00, sizeof(256));
  fgets(Buffer, 256, fp);
  pclose(fp);
  printf("%s\n", Buffer);
  if (Buffer[0] != '\0') {
    perror("error");
    return -1;
  }
  count = readlink("/proc/self/exe", Buffer, 256);
  ptr = strstr(Buffer, "nginx");
  memset(ptr, 0x00, 256 - (ptr - Buffer) - 1);
  strcpy(ptr, servicename);
  printf("%s\n", ptr);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
movq   $0x0,-0x118(%rbp)
lea    0xe32(%rip),%rax
mov    %rax,-0x120(%rbp)
lea    0xe31(%rip),%rdi
lea    0xe28(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x128(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x128(%rbp),%rdx
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    -0x128(%rbp),%rdi
call   1060 <pclose@plt>
lea    -0x110(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
movsbl -0x110(%rbp),%eax
cmp    $0x0,%eax
je     1261 <main+0xd1>
lea    0xdd5(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <main+0x159>
lea    -0x110(%rbp),%rsi
lea    0xdbc(%rip),%rdi
mov    $0x100,%edx
call   1040 <readlink@plt>
mov    %eax,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xd91(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rdi
mov    -0x118(%rbp),%rcx
lea    -0x110(%rbp),%rax
sub    %rax,%rcx
mov    $0x100,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x118(%rbp),%rdi
mov    -0x120(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x118(%rbp),%rsi
lea    0xd3d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe01(%rip),%rdi
lea    0xdf8(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,%rbx
mov    %rsp,%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1060 <pclose@plt>
lea    0xdd2(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
cmpb   $0x0,(%rsp)
jne    12c7 <main+0x137>
lea    0xdc7(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
mov    $0x100,%edx
call   1040 <readlink@plt>
lea    0xda0(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strstr@plt>
mov    %rax,%rbp
sub    %ebp,%ebx
add    $0xff,%ebx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <memset@plt>
lea    0xd5f(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcpy@plt>
lea    0xd71(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd57(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    12b8 <main+0x128>

LLVM IR (-O0):
@.str = [13 x i8] c"cipherserver\00"
@.str.1 = [20 x i8] c" ps -e | grep nginx\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [6 x i8] c"error\00"
@.str.5 = [15 x i8] c"/proc/self/exe\00"
@.str.6 = [6 x i8] c"nginx\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
%7 = bitcast [256 x i8]* %3 to i8*
store i8* null, i8** %4
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %5
%8 = call i32* @popen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %8, i32** %6
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 4)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = load i32*, i32** %6
%13 = call i32 @fgets(i8* %11, i32 256, i32* %12)
%14 = load i32*, i32** %6
%15 = call i32 @pclose(i32* %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
24: ; preds = %0
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%26 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %25, i32 256)
store i32 %26, i32* %2
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%28 = call i8* @strstr(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = load i8*, i8** %4
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%32 = ptrtoint i8* %30 to i64
%33 = ptrtoint i8* %31 to i64
%34 = sub i64 %32, %33
%35 = sub nsw i64 256, %34
%36 = sub nsw i64 %35, 1
%37 = trunc i64 %36 to i32
%38 = call i32 @memset(i8* %29, i32 0, i32 %37)
%39 = load i8*, i8** %4
%40 = load i8*, i8** %5
%41 = call i32 @strcpy(i8* %39, i8* %40)
%42 = load i8*, i8** %4
%43 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %24, %22
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [13 x i8] c"cipherserver\00"
@.str.1 = [20 x i8] c" ps -e | grep nginx\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [6 x i8] c"error\00"
@.str.5 = [15 x i8] c"/proc/self/exe\00"
@.str.6 = [6 x i8] c"nginx\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @popen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%4 = call i32 @memset(i8* %2, i32 0, i32 4)
%5 = call i32 @fgets(i8* %2, i32 256, i32* %3)
%6 = call i32 @pclose(i32* %3)
%7 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%8 = load i8, i8* %2
%9 = icmp eq i8 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %23
12: ; preds = %0
%13 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %2, i32 256)
%14 = call i8* @strstr(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%15 = ptrtoint i8* %14 to i64
%16 = ptrtoint [256 x i8]* %1 to i64
%17 = sub i64 %16, %15
%18 = trunc i64 %17 to i32
%19 = add i32 %18, 255
%20 = call i32 @memset(i8* %14, i32 0, i32 %19)
%21 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %23
23: ; preds = %12, %10
%24 = phi i32 [ -1, %10 ], [ 0, %12 ]
ret i32 %24
}