#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 < 0) {
    printf("error: i is negetive\n");
    return x;
  }
  if (i > sizeof(unsigned) - 1) {
    printf("error: too big i");
    return x;
  }
  unsigned mask = ((unsigned)0xFF) << (i << 3);
  unsigned pos_byte = ((unsigned)b) << (i << 3);
  return (x & ~mask) | pos_byte;
}


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   $0x0,-0xc(%rbp)
jge    1144 <replace_byte+0x34>
lea    0xecc(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1199 <replace_byte+0x89>
movslq -0xc(%rbp),%rax
cmp    $0x3,%rax
jbe    1169 <replace_byte+0x59>
lea    0xebd(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1199 <replace_byte+0x89>
mov    -0xc(%rbp),%ecx
shl    $0x3,%ecx
mov    $0xff,%eax
shl    %cl,%eax
mov    %eax,-0x14(%rbp)
movzbl -0xd(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x14(%rbp),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
or     -0x18(%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
test   %esi,%esi
js     113a <replace_byte+0x1a>
cmp    $0x4,%esi
jb     1148 <replace_byte+0x28>
lea    0xecd(%rip),%rdi
call   1040 <printf@plt>
jmp    1161 <replace_byte+0x41>
lea    0xed0(%rip),%rdi
call   1030 <puts@plt>
jmp    1161 <replace_byte+0x41>
shl    $0x3,%esi
mov    $0xff,%eax
mov    %esi,%ecx
shl    %cl,%eax
movzbl %dl,%edx
shl    %cl,%edx
not    %eax
and    %ebx,%eax
or     %edx,%eax
mov    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"error: i is negetive\0A\00"
@.str.1 = [17 x i8] c"error: too big i\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
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8 %2, i8* %7
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %5
store i32 %14, i32* %4
br label %37
15: ; preds = %3
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = icmp ugt i64 %17, 3
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* %5
store i32 %21, i32* %4
br label %37
22: ; preds = %15
%23 = load i32, i32* %6
%24 = shl i32 %23, 3
%25 = shl i32 255, %24
store i32 %25, i32* %8
%26 = load i8, i8* %7
%27 = zext i8 %26 to i32
%28 = load i32, i32* %6
%29 = shl i32 %28, 3
%30 = shl i32 %27, %29
store i32 %30, i32* %9
%31 = load i32, i32* %5
%32 = load i32, i32* %8
%33 = xor i32 %32, -1
%34 = and i32 %31, %33
%35 = load i32, i32* %9
%36 = or i32 %34, %35
store i32 %36, i32* %4
br label %37
37: ; preds = %22, %19, %12
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"error: too big i\00"
@str = [21 x i8] c"error: i is negetive\00"
define i32 @replace_byte(i32 %0, i32 %1, i8 zeroext %2) {
%4 = icmp slt i32 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %19
7: ; preds = %3
%8 = icmp ugt i32 %1, 3
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %19
11: ; preds = %7
%12 = shl nuw nsw i32 %1, 3
%13 = shl nuw i32 255, %12
%14 = zext i8 %2 to i32
%15 = shl nuw i32 %14, %12
%16 = xor i32 %13, -1
%17 = and i32 %16, %0
%18 = or i32 %17, %15
br label %19
19: ; preds = %11, %9, %5
%20 = phi i32 [ %0, %5 ], [ %0, %9 ], [ %18, %11 ]
ret i32 %20
}