#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 fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int strlen(char *);
void my_fputc(char *path) {
  FILE *fp = NULL;
  fp = fopen(path, "w+");
  if (fp == NULL) {
    perror("my_fputs fopen");
    return;
  }
  char buf[] = "this is a test for fputc";
  int i = 0;
  int n = strlen(buf);
  for (i = 0; i < n; i++) {
    int ch = fputc(buf[i], fp);
    printf("ch = %c\n", ch);
  }
  if (fp != NULL) {
    fclose(fp);
    fp = NULL;
  }
}


x86 assembly (clang, -O0):
<my_fputc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <my_fputc+0x44>
lea    0xe69(%rip),%rdi
call   1080 <perror@plt>
jmp    1244 <my_fputc+0xe4>
mov    0xe75(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe72(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe6f(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe6d(%rip),%al
mov    %al,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    1228 <my_fputc+0xc8>
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,1),%al
mov    -0x10(%rbp),%rsi
movsbl %al,%edi
call   1060 <fputc@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xdfd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11e8 <my_fputc+0x88>
cmpq   $0x0,-0x10(%rbp)
je     1244 <my_fputc+0xe4>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x10(%rbp)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fputc>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe8e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e3 <my_fputc+0x83>
mov    %rax,%r15
movups 0xea3(%rip),%xmm0
movups %xmm0,0x9(%rsp)
movaps 0xe8e(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11cf <my_fputc+0x6f>
mov    %eax,%r12d
xor    %ebx,%ebx
lea    0xe64(%rip),%r14
xchg   %ax,%ax
movsbl (%rsp,%rbx,1),%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11b0 <my_fputc+0x50>
mov    %r15,%rdi
call   1030 <fclose@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe19(%rip),%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1080 <perror@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [15 x i8] c"my_fputs fopen\00"
@__const.my_fputc.buf = [25 x i8] c"this is a test for fputc\00"
@.str.2 = [9 x i8] c"ch = %c\0A\00"
define void @my_fputc(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [25 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 %40
14: ; preds = %1
%15 = bitcast [25 x i8]* %4 to i8*
store i32 0, i32* %5
%16 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %6
store i32 0, i32* %5
br label %18
18: ; preds = %31, %14
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %34
22: ; preds = %18
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [25 x i8], [25 x i8]* %4, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = load i32*, i32** %3
%28 = call i32 @fputc(i8 signext %26, i32* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %22
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
br label %18
34: ; preds = %18
%35 = load i32*, i32** %3
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32*, i32** %3
%39 = call i32 @fclose(i32* %38)
store i32* null, i32** %3
br label %40
40: ; preds = %12, %37, %34
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [15 x i8] c"my_fputs fopen\00"
@__const.my_fputc.buf = [25 x i8] c"this is a test for fputc\00"
@.str.2 = [9 x i8] c"ch = %c\0A\00"
define void @my_fputc(i8* nocapture %0) {
%2 = alloca [25 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %23
7: ; preds = %1
%8 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%9 = call i32 @strlen(i8* %8)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %7
%12 = zext i32 %9 to i64
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ 0, %11 ], [ %19, %13 ]
%15 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = call i32 @fputc(i8 signext %16, i32* %3)
%18 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = add nuw nsw i64 %14, 1
%20 = icmp eq i64 %19, %12
br i1 %20, label %21, label %13
21: ; preds = %13, %7
%22 = call i32 @fclose(i32* %3)
br label %23
23: ; preds = %21, %5
ret void
}