#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 uint8_t;

/* Variables and functions */
int bcd2i(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int i2bcd(int);
int i2c_read_byte(int, int, int *);
int i2c_write_byte(int, int, int);
int /*<<< orphan*/ stdout;
int ds1307_set_hfmt(int file, bool h12) {
  int res;
  uint8_t hrs;
  bool oldh12;
  if ((res = i2c_read_byte(file, (0x02), &hrs)) < 0) {
    return res;
  }
  oldh12 = (hrs & (1 << 6)) ? true : false;
  hrs &= (~(1 << 6));
  if (h12) {
    if (oldh12) {
      fputs("Hour format is already set to 12H\n", stdout);
      return 0;
    } else {
      bool pm = false;
      hrs = bcd2i(hrs);
      if (hrs > 12) {
        hrs -= 12;
        pm = true;
      }
      if (12 == hrs) {
        pm = true;
      }
      if (0 == hrs) {
        hrs = 12;
      }
      hrs = i2bcd(hrs);
      hrs |= (1 << 6);
      if (pm) {
        hrs |= (1 << 5);
      }
    }
  } else {
    if (!oldh12) {
      fputs("Hour format is already set to 24H\n", stdout);
      return 0;
    } else {
      bool pm = hrs & (1 << 5);
      hrs &= (~(1 << 5));
      hrs = bcd2i(hrs);
      if (pm) {
        hrs += 12;
        if (24 == hrs) {
          hrs = 12;
        }
      } else {
        if (12 == hrs) {
          hrs = 0;
        }
      }
      hrs = i2bcd(hrs);
    }
  }
  if ((res = i2c_write_byte(file, (0x02), hrs)) < 0) {
    return res;
  }
  fprintf(stdout, "Hour format set to %s (0x%02x)\n", h12 ? "12H" : "24H", hrs);
  return 0;
}


x86 assembly (clang, -O0):
<ds1307_set_hfmt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
lea    -0x14(%rbp),%rdx
call   1070 <i2c_read_byte@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1196 <ds1307_set_hfmt+0x36>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <ds1307_set_hfmt+0x20b>
mov    -0x14(%rbp),%edx
and    $0x40,%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
and    $0xffffffbf,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     126e <ds1307_set_hfmt+0x10e>
cmpl   $0x0,-0x18(%rbp)
je     11ea <ds1307_set_hfmt+0x8a>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
call   1040 <fputs@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <ds1307_set_hfmt+0x20b>
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <bcd2i@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xc,-0x14(%rbp)
jle    1216 <ds1307_set_hfmt+0xb6>
mov    -0x14(%rbp),%eax
sub    $0xc,%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    $0xc,%eax
cmp    -0x14(%rbp),%eax
jne    122b <ds1307_set_hfmt+0xcb>
movl   $0x1,-0x1c(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jne    123d <ds1307_set_hfmt+0xdd>
movl   $0xc,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <i2bcd@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
or     $0x40,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1264 <ds1307_set_hfmt+0x104>
mov    -0x14(%rbp),%eax
or     $0x20,%eax
mov    %eax,-0x14(%rbp)
jmp    1269 <ds1307_set_hfmt+0x109>
jmp    130d <ds1307_set_hfmt+0x1ad>
cmpl   $0x0,-0x18(%rbp)
jne    1299 <ds1307_set_hfmt+0x139>
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
lea    0xd9b(%rip),%rdi
call   1040 <fputs@plt>
movl   $0x0,-0x4(%rbp)
jmp    136b <ds1307_set_hfmt+0x20b>
mov    -0x14(%rbp),%eax
and    $0x20,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
and    $0xffffffdf,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <bcd2i@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12e3 <ds1307_set_hfmt+0x183>
mov    -0x14(%rbp),%eax
add    $0xc,%eax
mov    %eax,-0x14(%rbp)
mov    $0x18,%eax
cmp    -0x14(%rbp),%eax
jne    12de <ds1307_set_hfmt+0x17e>
movl   $0xc,-0x14(%rbp)
jmp    12fd <ds1307_set_hfmt+0x19d>
mov    $0xc,%eax
cmp    -0x14(%rbp),%eax
jne    12f8 <ds1307_set_hfmt+0x198>
movl   $0x0,-0x14(%rbp)
jmp    12fd <ds1307_set_hfmt+0x19d>
mov    -0x14(%rbp),%edi
call   1060 <i2bcd@plt>
mov    %eax,-0x14(%rbp)
jmp    130d <ds1307_set_hfmt+0x1ad>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    $0x2,%esi
call   1080 <i2c_write_byte@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1334 <ds1307_set_hfmt+0x1d4>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136b <ds1307_set_hfmt+0x20b>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%ecx
lea    0xd23(%rip),%rdx
lea    0xd18(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdx
mov    -0x14(%rbp),%ecx
lea    0xce7(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ds1307_set_hfmt>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xc(%rsp),%rdx
mov    $0x2,%esi
call   1070 <i2c_read_byte@plt>
test   %eax,%eax
js     129b <ds1307_set_hfmt+0x13b>
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
and    $0xffffffbf,%edi
mov    %edi,0xc(%rsp)
test   %r14d,%r14d
je     11db <ds1307_set_hfmt+0x7b>
test   $0x40,%bl
jne    11fa <ds1307_set_hfmt+0x9a>
call   1030 <bcd2i@plt>
lea    -0xc(%rax),%ecx
cmp    $0xd,%eax
cmovl  %eax,%ecx
xor    %edx,%edx
cmp    $0xc,%ecx
sete   %dl
shl    $0x5,%edx
or     $0x40,%edx
cmp    $0xd,%eax
mov    $0x60,%ebx
cmovl  %edx,%ebx
test   %ecx,%ecx
mov    $0xc,%edi
cmovne %ecx,%edi
mov    %edi,0xc(%rsp)
call   1060 <i2bcd@plt>
mov    %eax,%edx
or     %ebx,%edx
jmp    1257 <ds1307_set_hfmt+0xf7>
test   $0x40,%bl
jne    1214 <ds1307_set_hfmt+0xb4>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    0xe33(%rip),%rdi
call   1040 <fputs@plt>
jmp    1299 <ds1307_set_hfmt+0x139>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    0xdf6(%rip),%rdi
call   1040 <fputs@plt>
jmp    1299 <ds1307_set_hfmt+0x139>
mov    %ebx,%edi
and    $0xffffff9f,%edi
mov    %edi,0xc(%rsp)
call   1030 <bcd2i@plt>
mov    %eax,0xc(%rsp)
test   $0x20,%bl
jne    123c <ds1307_set_hfmt+0xdc>
cmp    $0xc,%eax
jne    124e <ds1307_set_hfmt+0xee>
movl   $0x0,0xc(%rsp)
xor    %eax,%eax
jmp    124e <ds1307_set_hfmt+0xee>
add    $0xc,%eax
cmp    $0x18,%eax
mov    $0xc,%ecx
cmove  %ecx,%eax
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   1060 <i2bcd@plt>
mov    %eax,%edx
mov    %edx,0xc(%rsp)
mov    %ebp,%edi
mov    $0x2,%esi
call   1080 <i2c_write_byte@plt>
test   %eax,%eax
js     129b <ds1307_set_hfmt+0x13b>
test   %r14d,%r14d
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rax
lea    0xde1(%rip),%rdx
cmove  %rax,%rdx
mov    0xc(%rsp),%ecx
lea    0xdb2(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Hour format is already set to 12H\0A\00"
@stdout = global i32 0
@.str.1 = [35 x i8] c"Hour format is already set to 24H\0A\00"
@.str.2 = [32 x i8] c"Hour format set to %s (0x%02x)\0A\00"
@.str.3 = [4 x i8] c"12H\00"
@.str.4 = [4 x i8] c"24H\00"
define i32 @ds1307_set_hfmt(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = load i32, i32* %4
%12 = call i32 @i2c_read_byte(i32 %11, i32 2, i32* %7)
store i32 %12, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %6
store i32 %15, i32* %3
br label %106
16: ; preds = %2
%17 = load i32, i32* %7
%18 = and i32 %17, 64
%19 = icmp ne i32 %18, 0
%20 = zext i1 %19 to i64
%21 = select i1 %19, i32 1, i32 0
store i32 %21, i32* %8
%22 = load i32, i32* %7
%23 = and i32 %22, -65
store i32 %23, i32* %7
%24 = load i32, i32* %5
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %60
26: ; preds = %16
%27 = load i32, i32* %8
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stdout
%31 = call i32 @fputs(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %30)
store i32 0, i32* %3
br label %106
32: ; preds = %26
store i32 0, i32* %9
%33 = load i32, i32* %7
%34 = call i32 @bcd2i(i32 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp sgt i32 %35, 12
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* %7
%39 = sub nsw i32 %38, 12
store i32 %39, i32* %7
store i32 1, i32* %9
br label %40
40: ; preds = %37, %32
%41 = load i32, i32* %7
%42 = icmp eq i32 12, %41
br i1 %42, label %43, label %44
43: ; preds = %40
store i32 1, i32* %9
br label %44
44: ; preds = %43, %40
%45 = load i32, i32* %7
%46 = icmp eq i32 0, %45
br i1 %46, label %47, label %48
47: ; preds = %44
store i32 12, i32* %7
br label %48
48: ; preds = %47, %44
%49 = load i32, i32* %7
%50 = call i32 @i2bcd(i32 %49)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = or i32 %51, 64
store i32 %52, i32* %7
%53 = load i32, i32* %9
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = load i32, i32* %7
%57 = or i32 %56, 32
store i32 %57, i32* %7
br label %58
58: ; preds = %55, %48
br label %59
59: ; preds = %58
br label %91
60: ; preds = %16
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %66, label %63
63: ; preds = %60
%64 = load i32, i32* @stdout
%65 = call i32 @fputs(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %64)
store i32 0, i32* %3
br label %106
66: ; preds = %60
%67 = load i32, i32* %7
%68 = and i32 %67, 32
store i32 %68, i32* %10
%69 = load i32, i32* %7
%70 = and i32 %69, -33
store i32 %70, i32* %7
%71 = load i32, i32* %7
%72 = call i32 @bcd2i(i32 %71)
store i32 %72, i32* %7
%73 = load i32, i32* %10
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %82
75: ; preds = %66
%76 = load i32, i32* %7
%77 = add nsw i32 %76, 12
store i32 %77, i32* %7
%78 = load i32, i32* %7
%79 = icmp eq i32 24, %78
br i1 %79, label %80, label %81
80: ; preds = %75
store i32 12, i32* %7
br label %81
81: ; preds = %80, %75
br label %87
82: ; preds = %66
%83 = load i32, i32* %7
%84 = icmp eq i32 12, %83
br i1 %84, label %85, label %86
85: ; preds = %82
store i32 0, i32* %7
br label %86
86: ; preds = %85, %82
br label %87
87: ; preds = %86, %81
%88 = load i32, i32* %7
%89 = call i32 @i2bcd(i32 %88)
store i32 %89, i32* %7
br label %90
90: ; preds = %87
br label %91
91: ; preds = %90, %59
%92 = load i32, i32* %4
%93 = load i32, i32* %7
%94 = call i32 @i2c_write_byte(i32 %92, i32 2, i32 %93)
store i32 %94, i32* %6
%95 = icmp slt i32 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %91
%97 = load i32, i32* %6
store i32 %97, i32* %3
br label %106
98: ; preds = %91
%99 = load i32, i32* @stdout
%100 = load i32, i32* %5
%101 = icmp ne i32 %100, 0
%102 = zext i1 %101 to i64
%103 = select i1 %101, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0)
%104 = load i32, i32* %7
%105 = call i32 @fprintf(i32 %99, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %103, i32 %104)
store i32 0, i32* %3
br label %106
106: ; preds = %98, %96, %63, %29, %14
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@.str = [35 x i8] c"Hour format is already set to 12H\0A\00"
@stdout = global i32 0
@.str.1 = [35 x i8] c"Hour format is already set to 24H\0A\00"
@.str.2 = [32 x i8] c"Hour format set to %s (0x%02x)\0A\00"
@.str.3 = [4 x i8] c"12H\00"
@.str.4 = [4 x i8] c"24H\00"
define i32 @ds1307_set_hfmt(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @i2c_read_byte(i32 %0, i32 2, i32* %3)
%6 = icmp slt i32 %5, 0
br i1 %6, label %57, label %7
7: ; preds = %2
%8 = load i32, i32* %3
%9 = and i32 %8, 64
%10 = icmp eq i32 %9, 0
%11 = and i32 %8, -65
store i32 %11, i32* %3
%12 = icmp eq i32 %1, 0
br i1 %12, label %29, label %13
13: ; preds = %7
br i1 %10, label %17, label %14
14: ; preds = %13
%15 = load i32, i32* @stdout
%16 = call i32 @fputs(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %57
17: ; preds = %13
%18 = call i32 @bcd2i(i32 %11)
%19 = icmp sgt i32 %18, 12
%20 = add nsw i32 %18, -12
%21 = select i1 %19, i32 %20, i32 %18
%22 = icmp eq i32 %21, 12
%23 = or i1 %19, %22
%24 = icmp eq i32 %21, 0
%25 = select i1 %24, i32 12, i32 %21
store i32 %25, i32* %3
%26 = call i32 @i2bcd(i32 %25)
%27 = select i1 %23, i32 96, i32 64
%28 = or i32 %27, %26
br label %48
29: ; preds = %7
br i1 %10, label %30, label %33
30: ; preds = %29
%31 = load i32, i32* @stdout
%32 = call i32 @fputs(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %57
33: ; preds = %29
%34 = and i32 %8, 32
%35 = and i32 %8, -97
store i32 %35, i32* %3
%36 = call i32 @bcd2i(i32 %35)
store i32 %36, i32* %3
%37 = icmp eq i32 %34, 0
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = add nsw i32 %36, 12
%40 = icmp eq i32 %39, 24
%41 = select i1 %40, i32 12, i32 %39
store i32 %41, i32* %3
br label %45
42: ; preds = %33
%43 = icmp eq i32 %36, 12
br i1 %43, label %44, label %45
44: ; preds = %42
store i32 0, i32* %3
br label %45
45: ; preds = %42, %44, %38
%46 = phi i32 [ %36, %42 ], [ 0, %44 ], [ %41, %38 ]
%47 = call i32 @i2bcd(i32 %46)
br label %48
48: ; preds = %45, %17
%49 = phi i32 [ %28, %17 ], [ %47, %45 ]
store i32 %49, i32* %3
%50 = call i32 @i2c_write_byte(i32 %0, i32 2, i32 %49)
%51 = icmp slt i32 %50, 0
br i1 %51, label %57, label %52
52: ; preds = %48
%53 = load i32, i32* @stdout
%54 = select i1 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0)
%55 = load i32, i32* %3
%56 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %54, i32 %55)
br label %57
57: ; preds = %48, %2, %52, %30, %14
%58 = phi i32 [ 0, %14 ], [ 0, %52 ], [ 0, %30 ], [ %5, %2 ], [ %50, %48 ]
ret i32 %58
}