#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int strlen(char *);
int write(int, char *, int);
int main() {
  int ret;
  int fd;
  int len = strlen("Hello again");
  fd = open("/sys/kernel/debug/a_funky/filetxt", O_WRONLY);
  if (fd < 0) {
    perror("error in opening file\n");
    exit(EXIT_FAILURE);
  }
  ret = write(fd, "Hello again", len);
  printf("ret = %d\n", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe7e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ba <main+0x5a>
lea    0xe87(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%edx
lea    0xe39(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6c(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe98(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11aa <main+0x4a>
lea    0xe71(%rip),%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   1030 <write@plt>
lea    0xea6(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe7d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Hello again\00"
@.str.1 = [34 x i8] c"/sys/kernel/debug/a_funky/filetxt\00"
@O_WRONLY = global i32 0
@.str.2 = [23 x i8] c"error in opening file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [10 x i8] c"ret = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 %5, i32* %4
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = call i32 @write(i32 %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %18)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"Hello again\00"
@.str.1 = [34 x i8] c"/sys/kernel/debug/a_funky/filetxt\00"
@O_WRONLY = global i32 0
@.str.2 = [23 x i8] c"error in opening file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [10 x i8] c"ret = %d\0A\00"
define i32 @main() {
%1 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @O_WRONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %10)
ret i32 0
}