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

/* Variables and functions */
unsigned long _tga_write_get_pixel(unsigned char *, int);
long _tga_write_count_save_pixels(unsigned char *ucp_will_encode,
                                  int l_samples_per_pixel, long l_pixel_count) {
  unsigned long ul_pixel;
  unsigned long ul_next_pixel;
  long l_count;
  l_count = 1;
  ul_pixel = _tga_write_get_pixel(ucp_will_encode, l_samples_per_pixel);
  l_pixel_count--;
  while ((long)0 < l_pixel_count) {
    ucp_will_encode += l_samples_per_pixel;
    ul_next_pixel = _tga_write_get_pixel(ucp_will_encode, l_samples_per_pixel);
    if (ul_next_pixel != ul_pixel)
      break;
    ++l_count;
    --l_pixel_count;
  }
  return l_count;
}


x86 assembly (clang, -O0):
<_tga_write_count_save_pixels>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x1,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <_tga_write_get_pixel@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jge    11a4 <_tga_write_count_save_pixels+0x94>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <_tga_write_get_pixel@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1187 <_tga_write_count_save_pixels+0x77>
jmp    11a4 <_tga_write_count_save_pixels+0x94>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1147 <_tga_write_count_save_pixels+0x37>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tga_write_count_save_pixels>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
call   1030 <_tga_write_get_pixel@plt>
mov    $0x1,%ebp
cmp    $0x2,%r14
jl     116f <_tga_write_count_save_pixels+0x5f>
mov    %rax,%r12
movslq %r15d,%r13
add    %r13,%rbx
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <_tga_write_get_pixel@plt>
cmp    %r12,%rax
jne    116f <_tga_write_count_save_pixels+0x5f>
add    $0x1,%rbp
add    %r13,%rbx
cmp    %rbp,%r14
jne    1150 <_tga_write_count_save_pixels+0x40>
mov    %r14,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @_tga_write_count_save_pixels(i8* %0, i32 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i64 %2, i64* %6
store i64 1, i64* %9
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = call i64 @_tga_write_get_pixel(i8* %10, i32 %11)
store i64 %12, i64* %7
%13 = load i64, i64* %6
%14 = add nsw i64 %13, -1
store i64 %14, i64* %6
br label %15
15: ; preds = %30, %3
%16 = load i64, i64* %6
%17 = icmp slt i64 0, %16
br i1 %17, label %18, label %35
18: ; preds = %15
%19 = load i32, i32* %5
%20 = load i8*, i8** %4
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = call i64 @_tga_write_get_pixel(i8* %23, i32 %24)
store i64 %25, i64* %8
%26 = load i64, i64* %8
%27 = load i64, i64* %7
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %18
br label %35
30: ; preds = %18
%31 = load i64, i64* %9
%32 = add nsw i64 %31, 1
store i64 %32, i64* %9
%33 = load i64, i64* %6
%34 = add nsw i64 %33, -1
store i64 %34, i64* %6
br label %15
35: ; preds = %29, %15
%36 = load i64, i64* %9
ret i64 %36
}

LLVM IR (-O2):
define i64 @_tga_write_count_save_pixels(i8* %0, i32 %1, i64 %2) {
%4 = tail call i64 @_tga_write_get_pixel(i8* %0, i32 %1)
%5 = sext i32 %1 to i64
%6 = icmp sgt i64 %2, 1
br i1 %6, label %7, label %16
7: ; preds = %3, %13
%8 = phi i64 [ %14, %13 ], [ 1, %3 ]
%9 = phi i8* [ %10, %13 ], [ %0, %3 ]
%10 = getelementptr inbounds i8, i8* %9, i64 %5
%11 = tail call i64 @_tga_write_get_pixel(i8* %10, i32 %1)
%12 = icmp eq i64 %11, %4
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = add nuw nsw i64 %8, 1
%15 = icmp eq i64 %14, %2
br i1 %15, label %16, label %7
16: ; preds = %13, %7, %3
%17 = phi i64 [ 1, %3 ], [ %8, %7 ], [ %2, %13 ]
ret i64 %17
}