#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*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int n, i, sum = 0, input;
  printf("\nInteger to square: ");
  scanf("%d", &n);
  printf("1\tFOR-LOOP\n");
  printf("2\tDO-WHILE-LOOP\n");
  printf("\nChoose a loop: ");
  scanf("%d", &input);
  switch (input) {
  case 1: {
    for (i = 0; i < n; i++) {
      sum += n;
    }
    break;
  }
  case 2: {
    i = 0;
    do {
      sum += n;
      i++;
    } while (i < n);
    break;
  }
  default:
    printf("Invalid Input\n");
    return 0;
    break;
  }
  printf("\nInteger squared: %d\n\n", sum);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xec3(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x21(%rbp)
call   1030 <printf@plt>
lea    0xec7(%rip),%rdi
mov    %rdi,-0x20(%rbp)
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x21(%rbp),%al
lea    0xeb3(%rip),%rdi
call   1030 <printf@plt>
mov    -0x21(%rbp),%al
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
mov    -0x21(%rbp),%al
lea    0xeb2(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x1,%eax
je     11ba <main+0x9a>
jmp    11a9 <main+0x89>
mov    -0x18(%rbp),%eax
sub    $0x2,%eax
je     11e9 <main+0xc9>
jmp    1213 <main+0xf3>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11e4 <main+0xc4>
mov    -0x8(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c1 <main+0xa1>
jmp    122d <main+0x10d>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     11f0 <main+0xd0>
jmp    122d <main+0x10d>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <main+0x125>
mov    -0x10(%rbp),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xec5(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeca(%rip),%r14
mov    %rsp,%rsi
mov    %r14,%rdi
call   1050 <scanf@plt>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
lea    0xea3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
call   1050 <scanf@plt>
mov    0x4(%rsp),%eax
cmp    $0x1,%eax
je     11aa <main+0x7a>
cmp    $0x2,%eax
jne    11c9 <main+0x99>
mov    (%rsp),%eax
cmp    $0x2,%eax
mov    $0x1,%ebx
cmovge %eax,%ebx
imul   %eax,%ebx
jmp    11b7 <main+0x87>
mov    (%rsp),%eax
mov    %eax,%ecx
imul   %eax,%ecx
test   %eax,%eax
cmovg  %ecx,%ebx
lea    0xe6b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d5 <main+0xa5>
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [21 x i8] c"\0AInteger to square: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [12 x i8] c"1\09FOR-LOOP\0A\00"
@.str.3 = [17 x i8] c"2\09DO-WHILE-LOOP\0A\00"
@.str.4 = [17 x i8] c"\0AChoose a loop: \00"
@.str.5 = [15 x i8] c"Invalid Input\0A\00"
@.str.6 = [23 x i8] c"\0AInteger squared: %d\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
%12 = load i32, i32* %5
switch i32 %12, label %38 [
i32 1, label %13
i32 2, label %26
]
13: ; preds = %0
store i32 0, i32* %3
br label %14
14: ; preds = %22, %13
%15 = load i32, i32* %3
%16 = load i32, i32* %2
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load i32, i32* %2
%20 = load i32, i32* %4
%21 = add nsw i32 %20, %19
store i32 %21, i32* %4
br label %22
22: ; preds = %18
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %14
25: ; preds = %14
br label %40
26: ; preds = %0
store i32 0, i32* %3
br label %27
27: ; preds = %33, %26
%28 = load i32, i32* %2
%29 = load i32, i32* %4
%30 = add nsw i32 %29, %28
store i32 %30, i32* %4
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
br label %33
33: ; preds = %27
%34 = load i32, i32* %3
%35 = load i32, i32* %2
%36 = icmp slt i32 %34, %35
br i1 %36, label %27, label %37
37: ; preds = %33
br label %40
38: ; preds = %0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %43
40: ; preds = %37, %25
%41 = load i32, i32* %4
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %41)
store i32 0, i32* %1
br label %43
43: ; preds = %40, %38
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str = [21 x i8] c"\0AInteger to square: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.4 = [17 x i8] c"\0AChoose a loop: \00"
@.str.6 = [23 x i8] c"\0AInteger squared: %d\0A\0A\00"
@str = [11 x i8] c"1\09FOR-LOOP\00"
@str.7 = [16 x i8] c"2\09DO-WHILE-LOOP\00"
@str.8 = [14 x i8] c"Invalid Input\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%11 = load i32, i32* %2
switch i32 %11, label %21 [
i32 1, label %16
i32 2, label %12
]
12: ; preds = %0
%13 = load i32, i32* %1
%14 = call i32 @llvm.smax.i32(i32 %13, i32 1)
%15 = mul i32 %13, %14
br label %23
16: ; preds = %0
%17 = load i32, i32* %1
%18 = icmp sgt i32 %17, 0
%19 = mul i32 %17, %17
%20 = select i1 %18, i32 %19, i32 0
br label %23
21: ; preds = %0
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
br label %26
23: ; preds = %16, %12
%24 = phi i32 [ %15, %12 ], [ %20, %16 ]
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %21
ret i32 0
}