#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*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
int main() {
  char test[100] = {0};
  FILE *fp;
  char str[60];
  fp = fopen("file.txt", "w");
  if (fp == NULL) {
    perror("Error opening file");
    return (-1);
  }
  fprintf(fp, "testing %s", "abcdef");
  fclose(fp);
  fgets(test, 5, stdin);
  if (!strcmp(test, "xyz\n")) {
    fwrite("good1\n", 1, 6, stdout);
  } else if (!strcmp(test, "wxyz")) {
    fwrite("good2\n", 1, 6, stderr);
  }
  if (!strcmp(test, "a\nb")) {
    puts("not possible");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
lea    0xe47(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
jne    11ec <main+0x5c>
lea    0xe30(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <main+0x139>
mov    -0x78(%rbp),%rdi
lea    0xe27(%rip),%rsi
lea    0xe2b(%rip),%rdx
call   1080 <fprintf@plt>
mov    -0x78(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x70(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    $0x5,%esi
call   1060 <fgets@plt>
lea    -0x70(%rbp),%rdi
lea    0xe02(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1260 <main+0xd0>
mov    0x2d8d(%rip),%rax
mov    (%rax),%ecx
lea    0xde9(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
call   10b0 <fwrite@plt>
jmp    129d <main+0x10d>
lea    -0x70(%rbp),%rdi
lea    0xdd1(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1298 <main+0x108>
mov    0x2d58(%rip),%rax
mov    (%rax),%ecx
lea    0xdb8(%rip),%rdi
mov    $0x1,%esi
mov    $0x6,%edx
call   10b0 <fwrite@plt>
jmp    129d <main+0x10d>
lea    -0x70(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12c2 <main+0x132>
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x70,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
lea    0xe5c(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1260 <main+0xf0>
mov    %rax,%rbx
lea    0xe5b(%rip),%rsi
lea    0xe5f(%rip),%rdx
mov    %rax,%rdi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x5,%esi
call   1050 <fgets@plt>
mov    $0xa7a7978,%eax
xor    (%rsp),%eax
movzbl 0x4(%rsp),%ecx
or     %eax,%ecx
je     1222 <main+0xb2>
mov    $0x7a797877,%eax
xor    (%rsp),%eax
movzbl 0x4(%rsp),%ecx
or     %eax,%ecx
jne    1241 <main+0xd1>
lea    0xe28(%rip),%rdi
mov    0x2db8(%rip),%rax
jmp    1230 <main+0xc0>
lea    0xe0c(%rip),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x6,%edx
call   1090 <fwrite@plt>
xor    %ebx,%ebx
cmpl   $0x620a61,(%rsp)
jne    1258 <main+0xe8>
lea    0xdf5(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x70,%rsp
pop    %rbx
ret
lea    0xda4(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1258 <main+0xe8>

LLVM IR (-O0):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"Error opening file\00"
@.str.3 = [11 x i8] c"testing %s\00"
@.str.4 = [7 x i8] c"abcdef\00"
@stdin = global i32 0
@.str.5 = [5 x i8] c"xyz\0A\00"
@.str.6 = [7 x i8] c"good1\0A\00"
@stdout = global i32 0
@.str.7 = [5 x i8] c"wxyz\00"
@.str.8 = [7 x i8] c"good2\0A\00"
@stderr = global i32 0
@.str.9 = [4 x i8] c"a\0Ab\00"
@.str.10 = [13 x i8] c"not possible\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca i32*
%4 = alloca [60 x i8]
store i32 0, i32* %1
%5 = bitcast [100 x i8]* %2 to i8*
%6 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
11: ; preds = %0
%12 = load i32*, i32** %3
%13 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%14 = load i32*, i32** %3
%15 = call i32 @fclose(i32* %14)
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%17 = load i32, i32* @stdin
%18 = call i32 @fgets(i8* %16, i32 5, i32 %17)
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%20 = call i32 @strcmp(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %11
%23 = load i32, i32* @stdout
%24 = call i32 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i32 1, i32 6, i32 %23)
br label %33
25: ; preds = %11
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%27 = call i32 @strcmp(i8* %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), i32 1, i32 6, i32 %30)
br label %32
32: ; preds = %29, %25
br label %33
33: ; preds = %32, %22
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%35 = call i32 @strcmp(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i64 0, i64 0))
%36 = icmp ne i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
store i32 0, i32* %1
br label %40
40: ; preds = %39, %9
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [19 x i8] c"Error opening file\00"
@.str.3 = [11 x i8] c"testing %s\00"
@.str.4 = [7 x i8] c"abcdef\00"
@stdin = global i32 0
@.str.5 = [5 x i8] c"xyz\0A\00"
@.str.6 = [7 x i8] c"good1\0A\00"
@stdout = global i32 0
@.str.7 = [5 x i8] c"wxyz\00"
@.str.8 = [7 x i8] c"good2\0A\00"
@stderr = global i32 0
@.str.10 = [13 x i8] c"not possible\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %28
7: ; preds = %0
%8 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%9 = tail call i32 @fclose(i32* %3)
%10 = load i32, i32* @stdin
%11 = call i32 @fgets(i8* %2, i32 5, i32 %10)
%12 = call i32 @bcmp(i8* dereferenceable(5) %2, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i64 5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %7
%15 = call i32 @bcmp(i8* dereferenceable(5) %2, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i64 5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14, %7
%18 = phi i32* [ @stdout, %7 ], [ @stderr, %14 ]
%19 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), %7 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0), %14 ]
%20 = load i32, i32* %18
%21 = call i32 @fwrite(i8* %19, i32 1, i32 6, i32 %20)
br label %22
22: ; preds = %17, %14
%23 = bitcast [100 x i8]* %1 to i32*
%24 = load i32, i32* %23
%25 = icmp eq i32 %24, 6425185
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
br label %28
28: ; preds = %22, %26, %5
%29 = phi i32 [ -1, %5 ], [ 0, %26 ], [ 0, %22 ]
ret i32 %29
}