#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ *strstr(char *, char *);
int has_libc_start_main(char *program) {
  char *cmd = malloc(sizeof(char) * (strlen(program) + 20));
  sprintf(cmd, "nm -D %s", program);
  FILE *fp = popen(cmd, "r");
  if (fp == NULL) {
    printf("cannot analyze program: %s\n", program);
    exit(EXIT_FAILURE);
  }
  char line[2000];
  int found = 0;
  while (fgets(line, 2000, fp)) {
    if (strstr(line, ("__libc_start_main")) != NULL) {
      found = 1;
    }
  }
  pclose(fp);
  return found;
}


x86 assembly (clang, -O0):
<has_libc_start_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x800,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x14,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe35(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120d <has_libc_start_main+0x7d>
mov    -0x8(%rbp),%rsi
lea    0xe11(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x0,-0x7f4(%rbp)
lea    -0x7f0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x7d0,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1262 <has_libc_start_main+0xd2>
lea    -0x7f0(%rbp),%rdi
lea    0xde3(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     125d <has_libc_start_main+0xcd>
movl   $0x1,-0x7f4(%rbp)
jmp    1217 <has_libc_start_main+0x87>
mov    -0x18(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x7f4(%rbp),%eax
add    $0x800,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<has_libc_start_main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x7d0,%rsp
mov    %rdi,%rbp
call   1030 <strlen@plt>
lea    0x14(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0xe47(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
call   1090 <sprintf@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
call   1080 <popen@plt>
test   %rax,%rax
je     1251 <has_libc_start_main+0xc1>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x7d0,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1237 <has_libc_start_main+0xa7>
lea    0xe2e(%rip),%r14
mov    %rsp,%rbx
mov    $0x1,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
cmovne %r12d,%ebp
mov    %rbx,%rdi
mov    $0x7d0,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    1210 <has_libc_start_main+0x80>
mov    %r15,%rdi
call   1050 <pclose@plt>
mov    %ebp,%eax
add    $0x7d0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb3(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"nm -D %s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [28 x i8] c"cannot analyze program: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [18 x i8] c"__libc_start_main\00"
define i32 @has_libc_start_main(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [2000 x i8]
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 20
%10 = sext i32 %9 to i64
%11 = mul i64 1, %10
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = load i8*, i8** %2
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %3
%18 = call i32* @popen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %4
%19 = load i32*, i32** %4
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %1
%22 = load i8*, i8** %2
%23 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %1
store i32 0, i32* %6
br label %27
27: ; preds = %37, %26
%28 = getelementptr inbounds [2000 x i8], [2000 x i8]* %5, i64 0, i64 0
%29 = load i32*, i32** %4
%30 = call i64 @fgets(i8* %28, i32 2000, i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %27
%33 = getelementptr inbounds [2000 x i8], [2000 x i8]* %5, i64 0, i64 0
%34 = call i32* @strstr(i8* %33, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 1, i32* %6
br label %37
37: ; preds = %36, %32
br label %27
38: ; preds = %27
%39 = load i32*, i32** %4
%40 = call i32 @pclose(i32* %39)
%41 = load i32, i32* %6
ret i32 %41
}

LLVM IR (-O2):
@.str = [9 x i8] c"nm -D %s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [28 x i8] c"cannot analyze program: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [18 x i8] c"__libc_start_main\00"
define i32 @has_libc_start_main(i8* %0) {
%2 = alloca [2000 x i8]
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 20
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32 @sprintf(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = tail call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds [2000 x i8], [2000 x i8]* %2, i64 0, i64 0
%15 = call i64 @fgets(i8* %14, i32 2000, i32* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %13, %17
%18 = phi i32 [ %21, %17 ], [ 0, %13 ]
%19 = call i32* @strstr(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i32* %19, null
%21 = select i1 %20, i32 %18, i32 1
%22 = call i64 @fgets(i8* %14, i32 2000, i32* %7)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %17
24: ; preds = %17, %13
%25 = phi i32 [ 0, %13 ], [ %21, %17 ]
%26 = call i32 @pclose(i32* %7)
ret i32 %25
}