#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*/ O_RDWR;
void *atoi(char *);
int /*<<< orphan*/ bzero(char *, int);
char *calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  int end = 0;
  char *buffer = calloc(3, sizeof(char) * 3);
  char *stdBuffer = calloc(3, sizeof(char) * 3);
  while (!end) {
    bzero(stdBuffer, 3);
    buffer[0] = 0x01;
    buffer[1] = 0x03;
    printf("Befehl oder Abbrechen (-1)?\n");
    read(0, stdBuffer, 3);
    int number = atoi(stdBuffer);
    if (number == -1) {
      end = 1;
    } else {
      buffer[2] = atoi(stdBuffer);
      int size = write(fd, buffer, 3);
      printf("Size written: %d\n", size);
    }
  }
  free(buffer);
  free(stdBuffer);
  close(fd);
  printf("Close..\n");
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f1 <main+0x51>
lea    0xe20(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f6 <main+0x156>
movl   $0x0,-0x18(%rbp)
mov    $0x3,%esi
mov    %esi,%edi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x3,%esi
mov    %esi,%edi
call   1080 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    122e <main+0x8e>
jmp    12c7 <main+0x127>
mov    -0x28(%rbp),%rdi
mov    $0x3,%esi
call   10b0 <bzero@plt>
mov    -0x20(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x3,0x1(%rax)
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
mov    $0x3,%edx
call   1070 <read@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    128b <main+0xeb>
movl   $0x1,-0x18(%rbp)
jmp    12c2 <main+0x122>
mov    -0x28(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1218 <main+0x78>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
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   %r13
push   %r12
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12dc <main+0x12c>
mov    %eax,%r15d
mov    $0x3,%edi
mov    $0x3,%esi
call   1090 <calloc@plt>
mov    %rax,%rbp
mov    $0x3,%edi
mov    $0x3,%esi
call   1090 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x3,%esi
call   10c0 <bzero@plt>
movw   $0x301,0x0(%rbp)
lea    0xe08(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1080 <read@plt>
mov    %rbx,%rdi
call   10d0 <atoi@plt>
cmp    $0xffffffff,%eax
je     12a6 <main+0xf6>
lea    0xdc3(%rip),%r12
lea    0xdd6(%rip),%r13
nopl   0x0(%rax)
mov    %al,0x2(%rbp)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1050 <write@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   10c0 <bzero@plt>
movw   $0x301,0x0(%rbp)
mov    %r13,%rdi
call   1040 <puts@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1080 <read@plt>
mov    %rbx,%rdi
call   10d0 <atoi@plt>
cmp    $0xffffffff,%eax
jne    1250 <main+0xa0>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1070 <close@plt>
lea    0xd52(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r14d
jmp    12ca <main+0x11a>

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [29 x i8] c"Befehl oder Abbrechen (-1)?\0A\00"
@.str.2 = [18 x i8] c"Size written: %d\0A\00"
@.str.3 = [9 x i8] c"Close..\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 i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %64
21: ; preds = %2
store i32 0, i32* %7
%22 = call i8* @calloc(i32 3, i32 3)
store i8* %22, i8** %8
%23 = call i8* @calloc(i32 3, i32 3)
store i8* %23, i8** %9
br label %24
24: ; preds = %55, %21
%25 = load i32, i32* %7
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
br i1 %27, label %28, label %56
28: ; preds = %24
%29 = load i8*, i8** %9
%30 = call i32 @bzero(i8* %29, i32 3)
%31 = load i8*, i8** %8
%32 = getelementptr inbounds i8, i8* %31, i64 0
store i8 1, i8* %32
%33 = load i8*, i8** %8
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 3, i8* %34
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%36 = load i8*, i8** %9
%37 = call i32 @read(i32 0, i8* %36, i32 3)
%38 = load i8*, i8** %9
%39 = call i8* @atoi(i8* %38)
%40 = ptrtoint i8* %39 to i32
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %28
store i32 1, i32* %7
br label %55
44: ; preds = %28
%45 = load i8*, i8** %9
%46 = call i8* @atoi(i8* %45)
%47 = ptrtoint i8* %46 to i8
%48 = load i8*, i8** %8
%49 = getelementptr inbounds i8, i8* %48, i64 2
store i8 %47, i8* %49
%50 = load i32, i32* %6
%51 = load i8*, i8** %8
%52 = call i32 @write(i32 %50, i8* %51, i32 3)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %44, %43
br label %24
56: ; preds = %24
%57 = load i8*, i8** %8
%58 = call i32 @free(i8* %57)
%59 = load i8*, i8** %9
%60 = call i32 @free(i8* %59)
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %64
64: ; preds = %56, %19
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@.str.2 = [18 x i8] c"Size written: %d\0A\00"
@str = [8 x i8] c"Close..\00"
@str.4 = [28 x i8] c"Befehl oder Abbrechen (-1)?\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %39
10: ; preds = %2
%11 = tail call dereferenceable_or_null(9) i8* @calloc(i32 3, i32 3)
%12 = tail call dereferenceable_or_null(9) i8* @calloc(i32 3, i32 3)
%13 = getelementptr inbounds i8, i8* %11, i64 1
%14 = getelementptr inbounds i8, i8* %11, i64 2
%15 = tail call i32 @bzero(i8* %12, i32 3)
store i8 1, i8* %11
store i8 3, i8* %13
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.4, i64 0, i64 0))
%17 = tail call i32 @read(i32 0, i8* %12, i32 3)
%18 = tail call i8* @atoi(i8* %12)
%19 = ptrtoint i8* %18 to i64
%20 = trunc i64 %19 to i32
%21 = icmp eq i32 %20, -1
br i1 %21, label %34, label %22
22: ; preds = %10, %22
%23 = phi i64 [ %31, %22 ], [ %19, %10 ]
%24 = trunc i64 %23 to i8
store i8 %24, i8* %14
%25 = tail call i32 @write(i32 %6, i8* %11, i32 3)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = tail call i32 @bzero(i8* %12, i32 3)
store i8 1, i8* %11
store i8 3, i8* %13
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.4, i64 0, i64 0))
%29 = tail call i32 @read(i32 0, i8* %12, i32 3)
%30 = tail call i8* @atoi(i8* %12)
%31 = ptrtoint i8* %30 to i64
%32 = trunc i64 %31 to i32
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %22
34: ; preds = %22, %10
%35 = tail call i32 @free(i8* %11)
%36 = tail call i32 @free(i8* %12)
%37 = tail call i32 @close(i32 %6)
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %39
39: ; preds = %34, %8
%40 = phi i32 [ 1, %8 ], [ 0, %34 ]
ret i32 %40
}