#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 scalar_t__ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ ntohl(scalar_t__);
int /*<<< orphan*/ read(int, scalar_t__ *, int);
int /*<<< orphan*/ stderr;
scalar_t__ sum_addc(scalar_t__, scalar_t__);
uint32_t sum_compute(int fd, size_t romsize) {
  size_t i;
  uint32_t longword_be, longword, sum;
  sum = 0;
  i = 0;
  if (lseek(fd, 0, SEEK_SET)) {
    fprintf(stderr, "error seeking to begnning of ROM\n");
  }
  while (i < romsize) {
    read(fd, &longword_be, sizeof(uint32_t));
    i += sizeof(uint32_t);
    longword = ntohl(longword_be);
    sum = sum_addc(sum, longword);
  }
  return sum;
}


x86 assembly (clang, -O0):
<sum_compute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
je     11a1 <sum_compute+0x51>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11a6 <sum_compute+0x56>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11f4 <sum_compute+0xa4>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <ntohl@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <sum_addc@plt>
mov    %rax,-0x30(%rbp)
jmp    11a6 <sum_compute+0x56>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sum_compute>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
je     118c <sum_compute+0x3c>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
test   %r14,%r14
je     11d1 <sum_compute+0x81>
lea    0x8(%rsp),%r15
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1050 <read@plt>
add    $0x8,%rbp
mov    0x8(%rsp),%rdi
call   1070 <ntohl@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <sum_addc@plt>
mov    %rax,%rbx
cmp    %r14,%rbp
jb     11a0 <sum_compute+0x50>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"error seeking to begnning of ROM\0A\00"
define i64 @sum_compute(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %3
store i64 %1, i64* %4
store i64 0, i64* %8
store i64 0, i64* %5
%9 = load i32, i32* %3
%10 = load i32, i32* @SEEK_SET
%11 = call i64 @lseek(i32 %9, i32 0, i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %2
br label %17
17: ; preds = %21, %16
%18 = load i64, i64* %5
%19 = load i64, i64* %4
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i32, i32* %3
%23 = call i32 @read(i32 %22, i64* %6, i32 8)
%24 = load i64, i64* %5
%25 = add i64 %24, 8
store i64 %25, i64* %5
%26 = load i64, i64* %6
%27 = call i64 @ntohl(i64 %26)
store i64 %27, i64* %7
%28 = load i64, i64* %8
%29 = load i64, i64* %7
%30 = call i64 @sum_addc(i64 %28, i64 %29)
store i64 %30, i64* %8
br label %17
31: ; preds = %17
%32 = load i64, i64* %8
ret i64 %32
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"error seeking to begnning of ROM\0A\00"
define i64 @sum_compute(i32 %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i32 0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %8, %2
%12 = icmp eq i64 %1, 0
br i1 %12, label %22, label %13
13: ; preds = %11, %13
%14 = phi i64 [ %20, %13 ], [ 0, %11 ]
%15 = phi i64 [ %17, %13 ], [ 0, %11 ]
%16 = call i32 @read(i32 %0, i64* %3, i32 8)
%17 = add i64 %15, 8
%18 = load i64, i64* %3
%19 = tail call i64 @ntohl(i64 %18)
%20 = tail call i64 @sum_addc(i64 %14, i64 %19)
%21 = icmp ult i64 %17, %1
br i1 %21, label %13, label %22
22: ; preds = %13, %11
%23 = phi i64 [ 0, %11 ], [ %20, %13 ]
ret i64 %23
}