#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 */
int /*<<< orphan*/ eeprom_delay(int);
int eeprom_put_word(unsigned long, unsigned short);
int eeprom_send_addr(unsigned long, int);
int eeprom_send_start(unsigned long, int);
int eeprom_write_disable(unsigned long);
int eeprom_write_enable(unsigned long);
int get_sda_line(unsigned long);
int eeprom_write(unsigned long pci_base, int eeprom_addr,
                 unsigned short *p_data, int nwords) {
  int status;
  int i;
  int check_cntr;
  unsigned short data;
  if ((eeprom_addr + nwords) > 64)
    return (3);
  if ((status = eeprom_write_enable(pci_base)) != 0)
    return (status);
  for (i = 0; i < nwords; i++, eeprom_addr++) {
    (*(unsigned char *)(pci_base + 0x0e) |= (1 << 1));
    eeprom_delay(50);
    if (((status = eeprom_send_start(pci_base, 1)) != 0) ||
        ((status = eeprom_send_addr(pci_base, eeprom_addr)) != 0))
      return (status);
    data = *p_data++;
    if ((status = eeprom_put_word(pci_base, data)) != 0)
      return (status);
    (*(unsigned char *)(pci_base + 0x0e) &= ~(1 << 1));
    eeprom_delay(100);
    (*(unsigned char *)(pci_base + 0x0e) |= (1 << 1));
    check_cntr = 0;
    while (1) {
      check_cntr++;
      if (get_sda_line(pci_base) == 1)
        break;
      if (check_cntr > 100000) {
        (*(unsigned char *)(pci_base + 0x0e) &= ~(1 << 1));
        eeprom_delay(100);
        return (1);
      }
    }
    (*(unsigned char *)(pci_base + 0x0e) &= ~(1 << 1));
    eeprom_delay(100);
  }
  if ((status = eeprom_write_disable(pci_base)) != 0)
    return (status);
  return (0);
}


