#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int lseek(int, int, int /*<<< orphan*/);
int write(int, int /*<<< orphan*/ *, int);
off_t pad_file(int fd, int boundary) {
  off_t pos = lseek(fd, 0, SEEK_CUR);
  uint8_t tmp = 0;
  if (boundary <= 0)
    return pos;
  pos = (pos & ~(boundary - 1)) + boundary;
  if (lseek(fd, pos - 1, SEEK_SET) == (off_t)-1)
    return (off_t)-1;
  if (write(fd, &tmp, 1) != 1)
    return (off_t)-1;
  return pos;
}


x86 assembly (clang, -O0):
<pad_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eb0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1160 <pad_file+0x40>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cc <pad_file+0xac>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
sub    $0x1,%esi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11a0 <pad_file+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <pad_file+0xac>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11c6 <pad_file+0xa6>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <pad_file+0xac>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pad_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
mov    0x2eb4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%ebx
movl   $0x0,0xc(%rsp)
test   %ebp,%ebp
jle    1195 <pad_file+0x75>
mov    %ebp,%eax
neg    %eax
and    %eax,%ebx
lea    (%rbx,%rbp,1),%esi
add    $0xffffffff,%esi
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     1190 <pad_file+0x70>
add    %ebp,%ebx
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
mov    $0xffffffff,%eax
cmove  %ebx,%eax
mov    %eax,%ebx
jmp    1195 <pad_file+0x75>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
define i32 @pad_file(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* @SEEK_CUR
%10 = call i32 @lseek(i32 %8, i32 0, i32 %9)
store i32 %10, i32* %6
store i32 0, i32* %7
%11 = load i32, i32* %5
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %37
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* %5
%18 = sub nsw i32 %17, 1
%19 = xor i32 %18, -1
%20 = and i32 %16, %19
%21 = load i32, i32* %5
%22 = add nsw i32 %20, %21
store i32 %22, i32* %6
%23 = load i32, i32* %4
%24 = load i32, i32* %6
%25 = sub nsw i32 %24, 1
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %23, i32 %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %15
store i32 -1, i32* %3
br label %37
30: ; preds = %15
%31 = load i32, i32* %4
%32 = call i32 @write(i32 %31, i32* %7, i32 1)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 -1, i32* %3
br label %37
35: ; preds = %30
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %34, %29, %13
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
define i32 @pad_file(i32 %0, i32 %1) {
%3 = alloca i32
%4 = load i32, i32* @SEEK_CUR
%5 = tail call i32 @lseek(i32 %0, i32 0, i32 %4)
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = icmp slt i32 %1, 1
br i1 %7, label %20, label %8
8: ; preds = %2
%9 = sub nsw i32 0, %1
%10 = and i32 %5, %9
%11 = add nsw i32 %10, %1
%12 = add nsw i32 %11, -1
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @lseek(i32 %0, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %20, label %16
16: ; preds = %8
%17 = call i32 @write(i32 %0, i32* %3, i32 1)
%18 = icmp eq i32 %17, 1
%19 = select i1 %18, i32 %11, i32 -1
br label %20
20: ; preds = %16, %8, %2
%21 = phi i32 [ %5, %2 ], [ -1, %8 ], [ %19, %16 ]
ret i32 %21
}