#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 */
struct user {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ chatdat_gen_add_user(struct user *);
struct user *chatdat_parse_users(int /*<<< orphan*/ *);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int yes_no();
void chatdat_gen() {
  printf("Chat.dat Generator\n");
  FILE *chatdat = fopen("chat.dat", "rw");
  if (chatdat == NULL)
    error("trying to open chat.dat");
  struct user *all_users = chatdat_parse_users(chatdat);
  int another = 1;
  while (another) {
  prompt:
    printf("\n");
    printf(" 0 - Add a user.\n");
    printf(" 1 - Change a user's password.\n");
    printf(" 2 - Change a user's name.\n");
    printf(" 3 - Remove a user.\n");
    printf("-1 - Exit\n");
    printf("What would you like to do? ");
    int decision = 0;
    char decision_s[4];
    fgets(decision_s, 4, stdin);
    decision = atoi(decision_s);
    switch (decision) {
    case -1:
      printf("Goodbye.\n");
      return;
      break;
    case 0:
      chatdat_gen_add_user(all_users);
      break;
    case 1:
      break;
    case 2:
      break;
    case 3:
      break;
    default:
      printf("Invalid option.\n");
      goto prompt;
      break;
    }
    printf("\n");
    printf("Would you like to do something else? (y/n) ");
    another = yes_no();
  }
  printf("Goodbye.\n");
}


x86 assembly (clang, -O0):
<chatdat_gen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe85(%rip),%rdi
call   1050 <printf@plt>
lea    0xe9f(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c2 <chatdat_gen+0x42>
lea    0xe6e(%rip),%rdi
call   1080 <error@plt>
mov    -0x8(%rbp),%rdi
call   1030 <chatdat_parse_users@plt>
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12f6 <chatdat_gen+0x176>
jmp    11e5 <chatdat_gen+0x65>
lea    0xe87(%rip),%rdi
call   1050 <printf@plt>
lea    0xe4b(%rip),%rdi
call   1050 <printf@plt>
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
lea    0xe65(%rip),%rdi
call   1050 <printf@plt>
lea    0xe75(%rip),%rdi
call   1050 <printf@plt>
lea    0xe7e(%rip),%rdi
call   1050 <printf@plt>
lea    0xe7d(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x18(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rdi
mov    %rdi,-0x30(%rbp)
mov    $0x4,%esi
call   1060 <fgets@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x4,%eax
ja     12be <chatdat_gen+0x13e>
mov    -0x28(%rbp),%rax
lea    0xd79(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
jmp    1302 <chatdat_gen+0x182>
mov    -0x10(%rbp),%rdi
call   1040 <chatdat_gen_add_user@plt>
jmp    12cf <chatdat_gen+0x14f>
jmp    12cf <chatdat_gen+0x14f>
jmp    12cf <chatdat_gen+0x14f>
jmp    12cf <chatdat_gen+0x14f>
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
jmp    11e5 <chatdat_gen+0x65>
lea    0xd9d(%rip),%rdi
call   1050 <printf@plt>
lea    0xe06(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <yes_no@plt>
mov    %eax,-0x14(%rbp)
jmp    11d6 <chatdat_gen+0x56>
lea    0xdd0(%rip),%rdi
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chatdat_gen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xeae(%rip),%rdi
call   1040 <puts@plt>
lea    0xe51(%rip),%rdi
lea    0xe38(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11e1 <chatdat_gen+0x41>
lea    0xe27(%rip),%rdi
call   10a0 <error@plt>
mov    %rbx,%rdi
call   1050 <chatdat_parse_users@plt>
mov    %rax,0x8(%rsp)
lea    0xeb1(%rip),%r13
lea    0xec5(%rip),%rbx
lea    0xed2(%rip),%rbp
lea    0xe11(%rip),%r14
mov    0x2dd7(%rip),%r12
lea    0x14(%rsp),%r15
jmp    1247 <chatdat_gen+0xa7>
mov    0x8(%rsp),%rdi
call   1060 <chatdat_gen_add_user@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe04(%rip),%rdi
call   1070 <printf@plt>
xor    %eax,%eax
call   1090 <yes_no@plt>
test   %eax,%eax
je     12cc <chatdat_gen+0x12c>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe1e(%rip),%rdi
call   1040 <puts@plt>
lea    0xe23(%rip),%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1070 <printf@plt>
mov    (%r12),%edx
mov    %r15,%rdi
mov    $0x4,%esi
call   1080 <fgets@plt>
mov    %r15,%rdi
call   10c0 <atoi@plt>
lea    -0x1(%rax),%ecx
cmp    $0x3,%ecx
jb     1222 <chatdat_gen+0x82>
test   %eax,%eax
je     1218 <chatdat_gen+0x78>
cmp    $0xffffffff,%eax
je     12cc <chatdat_gen+0x12c>
lea    0xe26(%rip),%rdi
call   1040 <puts@plt>
jmp    1247 <chatdat_gen+0xa7>
lea    0xe0c(%rip),%rdi
call   1040 <puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.user = type { i32 }
@.str = [20 x i8] c"Chat.dat Generator\0A\00"
@.str.1 = [9 x i8] c"chat.dat\00"
@.str.2 = [3 x i8] c"rw\00"
@.str.3 = [24 x i8] c"trying to open chat.dat\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [18 x i8] c" 0 - Add a user.\0A\00"
@.str.6 = [32 x i8] c" 1 - Change a user's password.\0A\00"
@.str.7 = [28 x i8] c" 2 - Change a user's name.\0A\00"
@.str.8 = [21 x i8] c" 3 - Remove a user.\0A\00"
@.str.9 = [11 x i8] c"-1 - Exit\0A\00"
@.str.10 = [28 x i8] c"What would you like to do? \00"
@stdin = global i32 0
@.str.11 = [10 x i8] c"Goodbye.\0A\00"
@.str.12 = [17 x i8] c"Invalid option.\0A\00"
@.str.13 = [44 x i8] c"Would you like to do something else? (y/n) \00"
define void @chatdat_gen() {
%1 = alloca i32*
%2 = alloca %struct.user*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4 x i8]
%6 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%7 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %7, i32** %1
%8 = load i32*, i32** %1
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32*, i32** %1
%14 = call %struct.user* @chatdat_parse_users(i32* %13)
store %struct.user* %14, %struct.user** %2
store i32 1, i32* %3
br label %15
15: ; preds = %43, %12
%16 = load i32, i32* %3
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %47
18: ; preds = %15
br label %19
19: ; preds = %41, %18
%20 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%23 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
%24 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
%25 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %4
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* @stdin
%29 = call i32 @fgets(i8* %27, i32 4, i32 %28)
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
switch i32 %32, label %41 [
i32 -1, label %33
i32 0, label %35
i32 1, label %38
i32 2, label %39
i32 3, label %40
]
33: ; preds = %19
%34 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
br label %49
35: ; preds = %19
%36 = load %struct.user*, %struct.user** %2
%37 = call i32 @chatdat_gen_add_user(%struct.user* %36)
br label %43
38: ; preds = %19
br label %43
39: ; preds = %19
br label %43
40: ; preds = %19
br label %43
41: ; preds = %19
%42 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
br label %19
43: ; preds = %40, %39, %38, %35
%44 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.13, i64 0, i64 0))
%46 = call i32 (...) @yes_no()
store i32 %46, i32* %3
br label %15
47: ; preds = %15
%48 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.11, i64 0, i64 0))
br label %49
49: ; preds = %47, %33
ret void
}

LLVM IR (-O2):
%struct.user = type { i32 }
@.str.1 = [9 x i8] c"chat.dat\00"
@.str.2 = [3 x i8] c"rw\00"
@.str.3 = [24 x i8] c"trying to open chat.dat\00"
@.str.10 = [28 x i8] c"What would you like to do? \00"
@stdin = global i32 0
@.str.13 = [44 x i8] c"Would you like to do something else? (y/n) \00"
@str = [19 x i8] c"Chat.dat Generator\00"
@str.15 = [17 x i8] c" 0 - Add a user.\00"
@str.16 = [31 x i8] c" 1 - Change a user's password.\00"
@str.17 = [27 x i8] c" 2 - Change a user's name.\00"
@str.18 = [20 x i8] c" 3 - Remove a user.\00"
@str.19 = [10 x i8] c"-1 - Exit\00"
@str.20 = [9 x i8] c"Goodbye.\00"
@str.21 = [16 x i8] c"Invalid option.\00"
define void @chatdat_gen() {
%1 = alloca [4 x i8]
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = tail call %struct.user* @chatdat_parse_users(i32* %3)
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
br label %10
10: ; preds = %25, %7
%11 = call i32 @putchar(i32 10)
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.15, i64 0, i64 0))
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.16, i64 0, i64 0))
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.17, i64 0, i64 0))
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.18, i64 0, i64 0))
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.19, i64 0, i64 0))
%17 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
%18 = load i32, i32* @stdin
%19 = call i32 @fgets(i8* %9, i32 4, i32 %18)
%20 = call i32 @atoi(i8* %9)
switch i32 %20, label %23 [
i32 -1, label %31
i32 0, label %21
i32 1, label %26
i32 2, label %26
i32 3, label %26
]
21: ; preds = %10
%22 = call i32 @chatdat_gen_add_user(%struct.user* %8)
br label %26
23: ; preds = %10
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.21, i64 0, i64 0))
br label %25
25: ; preds = %23, %26
br label %10
26: ; preds = %10, %10, %10, %21
%27 = call i32 @putchar(i32 10)
%28 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.13, i64 0, i64 0))
%29 = call i32 (...) @yes_no()
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %25
31: ; preds = %26, %10
%32 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @str.20, i64 0, i64 0), %10 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @str.20, i64 0, i64 0), %26 ]
%33 = call i32 @puts(i8* dereferenceable(1) %32)
ret void
}