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

/* Variables and functions */
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, size_t);
int write_out_data(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t,
                   struct csum_state *);
int write_out_padding(FILE *outfile, size_t len, uint8_t padc,
                      struct csum_state *css) {
  uint8_t buf[512];
  size_t buflen = sizeof(buf);
  int err;
  memset(buf, padc, buflen);
  while (len > 0) {
    if (len < buflen)
      buflen = len;
    err = write_out_data(outfile, buf, buflen, css);
    if (err)
      return err;
    len -= buflen;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_out_padding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x800,-0x838(%rbp)
lea    -0x830(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x838(%rbp),%rdx
call   1030 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
jbe    11d5 <write_out_padding+0xb5>
mov    -0x18(%rbp),%rax
cmp    -0x838(%rbp),%rax
jae    1182 <write_out_padding+0x62>
mov    -0x18(%rbp),%rax
mov    %rax,-0x838(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x830(%rbp),%rsi
mov    -0x838(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <write_out_data@plt>
mov    %eax,-0x83c(%rbp)
cmpl   $0x0,-0x83c(%rbp)
je     11be <write_out_padding+0x9e>
mov    -0x83c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <write_out_padding+0xbc>
mov    -0x838(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    115b <write_out_padding+0x3b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_out_padding>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rcx,%r14
mov    %edx,%eax
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x800,%ebp
mov    $0x800,%edx
mov    %eax,%esi
call   1030 <memset@plt>
xor    %r12d,%r12d
test   %rbx,%rbx
je     1186 <write_out_padding+0x66>
mov    %rsp,%r13
nopl   0x0(%rax,%rax,1)
cmp    %rbp,%rbx
cmovb  %rbx,%rbp
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
call   1040 <write_out_data@plt>
test   %eax,%eax
jne    1183 <write_out_padding+0x63>
sub    %rbp,%rbx
jne    1160 <write_out_padding+0x40>
jmp    1186 <write_out_padding+0x66>
mov    %eax,%r12d
mov    %r12d,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.csum_state = type { i32 }
define i32 @write_out_padding(i32* %0, i64 %1, i32 %2, %struct.csum_state* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.csum_state*
%10 = alloca [512 x i32]
%11 = alloca i64
%12 = alloca i32
store i32* %0, i32** %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store %struct.csum_state* %3, %struct.csum_state** %9
store i64 2048, i64* %11
%13 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 0
%14 = load i32, i32* %8
%15 = load i64, i64* %11
%16 = call i32 @memset(i32* %13, i32 %14, i64 %15)
br label %17
17: ; preds = %36, %4
%18 = load i64, i64* %7
%19 = icmp ugt i64 %18, 0
br i1 %19, label %20, label %40
20: ; preds = %17
%21 = load i64, i64* %7
%22 = load i64, i64* %11
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i64, i64* %7
store i64 %25, i64* %11
br label %26
26: ; preds = %24, %20
%27 = load i32*, i32** %6
%28 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 0
%29 = load i64, i64* %11
%30 = load %struct.csum_state*, %struct.csum_state** %9
%31 = call i32 @write_out_data(i32* %27, i32* %28, i64 %29, %struct.csum_state* %30)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = load i32, i32* %12
store i32 %35, i32* %5
br label %41
36: ; preds = %26
%37 = load i64, i64* %11
%38 = load i64, i64* %7
%39 = sub i64 %38, %37
store i64 %39, i64* %7
br label %17
40: ; preds = %17
store i32 0, i32* %5
br label %41
41: ; preds = %40, %34
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
%struct.csum_state = type { i32 }
define i32 @write_out_padding(i32* %0, i64 %1, i32 %2, %struct.csum_state* %3) {
%5 = alloca [512 x i32]
%6 = bitcast [512 x i32]* %5 to i8*
%7 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 0
%8 = call i32 @memset(i32* %7, i32 %2, i64 2048)
%9 = icmp eq i64 %1, 0
br i1 %9, label %20, label %12
10: ; preds = %12
%11 = icmp eq i64 %19, 0
br i1 %11, label %20, label %12
12: ; preds = %4, %10
%13 = phi i64 [ %16, %10 ], [ 2048, %4 ]
%14 = phi i64 [ %19, %10 ], [ %1, %4 ]
%15 = icmp ult i64 %14, %13
%16 = select i1 %15, i64 %14, i64 %13
%17 = call i32 @write_out_data(i32* %0, i32* %7, i64 %16, %struct.csum_state* %3)
%18 = icmp eq i32 %17, 0
%19 = sub i64 %14, %16
br i1 %18, label %10, label %20
20: ; preds = %12, %10, %4
%21 = phi i32 [ 0, %4 ], [ 0, %10 ], [ %17, %12 ]
ret i32 %21
}