#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 EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(char const *, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t, char *);
int /*<<< orphan*/ strlen(char const *);
int Main() {
  FILE *fp;
  size_t count;
  const char *str = "Jonas Gorauskas\n";
  fp = fopen("test123.txt", "w");
  if (fp == NULL) {
    perror("File not found!");
    return EXIT_FAILURE;
  }
  count = fwrite(str, 1, strlen(str), fp);
  printf("Wrote %zu bytes. fclose(fp) %s.\n", count,
         fclose(fp) == 0 ? "succeeded" : "failed");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<Main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe97(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b2 <Main+0x52>
lea    0xe83(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1221 <Main+0xc1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rcx
lea    0xe60(%rip),%rdx
lea    0xe4f(%rip),%rax
cmp    $0x0,%rcx
cmove  %rax,%rdx
lea    0xe1f(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Main>:
push   %r14
push   %rbx
push   %rax
lea    0xea6(%rip),%rdi
lea    0xeab(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e0 <Main+0x80>
mov    %rax,%rbx
lea    0xe7a(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
lea    0xe9b(%rip),%rax
lea    0xe9e(%rip),%rdx
cmove  %rax,%rdx
lea    0xe68(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe38(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ded(%rip),%rax
jmp    11d6 <Main+0x76>

LLVM IR (-O0):
@.str = [17 x i8] c"Jonas Gorauskas\0A\00"
@.str.1 = [12 x i8] c"test123.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [16 x i8] c"File not found!\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [33 x i8] c"Wrote %zu bytes. fclose(fp) %s.\0A\00"
@.str.5 = [10 x i8] c"succeeded\00"
@.str.6 = [7 x i8] c"failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @Main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8*
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
store i32 %10, i32* %1
br label %25
11: ; preds = %0
%12 = load i8*, i8** %4
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = load i32*, i32** %2
%16 = call i64 @fwrite(i8* %12, i32 1, i32 %14, i32* %15)
store i64 %16, i64* %3
%17 = load i64, i64* %3
%18 = load i32*, i32** %2
%19 = call i64 @fclose(i32* %18)
%20 = icmp eq i64 %19, 0
%21 = zext i1 %20 to i64
%22 = select i1 %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0)
%23 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i64 %17, i8* %22)
%24 = load i32, i32* @EXIT_SUCCESS
store i32 %24, i32* %1
br label %25
25: ; preds = %11, %8
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [17 x i8] c"Jonas Gorauskas\0A\00"
@.str.1 = [12 x i8] c"test123.txt\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [16 x i8] c"File not found!\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [33 x i8] c"Wrote %zu bytes. fclose(fp) %s.\0A\00"
@.str.5 = [10 x i8] c"succeeded\00"
@.str.6 = [7 x i8] c"failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @Main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %12
5: ; preds = %0
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 1, i32 %6, i32* %1)
%8 = tail call i64 @fclose(i32* %1)
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i64 %7, i8* %10)
br label %12
12: ; preds = %5, %3
%13 = phi i32* [ @EXIT_FAILURE, %3 ], [ @EXIT_SUCCESS, %5 ]
%14 = load i32, i32* %13
ret i32 %14
}