#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*/ new;
typedef int loff_t;

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ closedev(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int opendev(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int strtoul(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage();
int write(int, unsigned int *, int);
int main(int argc, char *argv[]) {
  int fd;
  unsigned new;
  loff_t addr;
  if (argc != 3) {
    usage();
    return EINVAL;
  }
  if ((fd = opendev(O_RDWR)) < 0) {
    return fd;
  }
  addr = strtoul(argv[1], NULL, 16) & 0xffffffff;
  new = strtoul(argv[2], NULL, 16);
  lseek(fd, addr, SEEK_SET);
  if (write(fd, &new, sizeof(new)) != sizeof(new)) {
    perror("write");
    closedev(fd);
    return -1;
  }
  closedev(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a8 <main+0x38>
mov    $0x0,%al
call   1050 <usage@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <main+0xf2>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1080 <opendev@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11cd <main+0x5d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <main+0xf2>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
and    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
je     1253 <main+0xe3>
lea    0xdc6(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%edi
call   1090 <closedev@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1262 <main+0xf2>
mov    -0x14(%rbp),%edi
call   1090 <closedev@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    11ef <main+0x7f>
mov    %rsi,%rbx
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1080 <opendev@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1202 <main+0x92>
mov    0x8(%rbx),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %eax,%r15d
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %eax,0x4(%rsp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <lseek@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    1213 <main+0xa3>
mov    %ebp,%edi
call   1090 <closedev@plt>
jmp    1205 <main+0x95>
xor    %eax,%eax
call   1050 <usage@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%r14d
jmp    1205 <main+0x95>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde6(%rip),%rdi
call   1060 <perror@plt>
mov    %ebp,%edi
call   1090 <closedev@plt>
mov    $0xffffffff,%r14d
jmp    1205 <main+0x95>

LLVM IR (-O0):
@EINVAL = global i32 0
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (...) @usage()
%13 = load i32, i32* @EINVAL
store i32 %13, i32* %3
br label %45
14: ; preds = %2
%15 = load i32, i32* @O_RDWR
%16 = call i32 @opendev(i32 %15)
store i32 %16, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* %6
store i32 %19, i32* %3
br label %45
20: ; preds = %14
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @strtoul(i8* %23, i32* null, i32 16)
%25 = and i32 %24, -1
store i32 %25, i32* %8
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @strtoul(i8* %28, i32* null, i32 16)
store i32 %29, i32* %7
%30 = load i32, i32* %6
%31 = load i32, i32* %8
%32 = load i32, i32* @SEEK_SET
%33 = call i32 @lseek(i32 %30, i32 %31, i32 %32)
%34 = load i32, i32* %6
%35 = call i32 @write(i32 %34, i32* %7, i32 4)
%36 = sext i32 %35 to i64
%37 = icmp ne i64 %36, 4
br i1 %37, label %38, label %42
38: ; preds = %20
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* %6
%41 = call i32 @closedev(i32 %40)
store i32 -1, i32* %3
br label %45
42: ; preds = %20
%43 = load i32, i32* %6
%44 = call i32 @closedev(i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %38, %18, %11
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@EINVAL = global i32 0
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 (...) @usage()
%8 = load i32, i32* @EINVAL
br label %29
9: ; preds = %2
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @opendev(i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %29, label %13
13: ; preds = %9
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @strtoul(i8* nocapture %15, i32* null, i32 16)
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @strtoul(i8* nocapture %18, i32* null, i32 16)
store i32 %19, i32* %3
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @lseek(i32 %11, i32 %16, i32 %20)
%22 = call i32 @write(i32 %11, i32* %3, i32 4)
%23 = icmp eq i32 %22, 4
br i1 %23, label %27, label %24
24: ; preds = %13
%25 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @closedev(i32 %11)
br label %29
27: ; preds = %13
%28 = tail call i32 @closedev(i32 %11)
br label %29
29: ; preds = %9, %27, %24, %6
%30 = phi i32 [ %8, %6 ], [ -1, %24 ], [ 0, %27 ], [ %11, %9 ]
ret i32 %30
}