x86 assembly (clang, -O0):
<eeprom_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x14(%rbp),%eax
add    -0x24(%rbp),%eax
cmp    $0x40,%eax
jle    11a1 <eeprom_write+0x31>
movl   $0x3,-0x4(%rbp)
jmp    133c <eeprom_write+0x1cc>
mov    -0x10(%rbp),%rdi
call   1060 <eeprom_write_enable@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     11c1 <eeprom_write+0x51>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <eeprom_write+0x1cc>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1315 <eeprom_write+0x1a5>
mov    -0x10(%rbp),%rax
movzbl 0xe(%rax),%ecx
or     $0x2,%ecx
mov    %cl,0xe(%rax)
mov    $0x32,%edi
call   1090 <eeprom_delay@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1040 <eeprom_send_start@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    121e <eeprom_write+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <eeprom_send_addr@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     1229 <eeprom_write+0xb9>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <eeprom_write+0x1cc>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x2,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%ax
mov    %ax,-0x32(%rbp)
mov    -0x10(%rbp),%rdi
movzwl -0x32(%rbp),%esi
call   1080 <eeprom_put_word@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     1263 <eeprom_write+0xf3>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <eeprom_write+0x1cc>
mov    -0x10(%rbp),%rax
movzbl 0xe(%rax),%ecx
and    $0xfffffffd,%ecx
mov    %cl,0xe(%rax)
mov    $0x64,%edi
call   1090 <eeprom_delay@plt>
mov    -0x10(%rbp),%rax
movzbl 0xe(%rax),%ecx
or     $0x2,%ecx
mov    %cl,0xe(%rax)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <get_sda_line@plt>
cmp    $0x1,%eax
jne    12b0 <eeprom_write+0x140>
jmp    12e6 <eeprom_write+0x176>
cmpl   $0x186a0,-0x30(%rbp)
jle    12e1 <eeprom_write+0x171>
mov    -0x10(%rbp),%rax
movzbl 0xe(%rax),%ecx
and    $0xfffffffd,%ecx
mov    %cl,0xe(%rax)
mov    $0x64,%edi
call   1090 <eeprom_delay@plt>
movl   $0x1,-0x4(%rbp)
jmp    133c <eeprom_write+0x1cc>
jmp    1290 <eeprom_write+0x120>
mov    -0x10(%rbp),%rax
movzbl 0xe(%rax),%ecx
and    $0xfffffffd,%ecx
mov    %cl,0xe(%rax)
mov    $0x64,%edi
call   1090 <eeprom_delay@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c8 <eeprom_write+0x58>
mov    -0x10(%rbp),%rdi
call   1050 <eeprom_write_disable@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     1335 <eeprom_write+0x1c5>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    133c <eeprom_write+0x1cc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eeprom_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %esi,%r12d
lea    (%r14,%r12,1),%ecx
mov    $0x3,%eax
cmp    $0x40,%ecx
jg     127c <eeprom_write+0x10c>
mov    %rdx,%r15
mov    %rdi,%rbx
call   1060 <eeprom_write_enable@plt>
test   %eax,%eax
jne    127c <eeprom_write+0x10c>
test   %r14d,%r14d
jle    1253 <eeprom_write+0xe3>
xor    %r13d,%r13d
orb    $0x2,0xe(%rbx)
mov    $0x32,%edi
call   1090 <eeprom_delay@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <eeprom_send_start@plt>
test   %eax,%eax
jne    127c <eeprom_write+0x10c>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <eeprom_send_addr@plt>
test   %eax,%eax
jne    127c <eeprom_write+0x10c>
movzwl (%r15),%esi
mov    %rbx,%rdi
call   1080 <eeprom_put_word@plt>
test   %eax,%eax
jne    127c <eeprom_write+0x10c>
add    $0x2,%r15
andb   $0xfd,0xe(%rbx)
mov    $0x64,%edi
call   1090 <eeprom_delay@plt>
orb    $0x2,0xe(%rbx)
mov    $0x186a1,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <get_sda_line@plt>
cmp    $0x1,%eax
je     1234 <eeprom_write+0xc4>
add    $0xffffffff,%ebp
jne    1220 <eeprom_write+0xb0>
jmp    1269 <eeprom_write+0xf9>
andb   $0xfd,0xe(%rbx)
mov    $0x64,%edi
call   1090 <eeprom_delay@plt>
add    $0x1,%r13d
add    $0x1,%r12d
cmp    %r14d,%r13d
jne    11b2 <eeprom_write+0x42>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <eeprom_write_disable@plt>
andb   $0xfd,0xe(%rbx)
mov    $0x64,%edi
call   1090 <eeprom_delay@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @eeprom_write(i64 %0, i32 %1, i16* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i16*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i16
store i64 %0, i64* %6
store i32 %1, i32* %7
store i16* %2, i16** %8
store i32 %3, i32* %9
%14 = load i32, i32* %7
%15 = load i32, i32* %9
%16 = add nsw i32 %14, %15
%17 = icmp sgt i32 %16, 64
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 3, i32* %5
br label %116
19: ; preds = %4
%20 = load i64, i64* %6
%21 = call i32 @eeprom_write_enable(i64 %20)
store i32 %21, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i32, i32* %10
store i32 %24, i32* %5
br label %116
25: ; preds = %19
store i32 0, i32* %11
br label %26
26: ; preds = %104, %25
%27 = load i32, i32* %11
%28 = load i32, i32* %9
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %109
30: ; preds = %26
%31 = load i64, i64* %6
%32 = add i64 %31, 14
%33 = inttoptr i64 %32 to i8*
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = or i32 %35, 2
%37 = trunc i32 %36 to i8
store i8 %37, i8* %33
%38 = call i32 @eeprom_delay(i32 50)
%39 = load i64, i64* %6
%40 = call i32 @eeprom_send_start(i64 %39, i32 1)
store i32 %40, i32* %10
%41 = icmp ne i32 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %30
%43 = load i64, i64* %6
%44 = load i32, i32* %7
%45 = call i32 @eeprom_send_addr(i64 %43, i32 %44)
store i32 %45, i32* %10
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42, %30
%48 = load i32, i32* %10
store i32 %48, i32* %5
br label %116
49: ; preds = %42
%50 = load i16*, i16** %8
%51 = getelementptr inbounds i16, i16* %50, i32 1
store i16* %51, i16** %8
%52 = load i16, i16* %50
store i16 %52, i16* %13
%53 = load i64, i64* %6
%54 = load i16, i16* %13
%55 = call i32 @eeprom_put_word(i64 %53, i16 zeroext %54)
store i32 %55, i32* %10
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = load i32, i32* %10
store i32 %58, i32* %5
br label %116
59: ; preds = %49
%60 = load i64, i64* %6
%61 = add i64 %60, 14
%62 = inttoptr i64 %61 to i8*
%63 = load i8, i8* %62
%64 = zext i8 %63 to i32
%65 = and i32 %64, -3
%66 = trunc i32 %65 to i8
store i8 %66, i8* %62
%67 = call i32 @eeprom_delay(i32 100)
%68 = load i64, i64* %6
%69 = add i64 %68, 14
%70 = inttoptr i64 %69 to i8*
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
%73 = or i32 %72, 2
%74 = trunc i32 %73 to i8
store i8 %74, i8* %70
store i32 0, i32* %12
br label %75
75: ; preds = %59, %94
%76 = load i32, i32* %12
%77 = add nsw i32 %76, 1
store i32 %77, i32* %12
%78 = load i64, i64* %6
%79 = call i32 @get_sda_line(i64 %78)
%80 = icmp eq i32 %79, 1
br i1 %80, label %81, label %82
81: ; preds = %75
br label %95
82: ; preds = %75
%83 = load i32, i32* %12
%84 = icmp sgt i32 %83, 100000
br i1 %84, label %85, label %94
85: ; preds = %82
%86 = load i64, i64* %6
%87 = add i64 %86, 14
%88 = inttoptr i64 %87 to i8*
%89 = load i8, i8* %88
%90 = zext i8 %89 to i32
%91 = and i32 %90, -3
%92 = trunc i32 %91 to i8
store i8 %92, i8* %88
%93 = call i32 @eeprom_delay(i32 100)
store i32 1, i32* %5
br label %116
94: ; preds = %82
br label %75
95: ; preds = %81
%96 = load i64, i64* %6
%97 = add i64 %96, 14
%98 = inttoptr i64 %97 to i8*
%99 = load i8, i8* %98
%100 = zext i8 %99 to i32
%101 = and i32 %100, -3
%102 = trunc i32 %101 to i8
store i8 %102, i8* %98
%103 = call i32 @eeprom_delay(i32 100)
br label %104
104: ; preds = %95
%105 = load i32, i32* %11
%106 = add nsw i32 %105, 1
store i32 %106, i32* %11
%107 = load i32, i32* %7
%108 = add nsw i32 %107, 1
store i32 %108, i32* %7
br label %26
109: ; preds = %26
%110 = load i64, i64* %6
%111 = call i32 @eeprom_write_disable(i64 %110)
store i32 %111, i32* %10
%112 = icmp ne i32 %111, 0
br i1 %112, label %113, label %115
113: ; preds = %109
%114 = load i32, i32* %10
store i32 %114, i32* %5
br label %116
115: ; preds = %109
store i32 0, i32* %5
br label %116
116: ; preds = %115, %113, %85, %57, %47, %23, %18
%117 = load i32, i32* %5
ret i32 %117
}

