#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*/ *fopen(char *, char *);
int fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
void my_fputs(char *path) {
  FILE *fp = NULL;
  fp = fopen(path, "w+");
  if (fp == NULL) {
    perror("my_fputs fopen");
    return;
  }
  char *buf[] = {"123456\n", "bbbbbbbbbb\n", "ccccccccccc\n"};
  int i = 0;
  int n = 3;
  for (i = 0; i < n; i++) {
    int result = fputs(buf[i], fp);
    printf("len = %d\n", result);
  }
  if (fp != NULL) {
    fclose(fp);
    fp = NULL;
  }
}


x86 assembly (clang, -O0):
<my_fputs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <my_fputs+0x44>
lea    0xe79(%rip),%rdi
call   1070 <perror@plt>
jmp    1224 <my_fputs+0xd4>
mov    0x2c65(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c62(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c5f(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x3,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    1208 <my_fputs+0xb8>
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <fputs@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11ca <my_fputs+0x7a>
cmpq   $0x0,-0x10(%rbp)
je     1224 <my_fputs+0xd4>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x10(%rbp)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fputs>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c9 <my_fputs+0x79>
mov    %rax,%rbx
lea    0xea3(%rip),%rdi
mov    %rax,%rsi
call   1050 <fputs@plt>
lea    0xeb5(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xe7e(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <fclose@plt>
lea    0xe33(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <perror@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [15 x i8] c"my_fputs fopen\00"
@.str.2 = [8 x i8] c"123456\0A\00"
@.str.3 = [12 x i8] c"bbbbbbbbbb\0A\00"
@.str.4 = [13 x i8] c"ccccccccccc\0A\00"
@__const.my_fputs.buf = [3 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i32 0, i32 0)]
@.str.5 = [10 x i8] c"len = %d\0A\00"
define void @my_fputs(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [3 x i8*]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
store i32* null, i32** %3
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %38
14: ; preds = %1
%15 = bitcast [3 x i8*]* %4 to i8*
store i32 0, i32* %5
store i32 3, i32* %6
store i32 0, i32* %5
br label %16
16: ; preds = %29, %14
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %32
20: ; preds = %16
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 %22
%24 = load i8*, i8** %23
%25 = load i32*, i32** %3
%26 = call i32 @fputs(i8* %24, i32* %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %20
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %16
32: ; preds = %16
%33 = load i32*, i32** %3
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32*, i32** %3
%37 = call i32 @fclose(i32* %36)
store i32* null, i32** %3
br label %38
38: ; preds = %12, %35, %32
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [15 x i8] c"my_fputs fopen\00"
@.str.2 = [8 x i8] c"123456\0A\00"
@.str.3 = [12 x i8] c"bbbbbbbbbb\0A\00"
@.str.4 = [13 x i8] c"ccccccccccc\0A\00"
@.str.5 = [10 x i8] c"len = %d\0A\00"
define void @my_fputs(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %12, label %4
4: ; preds = %1
%5 = tail call i32 @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %5)
%7 = tail call i32 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %7)
%9 = tail call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %9)
%11 = tail call i32 @fclose(i32* %2)
br label %14
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %4, %12
ret void
}