#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 */
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(unsigned short *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void write_calibration(int cal[4][2]) {
  unsigned short buf[16];
  int i, j, k = 0;
  FILE *f = fopen("/etc/ADC-calibration.dat", "w");
  for (j = 0; j < 2; j++)
    for (i = 0; i < 4; i++) {
      buf[k] = (unsigned short)cal[i][j];
      k++;
    }
  if (!f)
    goto unwrite_calibration;
  if (fwrite(buf, 4 * 4, 1, f) == 1)
    return;
unwrite_calibration:
  printf("Problem writing /etc/ADC-calibration.dat: %m\n");
}


x86 assembly (clang, -O0):
<write_calibration>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    0xeb6(%rip),%rdi
lea    0xec8(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x2,-0x38(%rbp)
jge    11c8 <write_calibration+0x98>
movl   $0x0,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
jge    11b5 <write_calibration+0x85>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %ax,%cx
movslq -0x3c(%rbp),%rax
mov    %cx,-0x30(%rbp,%rax,2)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1172 <write_calibration+0x42>
jmp    11ba <write_calibration+0x8a>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1161 <write_calibration+0x31>
cmpq   $0x0,-0x48(%rbp)
jne    11d8 <write_calibration+0xa8>
jmp    1202 <write_calibration+0xd2>
lea    -0x30(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    $0x10,%esi
mov    $0x1,%edx
call   1050 <fwrite@plt>
cmp    $0x1,%eax
jne    11fd <write_calibration+0xcd>
jmp    120e <write_calibration+0xde>
jmp    1202 <write_calibration+0xd2>
lea    0xe12(%rip),%rdi
call   1030 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_calibration>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
lea    0xec1(%rip),%rdi
lea    0xed3(%rip),%rsi
call   1040 <fopen@plt>
movzwl (%rbx),%ecx
mov    %cx,(%rsp)
movzwl 0x8(%rbx),%ecx
mov    %cx,0x2(%rsp)
movzwl 0x10(%rbx),%ecx
mov    %cx,0x4(%rsp)
movzwl 0x18(%rbx),%ecx
mov    %cx,0x6(%rsp)
movzwl 0x4(%rbx),%ecx
mov    %cx,0x8(%rsp)
movzwl 0xc(%rbx),%ecx
mov    %cx,0xa(%rsp)
movzwl 0x14(%rbx),%ecx
mov    %cx,0xc(%rsp)
movzwl 0x1c(%rbx),%ecx
mov    %cx,0xe(%rsp)
test   %rax,%rax
je     11b6 <write_calibration+0x86>
mov    %rsp,%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fwrite@plt>
cmp    $0x1,%eax
jne    11b6 <write_calibration+0x86>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe5e(%rip),%rdi
add    $0x20,%rsp
pop    %rbx
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"/etc/ADC-calibration.dat\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [46 x i8] c"Problem writing /etc/ADC-calibration.dat: %m\0A\00"
define void @write_calibration([2 x i32]* %0) {
%2 = alloca [2 x i32]*
%3 = alloca [16 x i16]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store [2 x i32]* %0, [2 x i32]** %2
store i32 0, i32* %6
%8 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %7
store i32 0, i32* %5
br label %9
9: ; preds = %35, %1
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %38
12: ; preds = %9
store i32 0, i32* %4
br label %13
13: ; preds = %31, %12
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 4
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = load [2 x i32]*, [2 x i32]** %2
%18 = load i32, i32* %4
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %17, i64 %19
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %20, i64 0, i64 %22
%24 = load i32, i32* %23
%25 = trunc i32 %24 to i16
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [16 x i16], [16 x i16]* %3, i64 0, i64 %27
store i16 %25, i16* %28
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
store i32 %30, i32* %6
br label %31
31: ; preds = %16
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %13
34: ; preds = %13
br label %35
35: ; preds = %34
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %9
38: ; preds = %9
%39 = load i32*, i32** %7
%40 = icmp ne i32* %39, null
br i1 %40, label %42, label %41
41: ; preds = %38
br label %49
42: ; preds = %38
%43 = getelementptr inbounds [16 x i16], [16 x i16]* %3, i64 0, i64 0
%44 = load i32*, i32** %7
%45 = call i32 @fwrite(i16* %43, i32 16, i32 1, i32* %44)
%46 = icmp eq i32 %45, 1
br i1 %46, label %47, label %48
47: ; preds = %42
br label %51
48: ; preds = %42
br label %49
49: ; preds = %48, %41
%50 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %47
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"/etc/ADC-calibration.dat\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [46 x i8] c"Problem writing /etc/ADC-calibration.dat: %m\0A\00"
define void @write_calibration([2 x i32]* nocapture %0) {
%2 = alloca [16 x i16]
%3 = bitcast [16 x i16]* %2 to i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %0, i64 0, i64 0
%6 = load i32, i32* %5
%7 = trunc i32 %6 to i16
%8 = getelementptr inbounds [16 x i16], [16 x i16]* %2, i64 0, i64 0
store i16 %7, i16* %8
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %0, i64 1, i64 0
%10 = load i32, i32* %9
%11 = trunc i32 %10 to i16
%12 = getelementptr inbounds [16 x i16], [16 x i16]* %2, i64 0, i64 1
store i16 %11, i16* %12
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %0, i64 2, i64 0
%14 = load i32, i32* %13
%15 = trunc i32 %14 to i16
%16 = getelementptr inbounds [16 x i16], [16 x i16]* %2, i64 0, i64 2
store i16 %15, i16* %16
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %0, i64 3, i64 0
%18 = load i32, i32* %17
%19 = trunc i32 %18 to i16
%20 = getelementptr inbounds [16 x i16], [16 x i16]* %2, i64 0, i64 3
store i16 %19, i16* %20
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %0, i64 0, i64 1
%22 = load i32, i32* %21
%23 = trunc i32 %22 to i16
%24 = getelementptr inbounds [16 x i16], [16 x i16]* %2, i64 0, i64 4
store i16 %23, i16* %24
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %0, i64 1, i64 1
%26 = load i32, i32* %25
%27 = trunc i32 %26 to i16
%28 = getelementptr inbounds [16 x i16], [16 x i16]* %2, i64 0, i64 5
store i16 %27, i16* %28
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %0, i64 2, i64 1
%30 = load i32, i32* %29
%31 = trunc i32 %30 to i16
%32 = getelementptr inbounds [16 x i16], [16 x i16]* %2, i64 0, i64 6
store i16 %31, i16* %32
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %0, i64 3, i64 1
%34 = load i32, i32* %33
%35 = trunc i32 %34 to i16
%36 = getelementptr inbounds [16 x i16], [16 x i16]* %2, i64 0, i64 7
store i16 %35, i16* %36
%37 = icmp eq i32* %4, null
br i1 %37, label %41, label %38
38: ; preds = %1
%39 = call i32 @fwrite(i16* %8, i32 16, i32 1, i32* %4)
%40 = icmp eq i32 %39, 1
br i1 %40, label %43, label %41
41: ; preds = %38, %1
%42 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %38, %41
ret void
}