#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ malloc(int);
int open(char const *, int, ...);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, void *, int);
int strlen(char const *);
int write(int, void *, int);
int main(int argc, char *argv[]) {
  int k, m, n;
  int fdout;
  int fdin;
  const char *FILENAME = "/tmp/out-lowlevel.txt";
  const char *CONTENT = "Dies ist ein Beispieltext 2";
  char *buffer;
  n = strlen(CONTENT) + 1;
  fdout = open(FILENAME, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
  printf("fdout=%d\n", fdout);
  m = write(fdout, (void *)CONTENT, n);
  close(fdout);
  printf("%d bytes written (length %d)\n", m, n);
  buffer = (char *)malloc(m);
  fdin = open(FILENAME, O_RDONLY);
  printf("fdin=%d\n", fdin);
  k = read(fdin, (void *)buffer, m);
  close(fdin);
  printf("%d byte read:\n%s\n", k, buffer);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe6e(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    0x2df7(%rip),%rax
or     (%rax),%edx
mov    0x2dde(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%esi
mov    -0x40(%rbp),%rdx
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe88(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x2e13(%rip),%rcx
mov    0x2e54(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e11(%rip),%rcx
mov    0x2e22(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
lea    0x1(%rax),%r15d
mov    0x2e03(%rip),%rax
or     (%rax),%edx
lea    0xe32(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
lea    0xe51(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe35(%rip),%rdi
mov    %ebx,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
lea    0xe21(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xe05(%rip),%rdi
mov    %ebx,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"/tmp/out-lowlevel.txt\00"
@.str.1 = [28 x i8] c"Dies ist ein Beispieltext 2\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str.2 = [10 x i8] c"fdout=%d\0A\00"
@.str.3 = [30 x i8] c"%d bytes written (length %d)\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [9 x i8] c"fdin=%d\0A\00"
@.str.5 = [18 x i8] c"%d byte read:\0A%s\0A\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
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8** %11
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8** %12
%14 = load i8*, i8** %12
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
store i32 %16, i32* %8
%17 = load i8*, i8** %11
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRGRP
%25 = or i32 %23, %24
%26 = call i32 (i8*, i32, ...) @open(i8* %17, i32 %20, i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %9
%30 = load i8*, i8** %12
%31 = load i32, i32* %8
%32 = call i32 @write(i32 %29, i8* %30, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %9
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 %36)
%38 = load i32, i32* %7
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %13
%41 = load i8*, i8** %11
%42 = load i32, i32* @O_RDONLY
%43 = call i32 (i8*, i32, ...) @open(i8* %41, i32 %42)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %10
%47 = load i8*, i8** %13
%48 = load i32, i32* %7
%49 = call i32 @read(i32 %46, i8* %47, i32 %48)
store i32 %49, i32* %6
%50 = load i32, i32* %10
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %6
%53 = load i8*, i8** %13
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %52, i8* %53)
%55 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [22 x i8] c"/tmp/out-lowlevel.txt\00"
@.str.1 = [28 x i8] c"Dies ist ein Beispieltext 2\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str.2 = [10 x i8] c"fdout=%d\0A\00"
@.str.3 = [30 x i8] c"%d bytes written (length %d)\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [9 x i8] c"fdin=%d\0A\00"
@.str.5 = [18 x i8] c"%d byte read:\0A%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%4 = add nsw i32 %3, 1
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %12)
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%16 = tail call i32 @close(i32 %13)
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %4)
%18 = tail call i64 @malloc(i32 %15)
%19 = inttoptr i64 %18 to i8*
%20 = load i32, i32* @O_RDONLY
%21 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = tail call i32 @read(i32 %21, i8* %19, i32 %15)
%24 = tail call i32 @close(i32 %21)
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %23, i8* %19)
%26 = tail call i32 @exit(i32 0)
unreachable
}