#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 collatz(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stdin;
int main(void) {
  char eingabepuffer[100];
  int obergrenze, i, ergebnis, maximum, mwert;
  obergrenze = 0;
  printf("Obergrenze: ");
  fgets(eingabepuffer, 100, stdin);
  sscanf(eingabepuffer, "%d", &obergrenze);
  maximum = 0;
  if (obergrenze < 1) {
    printf("Eingabefehler: Die Obergrenze muss mindestens 1 sein. \n");
    return 1;
  } else {
    for (i = 1; i <= obergrenze; ++i) {
      ergebnis = collatz(i);
      if (ergebnis == -1)
        printf("Ueberlauf bei Wert %d \n", i);
      if (maximum <= ergebnis) {
        maximum = ergebnis;
        mwert = i;
      }
      if ((ergebnis <= 10) & (ergebnis >= 0)) {
        printf("%d: %d Schritte \n", i, ergebnis);
      }
    }
    printf("Maximale Anzahl von Schritten sind %d fuer den Wert %d \n", maximum,
           mwert);
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x74(%rbp)
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1050 <fgets@plt>
lea    -0x70(%rbp),%rdi
lea    0xe84(%rip),%rsi
lea    -0x74(%rbp),%rdx
call   1060 <sscanf@plt>
movl   $0x0,-0x80(%rbp)
cmpl   $0x1,-0x74(%rbp)
jge    11bd <main+0x7d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127a <main+0x13a>
movl   $0x1,-0x78(%rbp)
mov    -0x78(%rbp),%eax
cmp    -0x74(%rbp),%eax
jg     125c <main+0x11c>
mov    -0x78(%rbp),%edi
call   1030 <collatz@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0xffffffff,-0x7c(%rbp)
jne    11f6 <main+0xb6>
mov    -0x78(%rbp),%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x80(%rbp),%eax
cmp    -0x7c(%rbp),%eax
jg     1211 <main+0xd1>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x80(%rbp)
mov    -0x78(%rbp),%eax
mov    %eax,-0x84(%rbp)
cmpl   $0xa,-0x7c(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%eax
cmpl   $0x0,-0x7c(%rbp)
setge  %cl
and    $0x1,%cl
movzbl %cl,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1249 <main+0x109>
mov    -0x78(%rbp),%esi
mov    -0x7c(%rbp),%edx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    124e <main+0x10e>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    11c4 <main+0x84>
mov    -0x80(%rbp),%esi
mov    -0x84(%rbp),%edx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
movl   $0x0,0xc(%rsp)
lea    0xe93(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    0xe77(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <sscanf@plt>
cmpl   $0x0,0xc(%rsp)
jle    1229 <main+0xd9>
lea    0xe5f(%rip),%r14
lea    0xe70(%rip),%r15
xor    %ebx,%ebx
xor    %r13d,%r13d
jmp    11c6 <main+0x76>
nop
cmp    0xc(%rsp),%ebx
jge    1211 <main+0xc1>
add    $0x1,%ebx
mov    %ebx,%edi
call   1030 <collatz@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11e3 <main+0x93>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    %ebp,%r13d
cmovle %ebp,%r13d
cmovle %ebx,%r12d
xor    %eax,%eax
cmp    $0xb,%ebp
setl   %al
mov    %ebp,%ecx
not    %ecx
shr    $0x1f,%ecx
test   %eax,%ecx
je     11c0 <main+0x70>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11c0 <main+0x70>
lea    0xe22(%rip),%rdi
xor    %ebx,%ebx
mov    %r13d,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    123a <main+0xea>
lea    0xe43(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Obergrenze: \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [56 x i8] c"Eingabefehler: Die Obergrenze muss mindestens 1 sein. \0A\00"
@.str.3 = [24 x i8] c"Ueberlauf bei Wert %d \0A\00"
@.str.4 = [18 x i8] c"%d: %d Schritte \0A\00"
@.str.5 = [57 x i8] c"Maximale Anzahl von Schritten sind %d fuer den Wert %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%10 = load i32, i32* @stdin
%11 = call i32 @fgets(i8* %9, i32 100, i32 %10)
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
store i32 0, i32* %6
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %59
18: ; preds = %0
store i32 1, i32* %4
br label %19
19: ; preds = %52, %18
%20 = load i32, i32* %4
%21 = load i32, i32* %3
%22 = icmp sle i32 %20, %21
br i1 %22, label %23, label %55
23: ; preds = %19
%24 = load i32, i32* %4
%25 = call i32 @collatz(i32 %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %28, %23
%32 = load i32, i32* %6
%33 = load i32, i32* %5
%34 = icmp sle i32 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* %5
store i32 %36, i32* %6
%37 = load i32, i32* %4
store i32 %37, i32* %7
br label %38
38: ; preds = %35, %31
%39 = load i32, i32* %5
%40 = icmp sle i32 %39, 10
%41 = zext i1 %40 to i32
%42 = load i32, i32* %5
%43 = icmp sge i32 %42, 0
%44 = zext i1 %43 to i32
%45 = and i32 %41, %44
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %38
%48 = load i32, i32* %4
%49 = load i32, i32* %5
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %48, i32 %49)
br label %51
51: ; preds = %47, %38
br label %52
52: ; preds = %51
%53 = load i32, i32* %4
%54 = add nsw i32 %53, 1
store i32 %54, i32* %4
br label %19
55: ; preds = %19
%56 = load i32, i32* %6
%57 = load i32, i32* %7
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i32 %56, i32 %57)
store i32 0, i32* %1
br label %59
59: ; preds = %55, %16
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@.str = [13 x i8] c"Obergrenze: \00"
@stdin = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [24 x i8] c"Ueberlauf bei Wert %d \0A\00"
@.str.4 = [18 x i8] c"%d: %d Schritte \0A\00"
@.str.5 = [57 x i8] c"Maximale Anzahl von Schritten sind %d fuer den Wert %d \0A\00"
@str = [55 x i8] c"Eingabefehler: Die Obergrenze muss mindestens 1 sein. \00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32 @fgets(i8* %3, i32 100, i32 %6)
%8 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
br label %39
13: ; preds = %0, %33
%14 = phi i32 [ %24, %33 ], [ undef, %0 ]
%15 = phi i32 [ %23, %33 ], [ 0, %0 ]
%16 = phi i32 [ %34, %33 ], [ 1, %0 ]
%17 = call i32 @collatz(i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %16)
br label %21
21: ; preds = %19, %13
%22 = icmp sgt i32 %15, %17
%23 = select i1 %22, i32 %15, i32 %17
%24 = select i1 %22, i32 %14, i32 %16
%25 = icmp slt i32 %17, 11
%26 = zext i1 %25 to i32
%27 = xor i32 %17, -1
%28 = lshr i32 %27, 31
%29 = and i32 %28, %26
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %21
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %16, i32 %17)
br label %33
33: ; preds = %21, %31
%34 = add nuw nsw i32 %16, 1
%35 = load i32, i32* %2
%36 = icmp slt i32 %16, %35
br i1 %36, label %13, label %37
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i32 %23, i32 %24)
br label %39
39: ; preds = %37, %11
%40 = phi i32 [ 1, %11 ], [ 0, %37 ]
ret i32 %40
}