#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
int write_preferences(void) {
  char *filename = "preferences.txt";
  char username_password[80];
  FILE *fp = NULL;
  puts("Please enter your username and password, separated by a colon, like "
       "this: username:password");
  fgets(username_password, sizeof username_password, stdin);
  fp = fopen(filename, "w");
  if (fp == NULL) {
    perror("Cannot write to preferences file");
    return 1;
  }
  fputs(username_password, fp);
  fclose(fp);
  puts("Username and password saved.");
  return 0;
}


x86 assembly (clang, -O0):
<write_preferences>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xe91(%rip),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x68(%rbp)
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1060 <fgets@plt>
mov    -0x10(%rbp),%rdi
lea    0xec3(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11d5 <write_preferences+0x75>
lea    0xeaa(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11fe <write_preferences+0x9e>
lea    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x68(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_preferences>:
push   %rbx
sub    $0x50,%rsp
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x50,%esi
call   1060 <fgets@plt>
lea    0xe72(%rip),%rdi
lea    0xed7(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c9 <write_preferences+0x69>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    %rax,%rsi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xe9e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    11c3 <write_preferences+0x63>

LLVM IR (-O0):
@.str = [16 x i8] c"preferences.txt\00"
@.str.1 = [92 x i8] c"Please enter your username and password, separated by a colon, like this: username:password\00"
@stdin = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [33 x i8] c"Cannot write to preferences file\00"
@.str.4 = [29 x i8] c"Username and password saved.\00"
define i32 @write_preferences() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [80 x i8]
%4 = alloca i32*
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %2
store i32* null, i32** %4
%5 = call i32 @puts(i8* getelementptr inbounds ([92 x i8], [92 x i8]* @.str.1, i64 0, i64 0))
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @stdin
%8 = call i32 @fgets(i8* %6, i32 80, i32 %7)
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %22
15: ; preds = %0
%16 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i32 @fputs(i8* %16, i32* %17)
%19 = load i32*, i32** %4
%20 = call i32 @fclose(i32* %19)
%21 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %22
22: ; preds = %15, %13
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
@.str = [16 x i8] c"preferences.txt\00"
@.str.1 = [92 x i8] c"Please enter your username and password, separated by a colon, like this: username:password\00"
@stdin = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [33 x i8] c"Cannot write to preferences file\00"
@.str.4 = [29 x i8] c"Username and password saved.\00"
define i32 @write_preferences() {
%1 = alloca [80 x i8]
%2 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([92 x i8], [92 x i8]* @.str.1, i64 0, i64 0))
%4 = load i32, i32* @stdin
%5 = call i32 @fgets(i8* %2, i32 80, i32 %4)
%6 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %14
10: ; preds = %0
%11 = call i32 @fputs(i8* %2, i32* %6)
%12 = call i32 @fclose(i32* %6)
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %14
14: ; preds = %10, %8
%15 = phi i32 [ 1, %8 ], [ 0, %10 ]
ret i32 %15
}