#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 = {"Edward Teach", 950, 48};
  out = fopen("data", "w");
  if (!out)
    return 1;
  if (!fwrite(&blackbeard, sizeof(struct pirate), 1, out)) {
    perror("fwrite");
    return 1;
  }
  if (fclose(out))
    return 1;
  in = fopen("data", "r");
  if (!in)
    return 1;
  if (!fread(&p, sizeof(struct pirate), 1, in)) {
    perror("fread");
    return 1;
  }
  if (fclose(in))
    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    11c8 <main+0x58>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
lea    -0x108(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x78,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmp    $0x0,%eax
jne    1203 <main+0x93>
lea    0xe8d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     1222 <main+0xb2>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
lea    0xe4f(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1250 <main+0xe0>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x78,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%eax
jne    128b <main+0x11b>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12aa <main+0x13a>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x15b>
lea    -0x90(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xdcf(%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   %r14
push   %rbx
sub    $0xf0,%rsp
mov    0xefe(%rip),%rax
mov    %rax,0x70(%rsp)
movups 0xee2(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movups 0xec6(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movups 0xeaa(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movups 0xe8e(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movups 0xe72(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movups 0xe56(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movups 0xe3a(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xea7(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1284 <main+0x124>
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     1297 <main+0x137>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    1289 <main+0x129>
lea    0xe59(%rip),%rdi
lea    0xe60(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1289 <main+0x129>
mov    %rax,%r14
lea    0x78(%rsp),%rdi
mov    $0x1,%ebp
mov    $0x78,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     12a5 <main+0x145>
mov    %r14,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    1289 <main+0x129>
mov    0xe0(%rsp),%rdx
mov    0xe8(%rsp),%ecx
lea    0xe18(%rip),%rdi
lea    0x78(%rsp),%rsi
call   1050 <printf@plt>
xor    %ebp,%ebp
jmp    1289 <main+0x129>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0xf0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde1(%rip),%rdi
call   1070 <perror@plt>
jmp    1289 <main+0x129>
lea    0xddc(%rip),%rdi
call   1070 <perror@plt>
jmp    1289 <main+0x129>

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"Edward Teach\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 950, i32 48 }
@.str = [5 x i8] c"data\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"fwrite\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"fread\00"
@.str.5 = [40 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 %11, label %10
10: ; preds = %0
store i32 1, i32* %1
br label %48
11: ; preds = %0
%12 = bitcast %struct.pirate* %5 to %struct.pirate.0*
%13 = load i32*, i32** %3
%14 = call i32 @fwrite(%struct.pirate.0* %12, i32 120, i32 1, i32* %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %48
18: ; preds = %11
%19 = load i32*, i32** %3
%20 = call i64 @fclose(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 1, i32* %1
br label %48
23: ; preds = %18
%24 = 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.3, i64 0, i64 0))
store i32* %24, i32** %2
%25 = load i32*, i32** %2
%26 = icmp ne i32* %25, null
br i1 %26, label %28, label %27
27: ; preds = %23
store i32 1, i32* %1
br label %48
28: ; preds = %23
%29 = bitcast %struct.pirate* %4 to %struct.pirate.0*
%30 = load i32*, i32** %2
%31 = call i32 @fread(%struct.pirate.0* %29, i32 120, i32 1, i32* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %48
35: ; preds = %28
%36 = load i32*, i32** %2
%37 = call i64 @fclose(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 1, i32* %1
br label %48
40: ; preds = %35
%41 = getelementptr inbounds %struct.pirate, %struct.pirate* %4, i32 0, i32 0
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %41, i64 0, i64 0
%43 = getelementptr inbounds %struct.pirate, %struct.pirate* %4, i32 0, i32 1
%44 = load i64, i64* %43
%45 = getelementptr inbounds %struct.pirate, %struct.pirate* %4, i32 0, i32 2
%46 = load i32, i32* %45
%47 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %42, i64 %44, i32 %46)
store i32 0, i32* %1
br label %48
48: ; preds = %40, %39, %33, %27, %22, %16, %10
%49 = load i32, i32* %1
ret i32 %49
}

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"Edward Teach\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 950, i32 48 }
@.str = [5 x i8] c"data\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [7 x i8] c"fwrite\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"fread\00"
@.str.5 = [40 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 %34, label %7
7: ; preds = %0
%8 = bitcast %struct.pirate* %2 to %struct.pirate.0*
%9 = call i32 @fwrite(%struct.pirate.0* %8, i32 120, i32 1, i32* %5)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %34
13: ; preds = %7
%14 = tail call i64 @fclose(i32* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = 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.3, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %34, label %19
19: ; preds = %16
%20 = bitcast %struct.pirate* %1 to %struct.pirate.0*
%21 = call i32 @fread(%struct.pirate.0* %20, i32 120, i32 1, i32* %17)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %34
25: ; preds = %19
%26 = tail call i64 @fclose(i32* %17)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = getelementptr inbounds %struct.pirate, %struct.pirate* %1, i64 0, i32 1
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.pirate, %struct.pirate* %1, i64 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %3, i64 %30, i32 %32)
br label %34
34: ; preds = %25, %16, %13, %0, %28, %23, %11
%35 = phi i32 [ 0, %28 ], [ 1, %23 ], [ 1, %11 ], [ 1, %0 ], [ 1, %13 ], [ 1, %16 ], [ 1, %25 ]
ret i32 %35
}