#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 uint32_t;
struct flashrom_flashctx {
  int dummy;
};

/* Variables and functions */
int min(int, int);
int spi_sfdp_read_sfdp_chunk(struct flashrom_flashctx *, int,
                             int /*<<< orphan*/ *, int);
int spi_sfdp_read_sfdp(struct flashrom_flashctx *flash, uint32_t address,
                       uint8_t *buf, int len) {
  int maxstep = 2;
  int ret = 0;
  while (len > 0) {
    int step = min(len, maxstep);
    ret = spi_sfdp_read_sfdp_chunk(flash, address, buf, step);
    if (ret)
      return ret;
    address += step;
    buf += step;
    len -= step;
  }
  return ret;
}


x86 assembly (clang, -O0):
<spi_sfdp_read_sfdp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x2,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11b5 <spi_sfdp_read_sfdp+0x95>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1040 <min@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%ecx
call   1030 <spi_sfdp_read_sfdp_chunk@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1187 <spi_sfdp_read_sfdp+0x67>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <spi_sfdp_read_sfdp+0x9b>
mov    -0x30(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
jmp    1144 <spi_sfdp_read_sfdp+0x24>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_sfdp_read_sfdp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %ecx,%ecx
jle    1176 <spi_sfdp_read_sfdp+0x56>
mov    %ecx,%r15d
mov    %rdx,%r13
mov    %esi,%ebp
mov    %rdi,%r12
nopl   (%rax)
mov    %r15d,%edi
mov    $0x2,%esi
call   1040 <min@plt>
mov    %eax,%ebx
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
mov    %eax,%ecx
call   1030 <spi_sfdp_read_sfdp_chunk@plt>
test   %eax,%eax
jne    1173 <spi_sfdp_read_sfdp+0x53>
add    %ebx,%ebp
movslq %ebx,%rax
lea    0x0(%r13,%rax,4),%r13
sub    %ebx,%r15d
jg     1140 <spi_sfdp_read_sfdp+0x20>
jmp    1176 <spi_sfdp_read_sfdp+0x56>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flashrom_flashctx = type { i32 }
define i32 @spi_sfdp_read_sfdp(%struct.flashrom_flashctx* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.flashrom_flashctx*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.flashrom_flashctx* %0, %struct.flashrom_flashctx** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32 2, i32* %10
store i32 0, i32* %11
br label %13
13: ; preds = %29, %4
%14 = load i32, i32* %9
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %40
16: ; preds = %13
%17 = load i32, i32* %9
%18 = load i32, i32* %10
%19 = call i32 @min(i32 %17, i32 %18)
store i32 %19, i32* %12
%20 = load %struct.flashrom_flashctx*, %struct.flashrom_flashctx** %6
%21 = load i32, i32* %7
%22 = load i32*, i32** %8
%23 = load i32, i32* %12
%24 = call i32 @spi_sfdp_read_sfdp_chunk(%struct.flashrom_flashctx* %20, i32 %21, i32* %22, i32 %23)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = load i32, i32* %11
store i32 %28, i32* %5
br label %42
29: ; preds = %16
%30 = load i32, i32* %12
%31 = load i32, i32* %7
%32 = add nsw i32 %31, %30
store i32 %32, i32* %7
%33 = load i32, i32* %12
%34 = load i32*, i32** %8
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i32, i32* %34, i64 %35
store i32* %36, i32** %8
%37 = load i32, i32* %12
%38 = load i32, i32* %9
%39 = sub nsw i32 %38, %37
store i32 %39, i32* %9
br label %13
40: ; preds = %13
%41 = load i32, i32* %11
store i32 %41, i32* %5
br label %42
42: ; preds = %40, %27
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
%struct.flashrom_flashctx = type { i32 }
define i32 @spi_sfdp_read_sfdp(%struct.flashrom_flashctx* %0, i32 %1, i32* %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %19
6: ; preds = %4, %13
%7 = phi i32 [ %14, %13 ], [ %1, %4 ]
%8 = phi i32 [ %17, %13 ], [ %3, %4 ]
%9 = phi i32* [ %16, %13 ], [ %2, %4 ]
%10 = tail call i32 @min(i32 %8, i32 2)
%11 = tail call i32 @spi_sfdp_read_sfdp_chunk(%struct.flashrom_flashctx* %0, i32 %7, i32* %9, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %6
%14 = add nsw i32 %10, %7
%15 = sext i32 %10 to i64
%16 = getelementptr inbounds i32, i32* %9, i64 %15
%17 = sub nsw i32 %8, %10
%18 = icmp sgt i32 %17, 0
br i1 %18, label %6, label %19
19: ; preds = %13, %6, %4
%20 = phi i32 [ 0, %4 ], [ %11, %6 ], [ 0, %13 ]
ret i32 %20
}