#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 record {
  int dummy;
};
struct library {
  int dummy;
};
struct crate {
  int dummy;
};
typedef int pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EXIT_SUCCESS;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ assert(int /*<<< orphan*/);
char *basename(char *);
int /*<<< orphan*/ *crate_add(struct crate *, struct record *);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int fork_pipe(int *, char const *, char *, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int get_record(int /*<<< orphan*/ *, struct record **);
struct record *library_add(struct library *, struct record *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strdupa(char const *);
struct crate *use_crate(struct library *, char *);
int waitpid(int, int *, int /*<<< orphan*/);
int library_import(struct library *li, const char *scan, const char *path) {
  int fd, status;
  char *cratename, *pathname;
  pid_t pid;
  FILE *fp;
  struct crate *crate;
  fprintf(stderr, "Scanning '%s'...\n", path);
  pathname = strdupa(path);
  cratename = basename(pathname);
  assert(cratename != NULL);
  crate = use_crate(li, cratename);
  if (crate == NULL)
    return -1;
  pid = fork_pipe(&fd, scan, "scan", path, NULL);
  if (pid == -1)
    return -1;
  fp = fdopen(fd, "r");
  if (fp == NULL) {
    perror("fdopen");
    abort();
  }
  for (;;) {
    struct record *d;
    if (get_record(fp, &d) == -1)
      return -1;
    if (d == NULL)
      break;
    d = library_add(li, d);
    if (d == NULL)
      return -1;
    if (crate_add(crate, d) == NULL)
      return -1;
  }
  if (fclose(fp) == -1) {
    perror("close");
    abort();
  }
  if (waitpid(pid, &status, 0) == -1) {
    perror("waitpid");
    return -1;
  }
  if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) {
    fputs("Library scan exited reporting failure.\n", stderr);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<library_import>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdc8(%rip),%rsi
call   10b0 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   10e0 <strdupa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   10c0 <basename@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <use_crate@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1291 <library_import+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    1424 <library_import+0x214>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%rdi
lea    0xd6e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1090 <fork_pipe@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    12c7 <library_import+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1424 <library_import+0x214>
mov    -0x24(%rbp),%edi
lea    0xd46(%rip),%rsi
call   10f0 <fdopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12f8 <library_import+0xe8>
lea    0xd2d(%rip),%rdi
call   1120 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
jmp    12fd <library_import+0xed>
mov    -0x48(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   10d0 <get_record@plt>
cmp    $0xffffffff,%eax
jne    131f <library_import+0x10f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1424 <library_import+0x214>
cmpq   $0x0,-0x58(%rbp)
jne    132f <library_import+0x11f>
jmp    137f <library_import+0x16f>
mov    -0x10(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1030 <library_add@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1357 <library_import+0x147>
movl   $0xffffffff,-0x4(%rbp)
jmp    1424 <library_import+0x214>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1050 <crate_add@plt>
cmp    $0x0,%rax
jne    137a <library_import+0x16a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1424 <library_import+0x214>
jmp    12fd <library_import+0xed>
mov    -0x48(%rbp),%rdi
call   1070 <fclose@plt>
cmp    $0xffffffff,%eax
jne    13a4 <library_import+0x194>
lea    0xc88(%rip),%rdi
call   1120 <perror@plt>
mov    $0x0,%al
call   1040 <abort@plt>
mov    -0x3c(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1110 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    13d3 <library_import+0x1c3>
lea    0xc64(%rip),%rdi
call   1120 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1424 <library_import+0x214>
mov    -0x28(%rbp),%edi
call   1130 <WIFEXITED@plt>
cmp    $0x0,%eax
je     13fc <library_import+0x1ec>
mov    -0x28(%rbp),%edi
call   1100 <WEXITSTATUS@plt>
mov    0x2be5(%rip),%rcx
cmp    (%rcx),%rax
je     141d <library_import+0x20d>
mov    0x2bdd(%rip),%rax
mov    (%rax),%esi
lea    0xc22(%rip),%rdi
call   1080 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1424 <library_import+0x214>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<library_import>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
call   10b0 <fprintf@plt>
mov    %rbx,%rdi
call   10e0 <strdupa@plt>
mov    %rax,%rdi
call   10c0 <basename@plt>
mov    %rax,%rbp
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10a0 <assert@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1060 <use_crate@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     1387 <library_import+0x177>
mov    %rax,%rbp
lea    0xd91(%rip),%rdx
lea    0x14(%rsp),%rdi
mov    %r15,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1090 <fork_pipe@plt>
cmp    $0xffffffff,%eax
je     1387 <library_import+0x177>
mov    %eax,%r15d
mov    0x14(%rsp),%edi
lea    0xd6c(%rip),%rsi
call   10f0 <fdopen@plt>
test   %rax,%rax
je     13a7 <library_import+0x197>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   10d0 <get_record@plt>
cmp    $0xffffffff,%eax
je     1381 <library_import+0x171>
lea    0x8(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
je     1322 <library_import+0x112>
mov    %r12,%rdi
call   1030 <library_add@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1381 <library_import+0x171>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1050 <crate_add@plt>
test   %rax,%rax
je     1381 <library_import+0x171>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10d0 <get_record@plt>
cmp    $0xffffffff,%eax
jne    12e0 <library_import+0xd0>
jmp    1381 <library_import+0x171>
mov    %rbx,%rdi
call   1070 <fclose@plt>
cmp    $0xffffffff,%eax
je     13ba <library_import+0x1aa>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   1110 <waitpid@plt>
cmp    $0xffffffff,%eax
mov    0x2c94(%rip),%rbx
je     1399 <library_import+0x189>
mov    0x8(%rsp),%edi
call   1130 <WIFEXITED@plt>
test   %eax,%eax
je     1373 <library_import+0x163>
mov    0x8(%rsp),%edi
call   1100 <WEXITSTATUS@plt>
mov    0x2c6d(%rip),%rcx
xor    %r14d,%r14d
cmp    (%rcx),%rax
je     1387 <library_import+0x177>
mov    (%rbx),%esi
lea    0xcb2(%rip),%rdi
call   1080 <fputs@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc86(%rip),%rdi
call   1120 <perror@plt>
jmp    1387 <library_import+0x177>
lea    0xc6b(%rip),%rdi
call   1120 <perror@plt>
xor    %eax,%eax
call   1040 <abort@plt>
lea    0xc5f(%rip),%rdi
jmp    13ae <library_import+0x19e>

LLVM IR (-O0):
%struct.library = type { i32 }
%struct.crate = type { i32 }
%struct.record = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Scanning '%s'...\0A\00"
@.str.1 = [5 x i8] c"scan\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [7 x i8] c"fdopen\00"
@.str.4 = [6 x i8] c"close\00"
@.str.5 = [8 x i8] c"waitpid\00"
@EXIT_SUCCESS = global i64 0
@.str.6 = [40 x i8] c"Library scan exited reporting failure.\0A\00"
define i32 @library_import(%struct.library* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.library*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca %struct.crate*
%15 = alloca %struct.record*
store %struct.library* %0, %struct.library** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %7
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %7
%20 = call i8* @strdupa(i8* %19)
store i8* %20, i8** %11
%21 = load i8*, i8** %11
%22 = call i8* @basename(i8* %21)
store i8* %22, i8** %10
%23 = load i8*, i8** %10
%24 = icmp ne i8* %23, null
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load %struct.library*, %struct.library** %5
%28 = load i8*, i8** %10
%29 = call %struct.crate* @use_crate(%struct.library* %27, i8* %28)
store %struct.crate* %29, %struct.crate** %14
%30 = load %struct.crate*, %struct.crate** %14
%31 = icmp eq %struct.crate* %30, null
br i1 %31, label %32, label %33
32: ; preds = %3
store i32 -1, i32* %4
br label %98
33: ; preds = %3
%34 = load i8*, i8** %6
%35 = load i8*, i8** %7
%36 = call i32 @fork_pipe(i32* %8, i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %35, i32* null)
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 -1, i32* %4
br label %98
40: ; preds = %33
%41 = load i32, i32* %8
%42 = call i32* @fdopen(i32 %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %42, i32** %13
%43 = load i32*, i32** %13
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 (...) @abort()
unreachable
48: ; preds = %40
br label %49
49: ; preds = %71, %48
%50 = load i32*, i32** %13
%51 = call i32 @get_record(i32* %50, %struct.record** %15)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 -1, i32* %4
br label %98
54: ; preds = %49
%55 = load %struct.record*, %struct.record** %15
%56 = icmp eq %struct.record* %55, null
br i1 %56, label %57, label %58
57: ; preds = %54
br label %72
58: ; preds = %54
%59 = load %struct.library*, %struct.library** %5
%60 = load %struct.record*, %struct.record** %15
%61 = call %struct.record* @library_add(%struct.library* %59, %struct.record* %60)
store %struct.record* %61, %struct.record** %15
%62 = load %struct.record*, %struct.record** %15
%63 = icmp eq %struct.record* %62, null
br i1 %63, label %64, label %65
64: ; preds = %58
store i32 -1, i32* %4
br label %98
65: ; preds = %58
%66 = load %struct.crate*, %struct.crate** %14
%67 = load %struct.record*, %struct.record** %15
%68 = call i32* @crate_add(%struct.crate* %66, %struct.record* %67)
%69 = icmp eq i32* %68, null
br i1 %69, label %70, label %71
70: ; preds = %65
store i32 -1, i32* %4
br label %98
71: ; preds = %65
br label %49
72: ; preds = %57
%73 = load i32*, i32** %13
%74 = call i32 @fclose(i32* %73)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %79
76: ; preds = %72
%77 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%78 = call i32 (...) @abort()
unreachable
79: ; preds = %72
%80 = load i32, i32* %12
%81 = call i32 @waitpid(i32 %80, i32* %9, i32 0)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %85
83: ; preds = %79
%84 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %4
br label %98
85: ; preds = %79
%86 = load i32, i32* %9
%87 = call i32 @WIFEXITED(i32 %86)
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %94
89: ; preds = %85
%90 = load i32, i32* %9
%91 = call i64 @WEXITSTATUS(i32 %90)
%92 = load i64, i64* @EXIT_SUCCESS
%93 = icmp ne i64 %91, %92
br i1 %93, label %94, label %97
94: ; preds = %89, %85
%95 = load i32, i32* @stderr
%96 = call i32 @fputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %95)
store i32 -1, i32* %4
br label %98
97: ; preds = %89
store i32 0, i32* %4
br label %98
98: ; preds = %97, %94, %83, %70, %64, %53, %39, %32
%99 = load i32, i32* %4
ret i32 %99
}

LLVM IR (-O2):
%struct.library = type { i32 }
%struct.record = type { i32 }
%struct.crate = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Scanning '%s'...\0A\00"
@.str.1 = [5 x i8] c"scan\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [7 x i8] c"fdopen\00"
@.str.4 = [6 x i8] c"close\00"
@.str.5 = [8 x i8] c"waitpid\00"
@EXIT_SUCCESS = global i64 0
@.str.6 = [40 x i8] c"Library scan exited reporting failure.\0A\00"
define i32 @library_import(%struct.library* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.record*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %2)
%11 = tail call i8* @strdupa(i8* %2)
%12 = tail call i8* @basename(i8* %11)
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call %struct.crate* @use_crate(%struct.library* %0, i8* %12)
%17 = icmp eq %struct.crate* %16, null
br i1 %17, label %68, label %18
18: ; preds = %3
%19 = call i32 @fork_pipe(i32* %4, i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %2, i32* null)
%20 = icmp eq i32 %19, -1
br i1 %20, label %68, label %21
21: ; preds = %18
%22 = load i32, i32* %4
%23 = call i32* @fdopen(i32 %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = bitcast %struct.record** %6 to i8*
%27 = call i32 @get_record(i32* %23, %struct.record** %6)
%28 = icmp eq i32 %27, -1
br i1 %28, label %41, label %32
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %25, %42
%33 = load %struct.record*, %struct.record** %6
%34 = icmp eq %struct.record* %33, null
br i1 %34, label %45, label %35
35: ; preds = %32
%36 = call %struct.record* @library_add(%struct.library* %0, %struct.record* %33)
store %struct.record* %36, %struct.record** %6
%37 = icmp eq %struct.record* %36, null
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = call i32* @crate_add(%struct.crate* %16, %struct.record* %36)
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %42
41: ; preds = %38, %35, %42, %25
br label %68
42: ; preds = %38
%43 = call i32 @get_record(i32* %23, %struct.record** %6)
%44 = icmp eq i32 %43, -1
br i1 %44, label %41, label %32
45: ; preds = %32
%46 = call i32 @fclose(i32* %23)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 (...) @abort()
unreachable
51: ; preds = %45
%52 = call i32 @waitpid(i32 %19, i32* %5, i32 0)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %68
56: ; preds = %51
%57 = load i32, i32* %5
%58 = call i32 @WIFEXITED(i32 %57)
%59 = icmp eq i32 %58, 0
br i1 %59, label %65, label %60
60: ; preds = %56
%61 = load i32, i32* %5
%62 = call i64 @WEXITSTATUS(i32 %61)
%63 = load i64, i64* @EXIT_SUCCESS
%64 = icmp eq i64 %62, %63
br i1 %64, label %68, label %65
65: ; preds = %60, %56
%66 = load i32, i32* @stderr
%67 = call i32 @fputs(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %66)
br label %68
68: ; preds = %41, %60, %18, %3, %65, %54
%69 = phi i32 [ -1, %54 ], [ -1, %65 ], [ -1, %3 ], [ -1, %18 ], [ 0, %60 ], [ -1, %41 ]
ret i32 %69
}