#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 sock_filter {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(struct sock_filter const *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, unsigned int);
char *strerror(int /*<<< orphan*/);
void dump_filter(const char *dname, unsigned cnt, const struct sock_filter *f,
                 size_t nmemb) {
  char fname[1024];
  snprintf(fname, 1024, "%s/%u", dname, cnt);
  printf("Writing file %s\n", fname);
  FILE *fp = fopen(fname, "w");
  if (!fp) {
    printf("Error: Cannot open %s for writing: %s\n", fname, strerror(errno));
    exit(1);
  }
  if (fwrite(f, sizeof(struct sock_filter), nmemb, fp) != nmemb) {
    printf("Error: Cannot write %s\n", fname);
    exit(1);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<dump_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
mov    $0x400,%esi
lea    0xe5b(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x420(%rbp),%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1225 <dump_filter+0xb5>
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x430(%rbp),%rsi
mov    %rax,%rdx
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x428(%rbp),%rcx
mov    $0x4,%esi
call   1080 <fwrite@plt>
cmp    -0x20(%rbp),%rax
je     1267 <dump_filter+0xf7>
lea    -0x420(%rbp),%rsi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_filter>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %esi,%r8d
mov    %rdi,%rcx
lea    0xe71(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1050 <snprintf@plt>
lea    0xe60(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe60(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f4 <dump_filter+0x84>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1080 <fwrite@plt>
cmp    %r15,%rax
jne    1220 <dump_filter+0xb0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe10(%rip),%rdi
mov    %rsp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe19(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sock_filter = type { i32 }
@.str = [6 x i8] c"%s/%u\00"
@.str.1 = [17 x i8] c"Writing file %s\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [39 x i8] c"Error: Cannot open %s for writing: %s\0A\00"
@errno = global i32 0
@.str.4 = [24 x i8] c"Error: Cannot write %s\0A\00"
define void @dump_filter(i8* %0, i32 %1, %struct.sock_filter* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.sock_filter*
%8 = alloca i64
%9 = alloca [1024 x i8]
%10 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store %struct.sock_filter* %2, %struct.sock_filter** %7
store i64 %3, i64* %8
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = call i32 @snprintf(i8* %11, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %18, i32** %10
%19 = load i32*, i32** %10
%20 = icmp ne i32* %19, null
br i1 %20, label %27, label %21
21: ; preds = %4
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %22, i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %4
%28 = load %struct.sock_filter*, %struct.sock_filter** %7
%29 = load i64, i64* %8
%30 = load i32*, i32** %10
%31 = call i64 @fwrite(%struct.sock_filter* %28, i32 4, i64 %29, i32* %30)
%32 = load i64, i64* %8
%33 = icmp ne i64 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %27
%39 = load i32*, i32** %10
%40 = call i32 @fclose(i32* %39)
ret void
}

LLVM IR (-O2):
%struct.sock_filter = type { i32 }
@.str = [6 x i8] c"%s/%u\00"
@.str.1 = [17 x i8] c"Writing file %s\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [39 x i8] c"Error: Cannot open %s for writing: %s\0A\00"
@errno = global i32 0
@.str.4 = [24 x i8] c"Error: Cannot write %s\0A\00"
define void @dump_filter(i8* %0, i32 %1, %struct.sock_filter* nocapture %2, i64 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%8 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%9 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %4
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %4
%17 = call i64 @fwrite(%struct.sock_filter* %2, i32 4, i64 %3, i32* %9)
%18 = icmp eq i64 %17, %3
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = call i32 @fclose(i32* %9)
ret void
}