#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ read32(int, int, int *);
scalar_t__ readcons(int, int, int *, int *);
char *readcstr(int, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char *);
uint32_t name_lookup(int dbfd, const char *name) {
  uint32_t namehead;
  if (read32(dbfd, 4, &namehead)) {
    fprintf(stderr, "persist: name_lookup: failed to read namehead at 0x4\n");
    return (0);
  }
  uint32_t nameptr = namehead, namecar, namecdr;
  while (nameptr) {
    if (readcons(dbfd, nameptr, &namecar, &namecdr)) {
      fprintf(stderr,
              "persist: name_lookup: failed to read namelist cons at 0x%08x\n",
              nameptr);
      return (0);
    }
    uint32_t namecaar, namecdar;
    if (readcons(dbfd, namecar, &namecaar, &namecdar)) {
      fprintf(stderr,
              "persist: name_lookup: failed to read namelist entry at 0x%08x\n",
              namecar);
      return (0);
    }
    char *buf = readcstr(dbfd, namecaar);
    if (strcmp(name, buf) == 0) {
      free(buf);
      return (namecar);
    }
    free(buf);
    nameptr = namecdr;
  }
  return (0);
}


x86 assembly (clang, -O0):
<name_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x4,%esi
lea    -0x14(%rbp),%rdx
call   1080 <read32@plt>
cmp    $0x0,%rax
je     11ad <name_lookup+0x4d>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1298 <name_lookup+0x138>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1291 <name_lookup+0x131>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1070 <readcons@plt>
cmp    $0x0,%rax
je     1200 <name_lookup+0xa0>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1298 <name_lookup+0x138>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1070 <readcons@plt>
cmp    $0x0,%rax
je     1243 <name_lookup+0xe3>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1298 <name_lookup+0x138>
mov    -0x8(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1040 <readcstr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    127d <name_lookup+0x11d>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1298 <name_lookup+0x138>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11b3 <name_lookup+0x53>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name_lookup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %edi,%r12d
lea    0x10(%rsp),%rdx
mov    $0x4,%esi
call   1080 <read32@plt>
test   %rax,%rax
je     11b4 <name_lookup+0x54>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%r15d
xor    %r14d,%r14d
test   %r15d,%r15d
je     11a2 <name_lookup+0x42>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r15d,%esi
lea    0x4(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1070 <readcons@plt>
test   %rax,%rax
jne    123e <name_lookup+0xde>
mov    0x4(%rsp),%esi
mov    %r12d,%edi
lea    0x8(%rsp),%rdx
lea    0x14(%rsp),%rcx
call   1070 <readcons@plt>
test   %rax,%rax
jne    1256 <name_lookup+0xf6>
mov    0x8(%rsp),%esi
mov    %r12d,%edi
call   1040 <readcstr@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    %rax,%rsi
call   1050 <strcmp@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
test   %rbp,%rbp
je     1279 <name_lookup+0x119>
mov    0xc(%rsp),%r15d
test   %r15d,%r15d
jne    11d0 <name_lookup+0x70>
jmp    11a2 <name_lookup+0x42>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
xor    %r14d,%r14d
mov    %r15d,%edx
jmp    126d <name_lookup+0x10d>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    0x4(%rsp),%edx
lea    0xe0a(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11a2 <name_lookup+0x42>
mov    0x4(%rsp),%r14d
jmp    11a2 <name_lookup+0x42>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [54 x i8] c"persist: name_lookup: failed to read namehead at 0x4\0A\00"
@.str.1 = [62 x i8] c"persist: name_lookup: failed to read namelist cons at 0x%08x\0A\00"
@.str.2 = [63 x i8] c"persist: name_lookup: failed to read namelist entry at 0x%08x\0A\00"
define i32 @name_lookup(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%13 = load i32, i32* %4
%14 = call i64 @read32(i32 %13, i32 4, i32* %6)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %59
19: ; preds = %2
%20 = load i32, i32* %6
store i32 %20, i32* %7
br label %21
21: ; preds = %54, %19
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %58
24: ; preds = %21
%25 = load i32, i32* %4
%26 = load i32, i32* %7
%27 = call i64 @readcons(i32 %25, i32 %26, i32* %8, i32* %9)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i32, i32* %7
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 0, i32* %3
br label %59
33: ; preds = %24
%34 = load i32, i32* %4
%35 = load i32, i32* %8
%36 = call i64 @readcons(i32 %34, i32 %35, i32* %10, i32* %11)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = load i32, i32* %8
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i32 %40)
store i32 0, i32* %3
br label %59
42: ; preds = %33
%43 = load i32, i32* %4
%44 = load i32, i32* %10
%45 = call i8* @readcstr(i32 %43, i32 %44)
store i8* %45, i8** %12
%46 = load i8*, i8** %5
%47 = load i8*, i8** %12
%48 = call i64 @strcmp(i8* %46, i8* %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %42
%51 = load i8*, i8** %12
%52 = call i32 @free(i8* %51)
%53 = load i32, i32* %8
store i32 %53, i32* %3
br label %59
54: ; preds = %42
%55 = load i8*, i8** %12
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %9
store i32 %57, i32* %7
br label %21
58: ; preds = %21
store i32 0, i32* %3
br label %59
59: ; preds = %58, %50, %38, %29, %16
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [54 x i8] c"persist: name_lookup: failed to read namehead at 0x4\0A\00"
@.str.1 = [62 x i8] c"persist: name_lookup: failed to read namelist cons at 0x%08x\0A\00"
@.str.2 = [63 x i8] c"persist: name_lookup: failed to read namelist entry at 0x%08x\0A\00"
define i32 @name_lookup(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %3 to i8*
%9 = call i64 @read32(i32 %0, i32 4, i32* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
br label %52
14: ; preds = %2
%15 = load i32, i32* %3
%16 = bitcast i32* %4 to i8*
%17 = bitcast i32* %5 to i8*
%18 = icmp eq i32 %15, 0
br i1 %18, label %50, label %19
19: ; preds = %14
%20 = bitcast i32* %6 to i8*
%21 = bitcast i32* %7 to i8*
br label %22
22: ; preds = %19, %47
%23 = phi i32 [ %15, %19 ], [ %48, %47 ]
%24 = call i64 @readcons(i32 %0, i32 %23, i32* %4, i32* %5)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %50
29: ; preds = %22
%30 = load i32, i32* %4
%31 = call i64 @readcons(i32 %0, i32 %30, i32* %6, i32* %7)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i32, i32* %4
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %45
37: ; preds = %29
%38 = load i32, i32* %6
%39 = call i8* @readcstr(i32 %0, i32 %38)
%40 = call i64 @strcmp(i8* %1, i8* %39)
%41 = icmp eq i64 %40, 0
%42 = call i32 @free(i8* %39)
br i1 %41, label %43, label %47
43: ; preds = %37
%44 = load i32, i32* %4
br label %45
45: ; preds = %33, %43
%46 = phi i32 [ %44, %43 ], [ 0, %33 ]
br label %50
47: ; preds = %37
%48 = load i32, i32* %5
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %22
50: ; preds = %47, %14, %45, %26
%51 = phi i32 [ 0, %26 ], [ %46, %45 ], [ 0, %14 ], [ 0, %47 ]
br label %52
52: ; preds = %50, %11
%53 = phi i32 [ 0, %11 ], [ %51, %50 ]
ret i32 %53
}