#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 */
typedef struct pirate TYPE_1__;

/* Type definitions */
struct pirate {
  char *name;
  unsigned long booty;
  unsigned int beard_len;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(TYPE_1__ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(TYPE_1__ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, unsigned long, unsigned int);
int main(void) {
  FILE *in, *out;
  struct pirate {
    char name[100];
    unsigned long booty;
    unsigned int beard_len;
  } p, blackbeard = {"EdwardTeach", 1000, 28};
  out = fopen("data", "w");
  if (!out) {
    perror("fopen");
    return 1;
  }
  if (!fwrite(&blackbeard, sizeof(struct pirate), 1, out)) {
    perror("fwrite");
    return 1;
  }
  if (fclose(out)) {
    perror("fclose");
    return 1;
  }
  in = fopen("data", "r");
  if (!in) {
    perror("fopen");
    return 1;
  }
  if (!fread(&p, sizeof(struct pirate), 1, in)) {
    perror("fread");
    return 1;
  }
  printf("name=\"%s\" booty=%lu beard_len=%u\n", p.name, p.booty, p.beard_len);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x108(%rbp),%rdi
lea    0xe70(%rip),%rsi
mov    $0x78,%edx
call   1060 <memcpy@plt>
lea    0xed7(%rip),%rdi
lea    0xed5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d4 <main+0x64>
lea    0xebc(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x160>
lea    -0x108(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x78,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmp    $0x0,%eax
jne    120f <main+0x9f>
lea    0xe87(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x160>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     123a <main+0xca>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x160>
lea    0xe37(%rip),%rdi
lea    0xe4b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1274 <main+0x104>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x160>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x78,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%eax
jne    12af <main+0x13f>
lea    0xdf7(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x160>
lea    -0x90(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xdd7(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0xf8,%rsp
mov    0xf00(%rip),%rax
mov    %rax,0x70(%rsp)
movups 0xee4(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movups 0xec8(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movups 0xeac(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movups 0xe90(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movups 0xe74(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movups 0xe58(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movups 0xe3c(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xea9(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1286 <main+0x126>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%ebp
mov    $0x78,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
test   %eax,%eax
je     128f <main+0x12f>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    129d <main+0x13d>
lea    0xe57(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1286 <main+0x126>
lea    0x80(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x78,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     12b0 <main+0x150>
mov    0xe8(%rsp),%rdx
mov    0xf0(%rsp),%ecx
lea    0xe30(%rip),%rdi
lea    0x80(%rsp),%rsi
call   1050 <printf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0xf8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf2(%rip),%rdi
jmp    12a4 <main+0x144>
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
jmp    127a <main+0x11a>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebp
jmp    127a <main+0x11a>
lea    0xdde(%rip),%rdi
call   1070 <perror@plt>
jmp    127a <main+0x11a>

LLVM IR (-O0):
%struct.pirate = type { [100 x i8], i64, i32 }
%struct.pirate.0 = type { i8*, i64, i32 }
@__const.main.blackbeard = %struct.pirate { [100 x i8] c"EdwardTeach\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", i64 1000, i32 28 }
@.str = [5 x i8] c"data\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"fwrite\00"
@.str.4 = [7 x i8] c"fclose\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [6 x i8] c"fread\00"
@.str.7 = [34 x i8] c"name=\22%s\22 booty=%lu beard_len=%u\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca %struct.pirate
%5 = alloca %struct.pirate
store i32 0, i32* %1
%6 = bitcast %struct.pirate* %5 to i8*
%7 = call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %46
12: ; preds = %0
%13 = bitcast %struct.pirate* %5 to %struct.pirate.0*
%14 = load i32*, i32** %3
%15 = call i32 @fwrite(%struct.pirate.0* %13, i32 120, i32 1, i32* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %46
19: ; preds = %12
%20 = load i32*, i32** %3
%21 = call i64 @fclose(i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %46
25: ; preds = %19
%26 = call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %26, i32** %2
%27 = load i32*, i32** %2
%28 = icmp ne i32* %27, null
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %46
31: ; preds = %25
%32 = bitcast %struct.pirate* %4 to %struct.pirate.0*
%33 = load i32*, i32** %2
%34 = call i32 @fread(%struct.pirate.0* %32, i32 120, i32 1, i32* %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %46
38: ; preds = %31
%39 = getelementptr inbounds %struct.pirate, %struct.pirate* %4, i32 0, i32 0
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %39, i64 0, i64 0
%41 = getelementptr inbounds %struct.pirate, %struct.pirate* %4, i32 0, i32 1
%42 = load i64, i64* %41
%43 = getelementptr inbounds %struct.pirate, %struct.pirate* %4, i32 0, i32 2
%44 = load i32, i32* %43
%45 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i8* %40, i64 %42, i32 %44)
store i32 0, i32* %1
br label %46
46: ; preds = %38, %36, %29, %23, %17, %10
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
%struct.pirate = type { [100 x i8], i64, i32 }
%struct.pirate.0 = type { i8*, i64, i32 }
@__const.main.blackbeard = %struct.pirate { [100 x i8] c"EdwardTeach\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", i64 1000, i32 28 }
@.str = [5 x i8] c"data\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"fwrite\00"
@.str.4 = [7 x i8] c"fclose\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [6 x i8] c"fread\00"
@.str.7 = [34 x i8] c"name=\22%s\22 booty=%lu beard_len=%u\0A\00"
define i32 @main() {
%1 = alloca %struct.pirate
%2 = alloca %struct.pirate
%3 = getelementptr inbounds %struct.pirate, %struct.pirate* %1, i64 0, i32 0, i64 0
%4 = getelementptr inbounds %struct.pirate, %struct.pirate* %2, i64 0, i32 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %37
9: ; preds = %0
%10 = bitcast %struct.pirate* %2 to %struct.pirate.0*
%11 = call i32 @fwrite(%struct.pirate.0* %10, i32 120, i32 1, i32* %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %37
15: ; preds = %9
%16 = tail call i64 @fclose(i32* %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %37
20: ; preds = %15
%21 = tail call i32* @fopen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %37
25: ; preds = %20
%26 = bitcast %struct.pirate* %1 to %struct.pirate.0*
%27 = call i32 @fread(%struct.pirate.0* %26, i32 120, i32 1, i32* %21)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %37
31: ; preds = %25
%32 = getelementptr inbounds %struct.pirate, %struct.pirate* %1, i64 0, i32 1
%33 = load i64, i64* %32
%34 = getelementptr inbounds %struct.pirate, %struct.pirate* %1, i64 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0), i8* %3, i64 %33, i32 %35)
br label %37
37: ; preds = %31, %29, %23, %18, %13, %7
%38 = phi i32 [ 1, %18 ], [ 0, %31 ], [ 1, %29 ], [ 1, %23 ], [ 1, %13 ], [ 1, %7 ]
ret i32 %38
}