#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int errno;
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *, int);
scalar_t__ pwrite(int, char *, int, int);
int main(int argc, char *argv[]) {
  const char *sysctl;
  int offset;
  int fd;
  ssize_t len;
  char *buffer = "test";
  if (argc != 3) {
    return 77;
  }
  sysctl = argv[1];
  offset = atoi(argv[2]);
  fd = open(sysctl, O_RDONLY);
  if (fd < 0) {
    perror("Failed to open sysctl");
    return 77;
  }
  printf("Writing to '%s' at offset %d\n", sysctl, offset);
  len = pwrite(fd, buffer, 1, offset);
  if (len < 0) {
    perror("Write failed");
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1187 <main+0x37>
movl   $0x4d,-0x4(%rbp)
jmp    1234 <main+0xe4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11da <main+0x8a>
lea    0xe3c(%rip),%rdi
call   1060 <perror@plt>
movl   $0x4d,-0x4(%rbp)
jmp    1234 <main+0xe4>
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
mov    $0x1,%edx
call   1040 <pwrite@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    122d <main+0xdd>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <main+0xe4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x4d,%ebx
cmp    $0x3,%edi
jne    11b7 <main+0x67>
mov    0x8(%rsi),%r15
mov    0x10(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r14d
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11c4 <main+0x74>
mov    %eax,%ebp
lea    0xe8c(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <printf@plt>
lea    0xe5f(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
mov    %r14d,%ecx
call   1040 <pwrite@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     11d2 <main+0x82>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
jmp    11b7 <main+0x67>
lea    0xe60(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%ebx
jmp    11b7 <main+0x67>

LLVM IR (-O0):
@.str = [5 x i8] c"test\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Failed to open sysctl\00"
@.str.2 = [30 x i8] c"Writing to '%s' at offset %d\0A\00"
@.str.3 = [13 x i8] c"Write failed\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 77, i32* %3
br label %43
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %7
%22 = load i8*, i8** %6
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 77, i32* %3
br label %43
29: ; preds = %14
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %30, i32 %31)
%33 = load i32, i32* %8
%34 = load i8*, i8** %10
%35 = load i32, i32* %7
%36 = call i64 @pwrite(i32 %33, i8* %34, i32 1, i32 %35)
store i64 %36, i64* %9
%37 = load i64, i64* %9
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %29
%40 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @errno
store i32 %41, i32* %3
br label %43
42: ; preds = %29
store i32 0, i32* %3
br label %43
43: ; preds = %42, %39, %27, %13
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [5 x i8] c"test\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Failed to open sysctl\00"
@.str.2 = [30 x i8] c"Writing to '%s' at offset %d\0A\00"
@.str.3 = [13 x i8] c"Write failed\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %4, label %22
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %6, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %22
15: ; preds = %4
%16 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %6, i32 %9)
%17 = tail call i64 @pwrite(i32 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1, i32 %9)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @errno
br label %22
22: ; preds = %15, %2, %19, %13
%23 = phi i32 [ 77, %13 ], [ %21, %19 ], [ 77, %2 ], [ 0, %15 ]
ret i32 %23
}