#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;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ eeprom_names;
typedef int /*<<< orphan*/ cmd;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char *);
char *strdup(char *);
int strlen(char *);
const char *find_eeprom(const char *path) {
  char *eeprom_path = NULL;
  char eeprom_names[512];
  FILE *fp = NULL;
  char cmd[512];
  if (path == NULL) {
    path = "/sys";
  }
  snprintf(cmd, sizeof(cmd), "find %s -name eeprom 2>/dev/null", path);
  fp = popen(cmd, "r");
  if (fp == NULL) {
    perror("popen");
    return NULL;
  }
  while (fgets(eeprom_names, sizeof(eeprom_names), fp) != NULL) {
    struct stat eeprom_file;
    char *__basename = NULL;
    if (eeprom_names[strlen(eeprom_names) - 1] == '\n')
      eeprom_names[strlen(eeprom_names) - 1] = '\0';
    __basename = strdup(eeprom_names);
    stat(eeprom_names, &eeprom_file);
    if (S_ISREG(eeprom_file.st_mode) &&
        !strcmp(basename(__basename), "eeprom") && eeprom_file.st_size == 256) {
      eeprom_path = strdup(eeprom_names);
      free(__basename);
      break;
    }
    free(__basename);
  }
  pclose(fp);
  return eeprom_path;
}


