#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*/ printf(char *);
unsigned replace_byte(unsigned x, int i, unsigned char b) {
  if (i > 3) {
    printf("Error: Out of bounds [i]\n");
    return x;
  }
  int shift = (24 - (8 * i));
  unsigned x1 = (x << shift) >> (shift + (i * 8)) << (i * 8);
  unsigned x2 = b << (i * 8);
  x = (x - x1) + x2;
  return x;
}


x86 assembly (clang, -O0):
<replace_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
cmpl   $0x3,-0xc(%rbp)
jle    1144 <replace_byte+0x34>
lea    0xecc(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1195 <replace_byte+0x85>
mov    -0xc(%rbp),%ecx
shl    $0x3,%ecx
mov    $0x18,%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    %cl,%eax
mov    -0x14(%rbp),%ecx
mov    -0xc(%rbp),%edx
shl    $0x3,%edx
add    %edx,%ecx
shr    %cl,%eax
mov    -0xc(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
movzbl -0xd(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_byte>:
push   %rbx
mov    %edi,%ebx
cmp    $0x4,%esi
jl     1126 <replace_byte+0x16>
lea    0xee1(%rip),%rdi
call   1030 <puts@plt>
jmp    1144 <replace_byte+0x34>
shl    $0x3,%esi
mov    $0x18,%cl
sub    %sil,%cl
mov    %ebx,%eax
shl    %cl,%eax
shr    $0x18,%eax
mov    %esi,%ecx
shl    %cl,%eax
movzbl %dl,%edx
shl    %cl,%edx
add    %ebx,%edx
sub    %eax,%edx
mov    %edx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Error: Out of bounds [i]\0A\00"
define i32 @replace_byte(i32 %0, i32 %1, i8 zeroext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8 %2, i8* %7
%11 = load i32, i32* %6
%12 = icmp sgt i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* %5
store i32 %15, i32* %4
br label %42
16: ; preds = %3
%17 = load i32, i32* %6
%18 = mul nsw i32 8, %17
%19 = sub nsw i32 24, %18
store i32 %19, i32* %8
%20 = load i32, i32* %5
%21 = load i32, i32* %8
%22 = shl i32 %20, %21
%23 = load i32, i32* %8
%24 = load i32, i32* %6
%25 = mul nsw i32 %24, 8
%26 = add nsw i32 %23, %25
%27 = lshr i32 %22, %26
%28 = load i32, i32* %6
%29 = mul nsw i32 %28, 8
%30 = shl i32 %27, %29
store i32 %30, i32* %9
%31 = load i8, i8* %7
%32 = zext i8 %31 to i32
%33 = load i32, i32* %6
%34 = mul nsw i32 %33, 8
%35 = shl i32 %32, %34
store i32 %35, i32* %10
%36 = load i32, i32* %5
%37 = load i32, i32* %9
%38 = sub i32 %36, %37
%39 = load i32, i32* %10
%40 = add i32 %38, %39
store i32 %40, i32* %5
%41 = load i32, i32* %5
store i32 %41, i32* %4
br label %42
42: ; preds = %16, %13
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@str = [25 x i8] c"Error: Out of bounds [i]\00"
define i32 @replace_byte(i32 %0, i32 %1, i8 zeroext %2) {
%4 = icmp sgt i32 %1, 3
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %17
7: ; preds = %3
%8 = shl nsw i32 %1, 3
%9 = sub nsw i32 24, %8
%10 = shl i32 %0, %9
%11 = lshr i32 %10, 24
%12 = shl i32 %11, %8
%13 = zext i8 %2 to i32
%14 = shl i32 %13, %8
%15 = add i32 %14, %0
%16 = sub i32 %15, %12
br label %17
17: ; preds = %7, %5
%18 = phi i32 [ %0, %5 ], [ %16, %7 ]
ret i32 %18
}