#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*/ FILE;

/* Variables and functions */
scalar_t__ snapshot_write_byte(int /*<<< orphan*/ *, unsigned char);
int snapshot_write_padded_string(FILE *f, const char *s, unsigned char pad_char,
                                 int len) {
  int i, found_zero;
  unsigned char c;
  for (i = found_zero = 0; i < len; i++) {
    if (!found_zero && s[i] == 0)
      found_zero = 1;
    c = found_zero ? (unsigned char)pad_char : (unsigned char)s[i];
    if (snapshot_write_byte(f, c) < 0)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<snapshot_write_padded_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11c9 <snapshot_write_padded_string+0xb9>
cmpl   $0x0,-0x28(%rbp)
jne    1168 <snapshot_write_padded_string+0x58>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1168 <snapshot_write_padded_string+0x58>
movl   $0x1,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     117e <snapshot_write_padded_string+0x6e>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    118d <snapshot_write_padded_string+0x7d>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rdi
movzbl -0x29(%rbp),%esi
call   1030 <snapshot_write_byte@plt>
cmp    $0x0,%rax
jge    11b6 <snapshot_write_padded_string+0xa6>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <snapshot_write_padded_string+0xc0>
jmp    11bb <snapshot_write_padded_string+0xab>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1136 <snapshot_write_padded_string+0x26>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<snapshot_write_padded_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
test   %ecx,%ecx
jle    117b <snapshot_write_padded_string+0x6b>
mov    %edx,%ebp
mov    %rdi,%r15
mov    %ecx,%r12d
xor    %ebx,%ebx
movzbl %bpl,%r13d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%eax
test   %r14d,%r14d
mov    $0x1,%r14d
jne    1162 <snapshot_write_padded_string+0x52>
mov    (%rsp),%rax
movzbl (%rax,%rbx,1),%eax
xor    %r14d,%r14d
test   %al,%al
sete   %r14b
cmove  %r13d,%eax
movzbl %al,%esi
mov    %r15,%rdi
call   1030 <snapshot_write_byte@plt>
test   %rax,%rax
js     117f <snapshot_write_padded_string+0x6f>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1140 <snapshot_write_padded_string+0x30>
xor    %eax,%eax
jmp    1184 <snapshot_write_padded_string+0x74>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @snapshot_write_padded_string(i32* %0, i8* %1, i8 zeroext %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
store i32* %0, i32** %6
store i8* %1, i8** %7
store i8 %2, i8* %8
store i32 %3, i32* %9
store i32 0, i32* %11
store i32 0, i32* %10
br label %13
13: ; preds = %51, %4
%14 = load i32, i32* %10
%15 = load i32, i32* %9
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %54
17: ; preds = %13
%18 = load i32, i32* %11
%19 = icmp ne i32 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %20
store i32 1, i32* %11
br label %29
29: ; preds = %28, %20, %17
%30 = load i32, i32* %11
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8, i8* %8
%34 = zext i8 %33 to i32
br label %42
35: ; preds = %29
%36 = load i8*, i8** %7
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
br label %42
42: ; preds = %35, %32
%43 = phi i32 [ %34, %32 ], [ %41, %35 ]
%44 = trunc i32 %43 to i8
store i8 %44, i8* %12
%45 = load i32*, i32** %6
%46 = load i8, i8* %12
%47 = call i64 @snapshot_write_byte(i32* %45, i8 zeroext %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %42
store i32 -1, i32* %5
br label %55
50: ; preds = %42
br label %51
51: ; preds = %50
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
br label %13
54: ; preds = %13
store i32 0, i32* %5
br label %55
55: ; preds = %54, %49
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
define i32 @snapshot_write_padded_string(i32* %0, i8* nocapture %1, i8 zeroext %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %26
6: ; preds = %4
%7 = zext i32 %3 to i64
br label %10
8: ; preds = %20
%9 = icmp eq i64 %25, %7
br i1 %9, label %26, label %10
10: ; preds = %6, %8
%11 = phi i64 [ 0, %6 ], [ %25, %8 ]
%12 = phi i32 [ 0, %6 ], [ %21, %8 ]
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %1, i64 %11
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
%18 = zext i1 %17 to i32
%19 = select i1 %17, i8 %2, i8 %16
br label %20
20: ; preds = %14, %10
%21 = phi i32 [ 1, %10 ], [ %18, %14 ]
%22 = phi i8 [ %2, %10 ], [ %19, %14 ]
%23 = tail call i64 @snapshot_write_byte(i32* %0, i8 zeroext %22)
%24 = icmp slt i64 %23, 0
%25 = add nuw nsw i64 %11, 1
br i1 %24, label %26, label %8
26: ; preds = %20, %8, %4
%27 = phi i32 [ 0, %4 ], [ 0, %8 ], [ -1, %20 ]
ret i32 %27
}