#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 mac_frame {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, unsigned char *, int);
void file_action(struct mac_frame *frame, const char *filename,
                 int (*action)(struct mac_frame *, const unsigned char *,
                               unsigned int),
                 const char *warning_message) {
  unsigned char buf[128];
  ssize_t n;
  int fd = open(filename, O_RDONLY);
  if (fd < 0)
    err(EXIT_FAILURE, "cannot open '%s'", filename);
  n = read(fd, buf, 128);
  if (n < 0)
    err(EXIT_FAILURE, "cannot read '%s'", filename);
  if (action(frame, buf, n) < 0)
    errx(EXIT_FAILURE, "%s", warning_message);
  close(fd);
}


x86 assembly (clang, -O0):
<file_action>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xac(%rbp)
cmpl   $0x0,-0xac(%rbp)
jge    11a9 <file_action+0x59>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5c(%rip),%rsi
call   1060 <err@plt>
mov    -0xac(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jge    11ee <file_action+0x9e>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe28(%rip),%rsi
call   1060 <err@plt>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    -0xa8(%rbp),%rcx
mov    %ecx,%edx
call   *%rax
cmp    $0x0,%eax
jge    122a <file_action+0xda>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdfd(%rip),%rsi
call   1030 <errx@plt>
mov    -0xac(%rbp),%edi
call   1040 <close@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_action>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r12
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    119c <file_action+0x4c>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
mov    %r13,%rdx
call   1060 <err@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11cb <file_action+0x7b>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
mov    %r13,%rdx
call   1060 <err@plt>
mov    %rsp,%rsi
mov    %r12,%rdi
mov    %ebx,%edx
call   *%r15
test   %eax,%eax
jns    11f2 <file_action+0xa2>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
mov    %r14,%rdx
call   1030 <errx@plt>
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mac_frame = type { i32 }
@O_RDONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [17 x i8] c"cannot open '%s'\00"
@.str.1 = [17 x i8] c"cannot read '%s'\00"
@.str.2 = [3 x i8] c"%s\00"
define void @file_action(%struct.mac_frame* %0, i8* %1, i32 (%struct.mac_frame*, i8*, i32)* %2, i8* %3) {
%5 = alloca %struct.mac_frame*
%6 = alloca i8*
%7 = alloca i32 (%struct.mac_frame*, i8*, i32)*
%8 = alloca i8*
%9 = alloca [128 x i8]
%10 = alloca i64
%11 = alloca i32
store %struct.mac_frame* %0, %struct.mac_frame** %5
store i8* %1, i8** %6
store i32 (%struct.mac_frame*, i8*, i32)* %2, i32 (%struct.mac_frame*, i8*, i32)** %7
store i8* %3, i8** %8
%12 = load i8*, i8** %6
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* @EXIT_FAILURE
%19 = load i8*, i8** %6
%20 = call i32 @err(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %17, %4
%22 = load i32, i32* %11
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%24 = call i64 @read(i32 %22, i8* %23, i32 128)
store i64 %24, i64* %10
%25 = load i64, i64* %10
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* @EXIT_FAILURE
%29 = load i8*, i8** %6
%30 = call i32 @err(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %27, %21
%32 = load i32 (%struct.mac_frame*, i8*, i32)*, i32 (%struct.mac_frame*, i8*, i32)** %7
%33 = load %struct.mac_frame*, %struct.mac_frame** %5
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%35 = load i64, i64* %10
%36 = trunc i64 %35 to i32
%37 = call i32 %32(%struct.mac_frame* %33, i8* %34, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %31
%40 = load i32, i32* @EXIT_FAILURE
%41 = load i8*, i8** %8
%42 = call i32 @errx(i32 %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %39, %31
%44 = load i32, i32* %11
%45 = call i32 @close(i32 %44)
ret void
}

LLVM IR (-O2):
%struct.mac_frame = type { i32 }
@O_RDONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [17 x i8] c"cannot open '%s'\00"
@.str.1 = [17 x i8] c"cannot read '%s'\00"
@.str.2 = [3 x i8] c"%s\00"
define void @file_action(%struct.mac_frame* %0, i8* %1, i32 (%struct.mac_frame*, i8*, i32)* nocapture %2, i8* %3) {
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %1, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @err(i32 %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %13
13: ; preds = %10, %4
%14 = call i64 @read(i32 %8, i8* %6, i32 128)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @err(i32 %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %19
19: ; preds = %16, %13
%20 = trunc i64 %14 to i32
%21 = call i32 %2(%struct.mac_frame* %0, i8* %6, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @errx(i32 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %26
26: ; preds = %23, %19
%27 = call i32 @close(i32 %8)
ret void
}