#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;
typedef int uint16_t;
struct can_frame {
  int *data;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ init_set_cmd(struct can_frame *);
int /*<<< orphan*/ perror(char *);
int write(int, struct can_frame *, int);
void set_checksum(int s, uint8_t module_id, uint16_t csum) {
  struct can_frame frame;
  init_set_cmd(&frame);
  frame.data[2] = module_id;
  frame.data[3] = 0x03;
  frame.data[4] = (csum >> 8) & 0xFF;
  frame.data[5] = csum & 0xFF;
  frame.data[6] = 0;
  if (write(s, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) {
    perror("write");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<set_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
call   1030 <init_set_cmd@plt>
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rax
movl   $0x3,0xc(%rax)
mov    -0xc(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x10(%rax)
mov    -0xc(%rbp),%ecx
and    $0xff,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x14(%rax)
mov    -0x18(%rbp),%rax
movl   $0x0,0x18(%rax)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cltq
cmp    $0x8,%rax
je     11d0 <set_checksum+0x90>
lea    0xe3f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_checksum>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%r14d
mov    %rsp,%r15
mov    %r15,%rdi
call   1030 <init_set_cmd@plt>
mov    (%rsp),%rax
mov    %ebp,0x8(%rax)
movl   $0x3,0xc(%rax)
movzbl %bh,%ecx
mov    %ecx,0x10(%rax)
movzbl %bl,%ecx
mov    %ecx,0x14(%rax)
movl   $0x0,0x18(%rax)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cmp    $0x8,%eax
jne    119a <set_checksum+0x5a>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.can_frame = type { i32* }
@.str = [6 x i8] c"write\00"
define void @set_checksum(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.can_frame
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = call i32 @init_set_cmd(%struct.can_frame* %7)
%9 = load i32, i32* %5
%10 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%11 = load i32*, i32** %10
%12 = getelementptr inbounds i32, i32* %11, i64 2
store i32 %9, i32* %12
%13 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%14 = load i32*, i32** %13
%15 = getelementptr inbounds i32, i32* %14, i64 3
store i32 3, i32* %15
%16 = load i32, i32* %6
%17 = ashr i32 %16, 8
%18 = and i32 %17, 255
%19 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%20 = load i32*, i32** %19
%21 = getelementptr inbounds i32, i32* %20, i64 4
store i32 %18, i32* %21
%22 = load i32, i32* %6
%23 = and i32 %22, 255
%24 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%25 = load i32*, i32** %24
%26 = getelementptr inbounds i32, i32* %25, i64 5
store i32 %23, i32* %26
%27 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%28 = load i32*, i32** %27
%29 = getelementptr inbounds i32, i32* %28, i64 6
store i32 0, i32* %29
%30 = load i32, i32* %4
%31 = call i32 @write(i32 %30, %struct.can_frame* %7, i32 8)
%32 = sext i32 %31 to i64
%33 = icmp ne i64 %32, 8
br i1 %33, label %34, label %37
34: ; preds = %3
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %3
ret void
}

LLVM IR (-O2):
%struct.can_frame = type { i32* }
@.str = [6 x i8] c"write\00"
define void @set_checksum(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.can_frame
%5 = bitcast %struct.can_frame* %4 to i8*
%6 = call i32 @init_set_cmd(%struct.can_frame* %4)
%7 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i64 0, i32 0
%8 = load i32*, i32** %7
%9 = getelementptr inbounds i32, i32* %8, i64 2
store i32 %1, i32* %9
%10 = getelementptr inbounds i32, i32* %8, i64 3
store i32 3, i32* %10
%11 = lshr i32 %2, 8
%12 = and i32 %11, 255
%13 = getelementptr inbounds i32, i32* %8, i64 4
store i32 %12, i32* %13
%14 = and i32 %2, 255
%15 = getelementptr inbounds i32, i32* %8, i64 5
store i32 %14, i32* %15
%16 = getelementptr inbounds i32, i32* %8, i64 6
store i32 0, i32* %16
%17 = call i32 @write(i32 %0, %struct.can_frame* %4, i32 8)
%18 = icmp eq i32 %17, 8
br i1 %18, label %22, label %19
19: ; preds = %3
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %3
ret void
}