#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
int main(void) {
  char buf[1024];
  struct sockaddr_in servaddr;
  int fd;
  int n, len;
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  servaddr.sin_port = htons(9527);
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  if (connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
    perror("connect");
    exit(1);
  }
  for (n = 0; n < 10; n++) {
    snprintf(buf, 1024, "loop time %dth", n);
    len = strlen(buf);
    if (write(fd, buf, len) != len) {
      printf("write error");
      close(fd);
      exit(1);
    }
  }
  close(fd);
  printf("after close");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x41c(%rbp)
mov    $0x2537,%edi
call   1050 <htons@plt>
mov    %eax,-0x420(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x424(%rbp)
cmp    $0xffffffff,%eax
jne    1254 <main+0x94>
lea    0xdbb(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x424(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    128a <main+0xca>
lea    0xd8c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x428(%rbp)
cmpl   $0xa,-0x428(%rbp)
jge    132f <main+0x16f>
lea    -0x410(%rbp),%rdi
mov    -0x428(%rbp),%ecx
mov    $0x400,%esi
lea    0xd55(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x424(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x42c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x42c(%rbp),%eax
je     1316 <main+0x156>
lea    0xd22(%rip),%rdi
call   1060 <printf@plt>
mov    -0x424(%rbp),%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    131b <main+0x15b>
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
jmp    1294 <main+0xd4>
mov    -0x424(%rbp),%edi
call   1090 <close@plt>
lea    0xce9(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rsp,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2e09(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x2537,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     14d1 <main+0x311>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     14da <main+0x31a>
lea    0xdce(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
xor    %ecx,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xd94(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x1,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xd57(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x2,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xd1a(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x3,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xcdd(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x4,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xca0(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x5,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xc63(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x6,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xc26(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x7,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xbe9(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x8,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
lea    0xbb0(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    $0x9,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    14b3 <main+0x2f3>
mov    %r14d,%edi
call   1090 <close@plt>
lea    0xb8a(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xb64(%rip),%rdi
call   1060 <printf@plt>
mov    %r14d,%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xb28(%rip),%rdi
jmp    14e1 <main+0x321>
lea    0xb26(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_LOOPBACK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [15 x i8] c"loop time %dth\00"
@.str.3 = [12 x i8] c"write error\00"
@.str.4 = [12 x i8] c"after close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_LOOPBACK
%11 = call i32 @htonl(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = call i32 @htons(i32 9527)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %0
%24 = load i32, i32* %4
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
store i32 0, i32* %5
br label %32
32: ; preds = %53, %31
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 10
br i1 %34, label %35, label %56
35: ; preds = %32
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%37 = load i32, i32* %5
%38 = call i32 @snprintf(i8* %36, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
store i32 %40, i32* %6
%41 = load i32, i32* %4
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%43 = load i32, i32* %6
%44 = call i32 @write(i32 %41, i8* %42, i32 %43)
%45 = load i32, i32* %6
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %35
%48 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %35
br label %53
53: ; preds = %52
%54 = load i32, i32* %5
%55 = add nsw i32 %54, 1
store i32 %55, i32* %5
br label %32
56: ; preds = %32
%57 = load i32, i32* %4
%58 = call i32 @close(i32 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_LOOPBACK = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [15 x i8] c"loop time %dth\00"
@.str.3 = [12 x i8] c"write error\00"
@.str.4 = [12 x i8] c"after close\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_LOOPBACK
%9 = tail call i32 @htonl(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 9527)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %0
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i32 @connect(i32 %15, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 0)
%26 = call i32 @strlen(i8* %3)
%27 = call i32 @write(i32 %15, i8* %3, i32 %26)
%28 = icmp eq i32 %27, %26
br i1 %28, label %32, label %80
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 1)
%34 = call i32 @strlen(i8* %3)
%35 = call i32 @write(i32 %15, i8* %3, i32 %34)
%36 = icmp eq i32 %35, %34
br i1 %36, label %37, label %80
37: ; preds = %32
%38 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 2)
%39 = call i32 @strlen(i8* %3)
%40 = call i32 @write(i32 %15, i8* %3, i32 %39)
%41 = icmp eq i32 %40, %39
br i1 %41, label %42, label %80
42: ; preds = %37
%43 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 3)
%44 = call i32 @strlen(i8* %3)
%45 = call i32 @write(i32 %15, i8* %3, i32 %44)
%46 = icmp eq i32 %45, %44
br i1 %46, label %47, label %80
47: ; preds = %42
%48 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 4)
%49 = call i32 @strlen(i8* %3)
%50 = call i32 @write(i32 %15, i8* %3, i32 %49)
%51 = icmp eq i32 %50, %49
br i1 %51, label %52, label %80
52: ; preds = %47
%53 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 5)
%54 = call i32 @strlen(i8* %3)
%55 = call i32 @write(i32 %15, i8* %3, i32 %54)
%56 = icmp eq i32 %55, %54
br i1 %56, label %57, label %80
57: ; preds = %52
%58 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 6)
%59 = call i32 @strlen(i8* %3)
%60 = call i32 @write(i32 %15, i8* %3, i32 %59)
%61 = icmp eq i32 %60, %59
br i1 %61, label %62, label %80
62: ; preds = %57
%63 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 7)
%64 = call i32 @strlen(i8* %3)
%65 = call i32 @write(i32 %15, i8* %3, i32 %64)
%66 = icmp eq i32 %65, %64
br i1 %66, label %67, label %80
67: ; preds = %62
%68 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 8)
%69 = call i32 @strlen(i8* %3)
%70 = call i32 @write(i32 %15, i8* %3, i32 %69)
%71 = icmp eq i32 %70, %69
br i1 %71, label %72, label %80
72: ; preds = %67
%73 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 9)
%74 = call i32 @strlen(i8* %3)
%75 = call i32 @write(i32 %15, i8* %3, i32 %74)
%76 = icmp eq i32 %75, %74
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = call i32 @close(i32 %15)
%79 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
80: ; preds = %72, %67, %62, %57, %52, %47, %42, %37, %32, %24
%81 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%82 = call i32 @close(i32 %15)
%83 = call i32 @exit(i32 1)
unreachable
}