LLVM IR (-O2):
define i32 @eeprom_write(i64 %0, i32 %1, i16* nocapture %2, i32 %3) {
%5 = add nsw i32 %3, %1
%6 = icmp sgt i32 %5, 64
br i1 %6, label %58, label %7
7: ; preds = %4
%8 = tail call i32 @eeprom_write_enable(i64 %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %58
10: ; preds = %7
%11 = icmp sgt i32 %3, 0
br i1 %11, label %12, label %56
12: ; preds = %10
%13 = add i64 %0, 14
%14 = inttoptr i64 %13 to i8*
br label %15
15: ; preds = %12, %49
%16 = phi i32 [ 0, %12 ], [ %53, %49 ]
%17 = phi i32 [ %1, %12 ], [ %54, %49 ]
%18 = phi i16* [ %2, %12 ], [ %28, %49 ]
%19 = load i8, i8* %14
%20 = or i8 %19, 2
store i8 %20, i8* %14
%21 = tail call i32 @eeprom_delay(i32 50)
%22 = tail call i32 @eeprom_send_start(i64 %0, i32 1)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %58
24: ; preds = %15
%25 = tail call i32 @eeprom_send_addr(i64 %0, i32 %17)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %58
27: ; preds = %24
%28 = getelementptr inbounds i16, i16* %18, i64 1
%29 = load i16, i16* %18
%30 = tail call i32 @eeprom_put_word(i64 %0, i16 zeroext %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %58
32: ; preds = %27
%33 = load i8, i8* %14
%34 = and i8 %33, -3
store i8 %34, i8* %14
%35 = tail call i32 @eeprom_delay(i32 100)
%36 = load i8, i8* %14
%37 = or i8 %36, 2
store i8 %37, i8* %14
br label %38
38: ; preds = %42, %32
%39 = phi i32 [ 0, %32 ], [ %43, %42 ]
%40 = tail call i32 @get_sda_line(i64 %0)
%41 = icmp eq i32 %40, 1
br i1 %41, label %49, label %42
42: ; preds = %38
%43 = add nuw nsw i32 %39, 1
%44 = icmp eq i32 %43, 100001
br i1 %44, label %45, label %38
45: ; preds = %42
%46 = load i8, i8* %14
%47 = and i8 %46, -3
store i8 %47, i8* %14
%48 = tail call i32 @eeprom_delay(i32 100)
br label %58
49: ; preds = %38
%50 = load i8, i8* %14
%51 = and i8 %50, -3
store i8 %51, i8* %14
%52 = tail call i32 @eeprom_delay(i32 100)
%53 = add nuw nsw i32 %16, 1
%54 = add nsw i32 %17, 1
%55 = icmp eq i32 %53, %3
br i1 %55, label %56, label %15
56: ; preds = %49, %10
%57 = tail call i32 @eeprom_write_disable(i64 %0)
br label %58
58: ; preds = %27, %15, %24, %56, %7, %4, %45
%59 = phi i32 [ 1, %45 ], [ 3, %4 ], [ %8, %7 ], [ %57, %56 ], [ %30, %27 ], [ %25, %24 ], [ %22, %15 ]
ret i32 %59
}