#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*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int read(int, ...);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ write(int, char *, int);
void *h_read(void *arg) {
  int sock = *((int *)arg);
  char count;
  char size;
  int i;
  char msg;
  char buf[100];
  int file_size;
  int read_size;
  int nread;
  int success = 0;
  FILE *fp;
  while (read(sock, &msg, 1)) {
    if (msg == 0x01) {
    } else if (msg == 0x02) {
      read(sock, &file_size, sizeof(int));
      printf("size : %d\n", file_size);
      if (file_size > 0) {
        fp = fopen("up", "w");
        if (fp != NULL) {
          while (1) {
            nread = read(sock, buf, 100);
            read_size += nread;
            fwrite(buf, 1, nread, fp);
            if (read_size == file_size)
              break;
          }
          fclose(fp);
          success = 1;
          system("sudo pkill -9 lcd-arm");
          system("rm lcd-arm");
          system("sudo chmod 755 up");
          system("mv up lcd-arm");
          system("sudo ./lcd-arm &");
          printf("Update complete\n");
        } else {
          printf("Failed to write file\n");
        }
      } else {
        printf("Failed to receive file\n");
      }
      if (success == 1) {
        msg = 0x02;
        write(sock, &msg, 1);
      } else {
        msg = 0x03;
        write(sock, &msg, 1);
      }
    }
  }
  printf("disconnected\n");
  close(sock);
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<h_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0xa0(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1080 <read@plt>
cmp    $0x0,%eax
je     138a <h_read+0x1fa>
movsbl -0x1d(%rbp),%eax
cmp    $0x1,%eax
jne    11e0 <h_read+0x50>
jmp    1385 <h_read+0x1f5>
movsbl -0x1d(%rbp),%eax
cmp    $0x2,%eax
jne    1380 <h_read+0x1f0>
mov    -0x14(%rbp),%edi
lea    -0x94(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    -0x94(%rbp),%esi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x94(%rbp)
jle    1331 <h_read+0x1a1>
lea    0xe12(%rip),%rdi
lea    0xdd9(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
je     131e <h_read+0x18e>
jmp    1251 <h_read+0xc1>
mov    -0x14(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
mov    $0x0,%al
call   1080 <read@plt>
mov    %eax,-0x9c(%rbp)
mov    -0x9c(%rbp),%eax
add    -0x98(%rbp),%eax
mov    %eax,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x9c(%rbp),%edx
mov    -0xa8(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x98(%rbp),%eax
cmp    -0x94(%rbp),%eax
jne    12b4 <h_read+0x124>
jmp    12b9 <h_read+0x129>
jmp    1251 <h_read+0xc1>
mov    -0xa8(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0xa0(%rbp)
lea    0xd37(%rip),%rdi
call   1050 <system@plt>
lea    0xd41(%rip),%rdi
call   1050 <system@plt>
lea    0xd40(%rip),%rdi
call   1050 <system@plt>
lea    0xd46(%rip),%rdi
call   1050 <system@plt>
lea    0xd48(%rip),%rdi
call   1050 <system@plt>
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    132c <h_read+0x19c>
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    133f <h_read+0x1af>
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x1,-0xa0(%rbp)
jne    1366 <h_read+0x1d6>
movb   $0x2,-0x1d(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    137b <h_read+0x1eb>
movb   $0x3,-0x1d(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1380 <h_read+0x1f0>
jmp    1385 <h_read+0x1f5>
jmp    11b2 <h_read+0x22>
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<h_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    (%rdi),%ebx
lea    0xb(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1090 <read@plt>
test   %eax,%eax
je     130a <h_read+0x16a>
lea    0x10(%rsp),%rbp
lea    0xb(%rsp),%r12
xor    %r14d,%r14d
jmp    121a <h_read+0x7a>
nopw   0x0(%rax,%rax,1)
call   1030 <puts@plt>
cmp    $0x1,%r14d
sete   %al
xor    $0x3,%al
mov    %al,0xb(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1090 <read@plt>
test   %eax,%eax
je     130a <h_read+0x16a>
cmpb   $0x2,0xb(%rsp)
jne    1201 <h_read+0x61>
mov    $0x4,%edx
mov    %ebx,%edi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1090 <read@plt>
mov    0xc(%rsp),%esi
lea    0xdc1(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
cmpl   $0x0,0xc(%rsp)
lea    0xe1a(%rip),%rdi
jle    11e0 <h_read+0x40>
lea    0xde2(%rip),%rdi
lea    0xda9(%rip),%rsi
call   10b0 <fopen@plt>
lea    0xe15(%rip),%rdi
test   %rax,%rax
je     11e0 <h_read+0x40>
mov    %rax,%r13
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x64,%edx
xor    %eax,%eax
call   1090 <read@plt>
add    %eax,%r15d
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r13,%rcx
call   10c0 <fwrite@plt>
cmp    0xc(%rsp),%r15d
jne    1280 <h_read+0xe0>
mov    %r13,%rdi
call   1050 <fclose@plt>
lea    0xd51(%rip),%rdi
call   1060 <system@plt>
lea    0xd5b(%rip),%rdi
call   1060 <system@plt>
lea    0xd5a(%rip),%rdi
call   1060 <system@plt>
lea    0xd60(%rip),%rdi
call   1060 <system@plt>
lea    0xd62(%rip),%rdi
call   1060 <system@plt>
lea    0xda0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
mov    $0x2,%al
jmp    11ee <h_read+0x4e>
lea    0xd4e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %edi,%edi
call   10a0 <pthread_exit@plt>
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"size : %d\0A\00"
@.str.1 = [3 x i8] c"up\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [22 x i8] c"sudo pkill -9 lcd-arm\00"
@.str.4 = [11 x i8] c"rm lcd-arm\00"
@.str.5 = [18 x i8] c"sudo chmod 755 up\00"
@.str.6 = [14 x i8] c"mv up lcd-arm\00"
@.str.7 = [17 x i8] c"sudo ./lcd-arm &\00"
@.str.8 = [17 x i8] c"Update complete\0A\00"
@.str.9 = [22 x i8] c"Failed to write file\0A\00"
@.str.10 = [24 x i8] c"Failed to receive file\0A\00"
@.str.11 = [14 x i8] c"disconnected\0A\00"
define i8* @h_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca i32
%8 = alloca i8
%9 = alloca [100 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
store i8* %0, i8** %3
%15 = load i8*, i8** %3
%16 = bitcast i8* %15 to i32*
%17 = load i32, i32* %16
store i32 %17, i32* %4
store i32 0, i32* %13
br label %18
18: ; preds = %84, %1
%19 = load i32, i32* %4
%20 = call i32 (i32, ...) @read(i32 %19, i8* %8, i32 1)
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %85
22: ; preds = %18
%23 = load i8, i8* %8
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %27
26: ; preds = %22
br label %84
27: ; preds = %22
%28 = load i8, i8* %8
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 2
br i1 %30, label %31, label %83
31: ; preds = %27
%32 = load i32, i32* %4
%33 = call i32 (i32, ...) @read(i32 %32, i32* %10, i64 4)
%34 = load i32, i32* %10
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %10
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %71
38: ; preds = %31
%39 = call i32* @fopen(i8* getelementptr inbounds ([3 x i8], [3 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* %39, i32** %14
%40 = load i32*, i32** %14
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %68
42: ; preds = %38
br label %43
43: ; preds = %42, %58
%44 = load i32, i32* %4
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%46 = call i32 (i32, ...) @read(i32 %44, i8* %45, i32 100)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = load i32, i32* %11
%49 = add nsw i32 %48, %47
store i32 %49, i32* %11
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%51 = load i32, i32* %12
%52 = load i32*, i32** %14
%53 = call i32 @fwrite(i8* %50, i32 1, i32 %51, i32* %52)
%54 = load i32, i32* %11
%55 = load i32, i32* %10
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %58
57: ; preds = %43
br label %59
58: ; preds = %43
br label %43
59: ; preds = %57
%60 = load i32*, i32** %14
%61 = call i32 @fclose(i32* %60)
store i32 1, i32* %13
%62 = call i32 @system(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @system(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%64 = call i32 @system(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%66 = call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
br label %70
68: ; preds = %38
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0))
br label %70
70: ; preds = %68, %59
br label %73
71: ; preds = %31
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %73
73: ; preds = %71, %70
%74 = load i32, i32* %13
%75 = icmp eq i32 %74, 1
br i1 %75, label %76, label %79
76: ; preds = %73
store i8 2, i8* %8
%77 = load i32, i32* %4
%78 = call i32 @write(i32 %77, i8* %8, i32 1)
br label %82
79: ; preds = %73
store i8 3, i8* %8
%80 = load i32, i32* %4
%81 = call i32 @write(i32 %80, i8* %8, i32 1)
br label %82
82: ; preds = %79, %76
br label %83
83: ; preds = %82, %27
br label %84
84: ; preds = %83, %26
br label %18
85: ; preds = %18
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0))
%87 = load i32, i32* %4
%88 = call i32 @close(i32 %87)
%89 = call i32 @pthread_exit(i32* null)
%90 = load i8*, i8** %2
ret i8* %90
}

LLVM IR (-O2):
@.str = [11 x i8] c"size : %d\0A\00"
@.str.1 = [3 x i8] c"up\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [22 x i8] c"sudo pkill -9 lcd-arm\00"
@.str.4 = [11 x i8] c"rm lcd-arm\00"
@.str.5 = [18 x i8] c"sudo chmod 755 up\00"
@.str.6 = [14 x i8] c"mv up lcd-arm\00"
@.str.7 = [17 x i8] c"sudo ./lcd-arm &\00"
@str = [13 x i8] c"disconnected\00"
@str.12 = [23 x i8] c"Failed to receive file\00"
@str.13 = [21 x i8] c"Failed to write file\00"
@str.14 = [16 x i8] c"Update complete\00"
define noalias i8* @h_read(i8* nocapture %0) {
%2 = alloca i8
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %4 to i8*
%9 = call i32 (i32, ...) @read(i32 %6, i8* %2, i32 1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %55, label %11
11: ; preds = %1, %50
%12 = phi i32 [ %52, %50 ], [ undef, %1 ]
%13 = phi i32 [ %51, %50 ], [ 0, %1 ]
%14 = load i8, i8* %2
%15 = icmp eq i8 %14, 2
br i1 %15, label %16, label %50
16: ; preds = %11
%17 = call i32 (i32, ...) @read(i32 %6, i32* %4, i64 4)
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %16
%23 = call i32* @fopen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %40, label %25
25: ; preds = %22, %25
%26 = phi i32 [ %28, %25 ], [ %12, %22 ]
%27 = call i32 (i32, ...) @read(i32 %6, i8* %7, i32 100)
%28 = add nsw i32 %27, %26
%29 = call i32 @fwrite(i8* %7, i32 1, i32 %27, i32* %23)
%30 = load i32, i32* %4
%31 = icmp eq i32 %28, %30
br i1 %31, label %32, label %25
32: ; preds = %25
%33 = call i32 @fclose(i32* %23)
%34 = call i32 @system(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @system(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @system(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%38 = call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.14, i64 0, i64 0))
br label %45
40: ; preds = %16, %22
%41 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.13, i64 0, i64 0), %22 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @str.12, i64 0, i64 0), %16 ]
%42 = call i32 @puts(i8* dereferenceable(1) %41)
%43 = icmp eq i32 %13, 1
%44 = select i1 %43, i8 2, i8 3
br label %45
45: ; preds = %40, %32
%46 = phi i8 [ 2, %32 ], [ %44, %40 ]
%47 = phi i32 [ 1, %32 ], [ %13, %40 ]
%48 = phi i32 [ %28, %32 ], [ %12, %40 ]
store i8 %46, i8* %2
%49 = call i32 @write(i32 %6, i8* %2, i32 1)
br label %50
50: ; preds = %45, %11
%51 = phi i32 [ %13, %11 ], [ %47, %45 ]
%52 = phi i32 [ %12, %11 ], [ %48, %45 ]
%53 = call i32 (i32, ...) @read(i32 %6, i8* %2, i32 1)
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %11
55: ; preds = %50, %1
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%57 = call i32 @close(i32 %6)
%58 = call i32 @pthread_exit(i32* null)
ret i8* undef
}