#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);
scalar_t__ 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);
  memset(buf, padc, buflen);
  while (len > 0) {
    if (len < buflen)
      buflen = len;
    if (write_out_data(outfile, buf, buflen, css))
      return -1;
    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    11ca <write_out_padding+0xaa>
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>
cmp    $0x0,%rax
je     11b3 <write_out_padding+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <write_out_padding+0xb1>
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,%r12
mov    %rsp,%rdi
mov    $0x800,%ebp
mov    $0x800,%edx
mov    %eax,%esi
call   1030 <memset@plt>
xor    %r15d,%r15d
test   %rbx,%rbx
je     118a <write_out_padding+0x6a>
mov    %rsp,%r13
nopl   0x0(%rax,%rax,1)
cmp    %rbp,%rbx
cmovb  %rbx,%rbp
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r14,%rcx
call   1040 <write_out_data@plt>
test   %rax,%rax
jne    1184 <write_out_padding+0x64>
sub    %rbp,%rbx
jne    1160 <write_out_padding+0x40>
jmp    118a <write_out_padding+0x6a>
mov    $0xffffffff,%r15d
mov    %r15d,%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
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
%12 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 0
%13 = load i32, i32* %8
%14 = load i64, i64* %11
%15 = call i32 @memset(i32* %12, i32 %13, i64 %14)
br label %16
16: ; preds = %33, %4
%17 = load i64, i64* %7
%18 = icmp ugt i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %16
%20 = load i64, i64* %7
%21 = load i64, i64* %11
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i64, i64* %7
store i64 %24, i64* %11
br label %25
25: ; preds = %23, %19
%26 = load i32*, i32** %6
%27 = getelementptr inbounds [512 x i32], [512 x i32]* %10, i64 0, i64 0
%28 = load i64, i64* %11
%29 = load %struct.csum_state*, %struct.csum_state** %9
%30 = call i64 @write_out_data(i32* %26, i32* %27, i64 %28, %struct.csum_state* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %5
br label %38
33: ; preds = %25
%34 = load i64, i64* %11
%35 = load i64, i64* %7
%36 = sub i64 %35, %34
store i64 %36, i64* %7
br label %16
37: ; preds = %16
store i32 0, i32* %5
br label %38
38: ; preds = %37, %32
%39 = load i32, i32* %5
ret i32 %39
}

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 i64 @write_out_data(i32* %0, i32* %7, i64 %16, %struct.csum_state* %3)
%18 = icmp eq i64 %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 ], [ -1, %12 ]
ret i32 %21
}