#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 int /*<<< orphan*/ pad;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
long ftell(int /*<<< orphan*/ *);
int fwrite(int /*<<< orphan*/ **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ msg_error(char *);
int /*<<< orphan*/ msg_perror(char *);
bool pad_region(FILE *f) {
  uint8_t pad[512];
  memset(pad, 0, sizeof(pad));
  long pos = ftell(f);
  if (pos < 0) {
    msg_perror("ftell");
    return false;
  }
  if (pos & 511) {
    int cnt = 512 - (pos & 511);
    if (fwrite(&pad, 1, cnt, f) != cnt) {
      msg_error("Failed to write region pad\n");
      return false;
    }
  }
  return true;
}


x86 assembly (clang, -O0):
<pad_region>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x818(%rbp)
cmpq   $0x0,-0x818(%rbp)
jge    11a8 <pad_region+0x58>
lea    0xe69(%rip),%rdi
call   1060 <msg_perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1226 <pad_region+0xd6>
mov    -0x818(%rbp),%rax
and    $0x1ff,%rax
cmp    $0x0,%rax
je     121f <pad_region+0xcf>
mov    -0x818(%rbp),%rcx
and    $0x1ff,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
mov    %eax,-0x81c(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x81c(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
cmp    -0x81c(%rbp),%eax
je     121a <pad_region+0xca>
lea    0xdfd(%rip),%rdi
call   1050 <msg_error@plt>
movl   $0x0,-0x4(%rbp)
jmp    1226 <pad_region+0xd6>
jmp    121f <pad_region+0xcf>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pad_region>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x800,%edx
call   1030 <memset@plt>
mov    %r14,%rdi
call   1040 <ftell@plt>
test   %rax,%rax
js     11b6 <pad_region+0x66>
and    $0x1ff,%rax
je     11c4 <pad_region+0x74>
mov    $0x200,%ebp
sub    %eax,%ebp
mov    %rsp,%rdi
mov    $0x1,%ebx
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
cmp    %ebp,%eax
je     11c9 <pad_region+0x79>
lea    0xe59(%rip),%rdi
call   1050 <msg_error@plt>
xor    %ebx,%ebx
jmp    11c9 <pad_region+0x79>
lea    0xe43(%rip),%rdi
call   1060 <msg_perror@plt>
jmp    11c9 <pad_region+0x79>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"ftell\00"
@.str.1 = [28 x i8] c"Failed to write region pad\0A\00"
define i32 @pad_region(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [512 x i32]
%5 = alloca i64
%6 = alloca i32
store i32* %0, i32** %3
%7 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 0
%8 = call i32 @memset(i32* %7, i32 0, i32 2048)
%9 = load i32*, i32** %3
%10 = call i64 @ftell(i32* %9)
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @msg_perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %34
15: ; preds = %1
%16 = load i64, i64* %5
%17 = and i64 %16, 511
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %15
%20 = load i64, i64* %5
%21 = and i64 %20, 511
%22 = sub nsw i64 512, %21
%23 = trunc i64 %22 to i32
store i32 %23, i32* %6
%24 = bitcast [512 x i32]* %4 to i32**
%25 = load i32, i32* %6
%26 = load i32*, i32** %3
%27 = call i32 @fwrite(i32** %24, i32 1, i32 %25, i32* %26)
%28 = load i32, i32* %6
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %19
%31 = call i32 @msg_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %34
32: ; preds = %19
br label %33
33: ; preds = %32, %15
store i32 1, i32* %2
br label %34
34: ; preds = %33, %30, %13
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [6 x i8] c"ftell\00"
@.str.1 = [28 x i8] c"Failed to write region pad\0A\00"
define i32 @pad_region(i32* nocapture %0) {
%2 = alloca [512 x i32]
%3 = bitcast [512 x i32]* %2 to i8*
%4 = getelementptr inbounds [512 x i32], [512 x i32]* %2, i64 0, i64 0
%5 = call i32 @memset(i32* %4, i32 0, i32 2048)
%6 = call i64 @ftell(i32* %0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @msg_perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %21
10: ; preds = %1
%11 = and i64 %6, 511
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = trunc i64 %11 to i32
%15 = sub nuw nsw i32 512, %14
%16 = bitcast [512 x i32]* %2 to i32**
%17 = call i32 @fwrite(i32** %16, i32 1, i32 %15, i32* %0)
%18 = icmp eq i32 %17, %15
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = call i32 @msg_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %10, %13, %19, %8
%22 = phi i32 [ 0, %8 ], [ 0, %19 ], [ 1, %13 ], [ 1, %10 ]
ret i32 %22
}