#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*/ uint8_t;
typedef scalar_t__ uint32_t;
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ log_fatal(char *, char *, ...);
int /*<<< orphan*/ pbm_set(int /*<<< orphan*/ **, int /*<<< orphan*/);
char *strchr(char *, char);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
uint32_t pbm_load_from_file(uint8_t **b, char *file) {
  if (!b) {
    log_fatal("pbm", "load_from_file called with NULL PBM");
  }
  if (!file) {
    log_fatal("pbm", "load_from_file called with NULL filename");
  }
  FILE *fp = fopen(file, "r");
  if (fp == NULL) {
    log_fatal("pbm", "unable to open file: %s: %s", file, strerror(errno));
  }
  char line[1000];
  uint32_t count = 0;
  while (fgets(line, sizeof(line), fp)) {
    char *comment = strchr(line, '#');
    if (comment) {
      *comment = '\0';
    }
    struct in_addr addr;
    if (inet_aton(line, &addr) != 1) {
      log_fatal("pbm", "unable to parse IP address: %s", line);
    }
    pbm_set(b, addr.s_addr);
    ++count;
  }
  fclose(fp);
  return count;
}


x86 assembly (clang, -O0):
<pbm_load_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b3 <pbm_load_from_file+0x33>
lea    0xe5b(%rip),%rdi
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1090 <log_fatal@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11d3 <pbm_load_from_file+0x53>
lea    0xe3b(%rip),%rdi
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1090 <log_fatal@plt>
mov    -0x10(%rbp),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1229 <pbm_load_from_file+0xa9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x420(%rbp),%rdx
mov    %eax,%ecx
lea    0xde5(%rip),%rdi
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1090 <log_fatal@plt>
movq   $0x0,-0x408(%rbp)
lea    -0x400(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3e8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12e1 <pbm_load_from_file+0x161>
lea    -0x400(%rbp),%rdi
mov    $0x23,%esi
call   1050 <strchr@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
je     1283 <pbm_load_from_file+0x103>
mov    -0x410(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x400(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x1,%eax
je     12bb <pbm_load_from_file+0x13b>
lea    -0x400(%rbp),%rdx
lea    0xd53(%rip),%rdi
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1090 <log_fatal@plt>
mov    -0x8(%rbp),%rdi
mov    -0x418(%rbp),%esi
call   1030 <pbm_set@plt>
mov    -0x408(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x408(%rbp)
jmp    1234 <pbm_load_from_file+0xb4>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x408(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pbm_load_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     1280 <pbm_load_from_file+0x100>
test   %rbx,%rbx
je     129e <pbm_load_from_file+0x11e>
lea    0xea1(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11e8 <pbm_load_from_file+0x68>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe2b(%rip),%rdi
lea    0xe77(%rip),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <log_fatal@plt>
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12b8 <pbm_load_from_file+0x138>
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r13
lea    0xe5b(%rip),%rbp
xor    %ebx,%ebx
jmp    1245 <pbm_load_from_file+0xc5>
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%esi
mov    %r14,%rdi
call   1030 <pbm_set@plt>
add    $0x1,%rbx
mov    %r12,%rdi
mov    $0x3e8,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12ba <pbm_load_from_file+0x13a>
mov    %r12,%rdi
mov    $0x23,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     125a <pbm_load_from_file+0xda>
movb   $0x0,(%rax)
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <inet_aton@plt>
cmp    $0x1,%eax
je     1220 <pbm_load_from_file+0xa0>
lea    0xd8f(%rip),%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <log_fatal@plt>
jmp    1220 <pbm_load_from_file+0xa0>
lea    0xd79(%rip),%rdi
lea    0xd76(%rip),%rsi
xor    %eax,%eax
call   1090 <log_fatal@plt>
test   %rbx,%rbx
jne    11a9 <pbm_load_from_file+0x29>
lea    0xd5b(%rip),%rdi
lea    0xd7c(%rip),%rsi
xor    %eax,%eax
call   1090 <log_fatal@plt>
jmp    11a9 <pbm_load_from_file+0x29>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@.str = [4 x i8] c"pbm\00"
@.str.1 = [36 x i8] c"load_from_file called with NULL PBM\00"
@.str.2 = [41 x i8] c"load_from_file called with NULL filename\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [28 x i8] c"unable to open file: %s: %s\00"
@errno = global i32 0
@.str.5 = [31 x i8] c"unable to parse IP address: %s\00"
define i64 @pbm_load_from_file(i32** %0, i8* %1) {
%3 = alloca i32**
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [1000 x i8]
%7 = alloca i64
%8 = alloca i8*
%9 = alloca %struct.in_addr
store i32** %0, i32*** %3
store i8* %1, i8** %4
%10 = load i32**, i32*** %3
%11 = icmp ne i32** %10, null
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = call i32 (i8*, i8*, ...) @log_fatal(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = call i32 (i8*, i8*, ...) @log_fatal(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = load i8*, i8** %4
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %21, i32** %5
%22 = load i32*, i32** %5
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 (i8*, i8*, ...) @log_fatal(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %25, i32 %27)
br label %29
29: ; preds = %24, %19
store i64 0, i64* %7
br label %30
30: ; preds = %49, %29
%31 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%32 = load i32*, i32** %5
%33 = call i64 @fgets(i8* %31, i32 1000, i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %56
35: ; preds = %30
%36 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%37 = call i8* @strchr(i8* %36, i8 signext 35)
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = load i8*, i8** %8
store i8 0, i8* %41
br label %42
42: ; preds = %40, %35
%43 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%44 = call i32 @inet_aton(i8* %43, %struct.in_addr* %9)
%45 = icmp ne i32 %44, 1
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%48 = call i32 (i8*, i8*, ...) @log_fatal(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %46, %42
%50 = load i32**, i32*** %3
%51 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %9, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @pbm_set(i32** %50, i32 %52)
%54 = load i64, i64* %7
%55 = add nsw i64 %54, 1
store i64 %55, i64* %7
br label %30
56: ; preds = %30
%57 = load i32*, i32** %5
%58 = call i32 @fclose(i32* %57)
%59 = load i64, i64* %7
ret i64 %59
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
@.str = [4 x i8] c"pbm\00"
@.str.1 = [36 x i8] c"load_from_file called with NULL PBM\00"
@.str.2 = [41 x i8] c"load_from_file called with NULL filename\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [28 x i8] c"unable to open file: %s: %s\00"
@errno = global i32 0
@.str.5 = [31 x i8] c"unable to parse IP address: %s\00"
define i64 @pbm_load_from_file(i32** %0, i8* %1) {
%3 = alloca [1000 x i8]
%4 = alloca %struct.in_addr
%5 = icmp eq i32** %0, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (i8*, i8*, ...) @log_fatal(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = icmp eq i8* %1, null
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 (i8*, i8*, ...) @log_fatal(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %8
%13 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 (i8*, i8*, ...) @log_fatal(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32 %17)
br label %19
19: ; preds = %15, %12
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%21 = call i64 @fgets(i8* %20, i32 1000, i32* %13)
%22 = icmp eq i64 %21, 0
br i1 %22, label %42, label %23
23: ; preds = %19
%24 = bitcast %struct.in_addr* %4 to i8*
%25 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
br label %26
26: ; preds = %23, %36
%27 = phi i64 [ 0, %23 ], [ %39, %36 ]
%28 = call i8* @strchr(i8* %20, i8 signext 35)
%29 = icmp eq i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %26
store i8 0, i8* %28
br label %31
31: ; preds = %30, %26
%32 = call i32 @inet_aton(i8* %20, %struct.in_addr* %4)
%33 = icmp eq i32 %32, 1
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 (i8*, i8*, ...) @log_fatal(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %20)
br label %36
36: ; preds = %34, %31
%37 = load i32, i32* %25
%38 = call i32 @pbm_set(i32** %0, i32 %37)
%39 = add nuw nsw i64 %27, 1
%40 = call i64 @fgets(i8* %20, i32 1000, i32* %13)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %26
42: ; preds = %36, %19
%43 = phi i64 [ 0, %19 ], [ %39, %36 ]
%44 = call i32 @fclose(i32* %13)
ret i64 %43
}