x86 assembly (clang, -O0):
<find_eeprom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x228(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f8 <find_eeprom+0x38>
lea    0xe0c(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x200,%esi
lea    0xdf6(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x430(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   10c0 <popen@plt>
mov    %rax,-0x228(%rbp)
cmpq   $0x0,-0x228(%rbp)
jne    1255 <find_eeprom+0x95>
lea    0xde5(%rip),%rdi
call   10d0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1376 <find_eeprom+0x1b6>
jmp    125a <find_eeprom+0x9a>
lea    -0x220(%rbp),%rdi
mov    -0x228(%rbp),%rdx
mov    $0x200,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1362 <find_eeprom+0x1a2>
movq   $0x0,-0x440(%rbp)
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    12c2 <find_eeprom+0x102>
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
call   10e0 <strdup@plt>
mov    %rax,-0x440(%rbp)
lea    -0x220(%rbp),%rdi
lea    -0x438(%rbp),%rsi
call   10a0 <stat@plt>
mov    -0x434(%rbp),%edi
call   10b0 <S_ISREG@plt>
cmp    $0x0,%rax
je     1351 <find_eeprom+0x191>
mov    -0x440(%rbp),%rdi
call   1090 <basename@plt>
mov    %eax,%edi
lea    0xd1c(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1351 <find_eeprom+0x191>
cmpl   $0x100,-0x438(%rbp)
jne    1351 <find_eeprom+0x191>
lea    -0x220(%rbp),%rdi
call   10e0 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x440(%rbp),%rdi
call   1030 <free@plt>
jmp    1362 <find_eeprom+0x1a2>
mov    -0x440(%rbp),%rdi
call   1030 <free@plt>
jmp    125a <find_eeprom+0x9a>
mov    -0x228(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_eeprom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
test   %rdi,%rdi
lea    0xe25(%rip),%rcx
cmovne %rdi,%rcx
lea    0xe1f(%rip),%rdx
lea    0x210(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1060 <snprintf@plt>
lea    0xe24(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <popen@plt>
test   %rax,%rax
je     1310 <find_eeprom+0x150>
mov    %rax,%r15
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12f0 <find_eeprom+0x130>
lea    0x8(%rsp),%r12
xor    %r14d,%r14d
lea    0xdeb(%rip),%r13
jmp    1271 <find_eeprom+0xb1>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12f3 <find_eeprom+0x133>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1291 <find_eeprom+0xd1>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   10e0 <strdup@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r12,%rsi
call   10a0 <stat@plt>
mov    0xc(%rsp),%edi
call   10b0 <S_ISREG@plt>
test   %rax,%rax
je     1250 <find_eeprom+0x90>
mov    %rbp,%rdi
call   1090 <basename@plt>
mov    %eax,%edi
mov    %r13,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
jne    1250 <find_eeprom+0x90>
cmpl   $0x100,0x8(%rsp)
jne    1250 <find_eeprom+0x90>
lea    0x10(%rsp),%rdi
call   10e0 <strdup@plt>
mov    %rax,%r14
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    12f3 <find_eeprom+0x133>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1050 <pclose@plt>
mov    %r14,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd11(%rip),%rdi
call   10d0 <perror@plt>
xor    %r14d,%r14d
jmp    12fb <find_eeprom+0x13b>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [5 x i8] c"/sys\00"
@.str.1 = [33 x i8] c"find %s -name eeprom 2>/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"popen\00"
@.str.4 = [7 x i8] c"eeprom\00"
define i8* @find_eeprom(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [512 x i8]
%6 = alloca i32*
%7 = alloca [512 x i8]
%8 = alloca %struct.stat
%9 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %4
store i32* null, i32** %6
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %3
br label %13
13: ; preds = %12, %1
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%15 = load i8*, i8** %3
%16 = call i32 @snprintf(i8* %14, i32 512, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%18 = call i32* @popen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %2
br label %74
23: ; preds = %13
br label %24
24: ; preds = %67, %23
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%26 = load i32*, i32** %6
%27 = call i32* @fgets(i8* %25, i32 512, i32* %26)
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %70
29: ; preds = %24
store i8* null, i8** %9
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %38, label %44
38: ; preds = %29
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %38, %29
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%46 = call i8* @strdup(i8* %45)
store i8* %46, i8** %9
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%48 = call i32 @stat(i8* %47, %struct.stat* %8)
%49 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i64 @S_ISREG(i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %67
53: ; preds = %44
%54 = load i8*, i8** %9
%55 = call i32 @basename(i8* %54)
%56 = call i32 @strcmp(i32 %55, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%57 = icmp ne i32 %56, 0
br i1 %57, label %67, label %58
58: ; preds = %53
%59 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%60 = load i32, i32* %59
%61 = icmp eq i32 %60, 256
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%64 = call i8* @strdup(i8* %63)
store i8* %64, i8** %4
%65 = load i8*, i8** %9
%66 = call i32 @free(i8* %65)
br label %70
67: ; preds = %58, %53, %44
%68 = load i8*, i8** %9
%69 = call i32 @free(i8* %68)
br label %24
70: ; preds = %62, %24
%71 = load i32*, i32** %6
%72 = call i32 @pclose(i32* %71)
%73 = load i8*, i8** %4
store i8* %73, i8** %2
br label %74
74: ; preds = %70, %21
%75 = load i8*, i8** %2
ret i8* %75
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [5 x i8] c"/sys\00"
@.str.1 = [33 x i8] c"find %s -name eeprom 2>/dev/null\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"popen\00"
@.str.4 = [7 x i8] c"eeprom\00"
define noalias i8* @find_eeprom(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = icmp eq i8* %0, null
%8 = select i1 %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0
%9 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %19, label %12
12: ; preds = %1
%13 = call i32* @fgets(i8* %5, i32 512, i32* %10)
%14 = icmp eq i32* %13, null
br i1 %14, label %53, label %15
15: ; preds = %12
%16 = bitcast %struct.stat* %4 to i8*
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %56
21: ; preds = %15, %49
%22 = call i32 @strlen(i8* %5)
%23 = add nsw i32 %22, -1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 10
br i1 %27, label %28, label %33
28: ; preds = %21
%29 = call i32 @strlen(i8* %5)
%30 = add nsw i32 %29, -1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %28, %21
%34 = call i8* @strdup(i8* %5)
%35 = call i32 @stat(i8* %5, %struct.stat* %4)
%36 = load i32, i32* %17
%37 = call i64 @S_ISREG(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %49, label %39
39: ; preds = %33
%40 = call i32 @basename(i8* %34)
%41 = call i32 @strcmp(i32 %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%42 = icmp eq i32 %41, 0
%43 = load i32, i32* %18
%44 = icmp eq i32 %43, 256
%45 = select i1 %42, i1 %44, i1 false
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = call i8* @strdup(i8* %5)
%48 = call i32 @free(i8* %34)
br label %53
49: ; preds = %33, %39
%50 = call i32 @free(i8* %34)
%51 = call i32* @fgets(i8* %5, i32 512, i32* %10)
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %21
53: ; preds = %49, %12, %46
%54 = phi i8* [ %47, %46 ], [ null, %12 ], [ null, %49 ]
%55 = call i32 @pclose(i32* %10)
br label %56
56: ; preds = %53, %19
%57 = phi i8* [ null, %19 ], [ %54, %53 ]
ret i8* %57
}